c语言编程:求1000--9999之间所有回文数的个数(如1111、1221、4994等均为回文数)

2025-01-04 18:41:10
推荐回答(4个)
回答1:

#include
#include
using namespace std;
void main()
{
for(unsigned int i = 1000; i <= 9999; i++)
{
char zzz[100];
sprintf(zzz,"%i",i);
string lol = zzz;
if(lol[0] == lol[3] && lol[1] == lol[2])
{
cout << lol << endl;
}
}
}

回答2:

存成字符型直接一个一个的判断
还不用区分下奇数和偶数情况呢

回答3:

真毛

回答4:

//---------------------------------------------------------------------------

#include

int ch(int b)
{
char a[5];
sprintf(a,"%d",b);
if (a[0]==a[3]&&a[1]==a[2])
return 1;

return 0;
}
int main(void)
{
int i;
for (i=1000; i<10000; i++)
if (ch(i)) printf("%d\n",i);
return 0;
}
//---------------------------------------------------------------------------