http://ac.jobdu.com/problem.php?pid=1124
原题没有给n的范围,但是说了可能n很大,我原本用的n为int型,然后转化为char[10],但是提示wrong answer,所以只好用char[100]存储n了,这样省略了将n转化成字符串的步骤。
注:
char str[100];
比较字符串的大小时:strcmp(str,"0") 返回0则相等
//题目1074:对称平方数
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;
int main()
{
int len , i;
char str[100];
while (cin >> str && strcmp(str,"0"))
{
int sum = 0;
len = strlen(str);
for (i = 0; i < len; i++)
sum += str[i] - '0';
while (sum >= 10)
{
char temp[100];
sprintf_s(temp, "%d", sum);//将10进制平方数的int型转化成字符型,从而头尾对称比较
len = strlen(temp);
sum = 0;
for (i = 0; i < len; i++)
sum += temp[i] - '0';
}
cout << sum << endl;
}
system("pause");
return 0;
}
注:以上博客内容中的所有代码均可以在本地VS2013上运行正确,若要到OJ上运行,有些地方要做稍微变化,比如printf_s要去掉_s之类的。这是编译器的问题。