基础练习 特殊回文数
时间限制:1.0s 内存限制:512.0MB
问题描述
123321是一个非常特殊的数,它从左边读和从右边读是一样的。
输入一个正整数n, 编程求所有这样的五位和六位十进制数,满足各位数字之和等于n 。
输入一个正整数n, 编程求所有这样的五位和六位十进制数,满足各位数字之和等于n 。
输入格式
输入一行,包含一个正整数n。
输出格式
按从小到大的顺序输出满足条件的整数,每个整数占一行。
样例输入
52
样例输出
899998
989989
998899
989989
998899
数据规模和约定
1<=n<=54。
直接打表
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int a[60][100];
char str[10],s[10];
void count(){
int i,j,e,n,b;memset(a,0,sizeof(a));
for(i=10000;i<=99999;++i){
sprintf(s,"%d",i);
strcpy(str,s);
strrev(str);
if(strcmp(str,s)==0){
n=0;
for(j=0;j<5;++j){
n+=str[j]-'0';
}
e=0;
while(1){
if(a[n][e]==0){
a[n][e]=i;break;
}
else e++;
}
}
}
for(i=100000;i<=999999;++i){
sprintf(s,"%d",i);
strcpy(str,s);
strrev(str);
if(strcmp(str,s)==0){
n=0;
for( j=0;j<6;++j){
n+=str[j]-'0';
}
e=0;
while(1){
if(a[n][e]==0){
a[n][e]=i;break;
}
else e++;
}
}
}
}
int main()
{
count();
int i,n;
while(scanf("%d",&n)==1){
for(i=0;i<100;++i){
if(a[n][i]==0)break;
printf("%d\n",a[n][i]);
}
}
return 0;
}