我被一个int转换为字符串,扭转字符串,使该字符串返回一个int来比较它(可能比较字符串也没有问题)制作一个简单的回文探测器,但由于某些原因,反向串保持原值并将新的值添加到行而不是替换它们......为什么?
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main(){
string tempreverse;
string temp;
stringstream out;
int tempnumber, tempnumber2;
int palindrome = 0;
for(int i = 100; i < 111; i++){
for(int j = 100; j < 111; j++){
tempnumber = i * j;
out << tempnumber;
temp = out.str();
tempreverse = string (temp.rbegin(), temp.rend());
tempnumber2 = atoi(tempreverse.c_str());
if (tempnumber == tempnumber2){
palindrome = tempnumber;
}
}
}
cout << palindrome << "\n";
cout << "Press ENTER to continue...";
cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
return 0;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
161 次 |
| 最近记录: |