ACM ZOJ-2000求指导http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1000

baominhao123 2012-02-05 11:15:22
我发现的规律是1~10 9个
10~100 9个
100~1000 90个
1000~10000 90个
然后依次类推 900 900 9000 9000 ····
我的代码现输入n确定n是在哪个范围内的第几个的数,接着构造数输出。
自己测试了一些数据都对了,可是提交上去就错了。
求各位高手解释一下我的错误··
求各位高手解释一下我的错误··
#include<stdio.h>
#include<string.h>
#include<math.h>
int fact(t)
{
int i,s=9;
for(i=1;i<t;i++)
s=s*10;
return s;
}
int main()
{
int n;
while(scanf("%d",&n)&&n)
{

if(n<10)
{
printf("%d\n",n);
continue;
}
if(n>=10&&n<=18)
{
printf("%d%d\n",n-9,n-9);
continue;
}
else
{
n=n-18;
double m=2;
int s=0;
int count=3;
while(1)
{
int t=m;
s=s+fact(t);
if(s>=n)
{
n=n-(s-fact(t));
break;
}
m=m+0.5;
count++;
}
char str[100];
int i,j;
if(count%2==0)//构造长度为偶数的回文数
{
int x=1;
for(i=1;i<count/2;i++)
x=x*10;
x=x+n-1;
printf("%d",x);
int j=0;
while(x)
{
str[j++]=x%10+'0';
x=x/10;
}
str[j]='\0';
printf("%s",str);
printf("\n");
}
if(count%2!=0)//构造长度为奇数的回文数
{
int y=1;
for(j=1;j<count/2+1;j++)
y=y*10;
y=y+n-1;
printf("%d",y);
j=0;
while(y)
{
str[j++]=y%10+'0';
y=y/10;
}
str[j]='\0';
for(i=1;i<j;i++)
printf("%c",str[i]);
printf("\n");
}
}
}
return 0;
}

...全文
176 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
gqjjqg 2012-02-06
  • 打赏
  • 举报
回复
http://www.cnblogs.com/pcwl/archive/2011/04/28/2031483.html

规律可以参考这个,写的虽然不是很详细,用sample case 里的值去套这个规律,应该是没有问题的。
面包大师 2012-02-06
  • 打赏
  • 举报
回复
另外建议楼主,将整型定义为long long型吧,否则会溢出,
你的方法是可行的,只是觉得你应该写一个更加通用的程序,呵呵
程序员小迷 2012-02-06
  • 打赏
  • 举报
回复
到底是什么规律?没看懂

楼主打断点,调一下,不就行了
面包大师 2012-02-06
  • 打赏
  • 举报
回复
你这样先发现规律再去写这个程序是会有问题的,这样虽然输出的结果是对的,但是并没有达到这个程序设计的目的

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧