设N是一个四位数,它的9倍恰好是其反序数(例如:1234 的反序数是4321),求N的值。
我的代码:
#include<cstdio>
#include<iostream>
#include<stdlib.h>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
for (int i = 1000; i <= 1111; i++)
{
string origin = std::to_string(i);
reverse(origin.begin(), origin.end());
string reverse = origin;
string mul = std::to_string(i * 9);
if (reverse.compare(mul)==0)
{
cout << i << endl;
}
}
return 0;
}
心得:
1.algorithm头文件中reverse的使用
reverse(origin.begin(), origin.end());
2.string类型中compare的使用:
if (reverse.compare(mul)==0)
c++ string类compare的使用
https://blog.csdn.net/zcc1229936385/article/details/79747371
3.我后来自己写的reverse:
string myreverse(string str)
{
int tail = str.length() - 1;
int head = 0;
while (head < tail)
{
char temp1 = str[head];
char temp2 = str[tail];
str[head] = temp2;
str[tail] = temp1;
head++;
tail--;
}
return str;
}