算法实现题1-1 统计数字问题
问题描述:
一本书的页码从自然数1 开始顺序编码直到自然数n
。书的页码按照通常的习惯编排,
每个页码都不含多余的前导数字0。例如,第6 页用数字6 表示,而不是06 或006 等。数
字计数问题要求对给定书的总页码n,计算出书的全部页码中分别用到多少次数字0,1,
2,…,9。
编程任务:
给定表示书的总页码的10 进制整数n (1≤n≤109) 。编程计算书的全部页码中分别用
到多少次数字0,1,2,…,9。
数据输入:
输入数据由文件名为input.txt的文本文件提供。
每个文件只有1 行,给出表示书的总页码的整数n。
结果输出:
程序运行结束时,将计算结果输出到文件output.txt中。输出文件共有10行,在第k行
输出页码中用到数字k-1 的次数,k=1,2,…,10。
输入文件示例 输出文件示例
input.txt
11
output.txt
1
4
1
1
1
1
1
1
1
1
为了实现此算法.我刚开始想到的是
1.一个整数数组int a[10]来记录0-9出现的次数.
2.用一个FOR循环来遍历,从1到n.
在写代码时,我又发现input.txt中的是数字字符串,而不是数字.所以我又在想,如何才能把数字字符串转换为数字呢?刚开始想到的
是用一个函数直接转换,
- #include <stdio.h>
- #include <math.h>
- void count(int x,int a[])
- {
- int i;
- for(i=0;i<10;i++)
- {
- if(i==x)
- {
- a[i]++;
- break;
- }
- }
- }
- main()
- {
- FILE *fp_input;
- FILE *fp_output;
- int num=0,i,j=0;
- char s[4];
- int ch;
- int a[10]={0,0,0,0,0,0,0,0,0,0};/*数组最好人工初始化,在turboc2中如果没有人工初始化的话,那么程序结果将出错, */
- /*也 就是没有人工初始化时,他的默认值并不为0*/
- system("cls");
- if((fp_input=fopen("D://text//input.txt","r"))==NULL)
- { /*不然是打开还是写入一个文件都要事先判断是不是打开或新建成功,这种写法标准*/
- printf("Error on open D://text//input.txt");
- getch(); /*getch()等待用户按任意键后,程序才能再往下执行.一般是用来让用户能有时间看清说明文字*/
- exit(1); /*exit(1)表示正常退出程序*/
- }
- ch=fgetc(fp_input);
- while(ch!=EOF) /*读取TXT文本中的字符串*/
- {
- s[j++]=ch;
- ch=fgetc(fp_input);
- }
- s[j]='/0'; /*注意字符串数组中,要在最后一个字符后加上字符串结束符'/0',表示到一个字符串的结尾*/
- printf("%s/n",s);
- fclose(fp_input);
- for(i=0;i<strlen(s);i++)
- {
- num=num+(s[i]-'0')*pow(10,strlen(s)-i-1);/*把数字字符串转换为数字*/
- }
- printf("%d/n",num);
- /*统计各个数字出现的次数*/
- for(i=1;i<=num;i++)
- {
- int x,y,z;
- if(i<=9)
- {
- count(i,a);
- }else if(i<=99)
- {
- y=i/10;
- z=i%10;
- count(y,a);
- count(z,a);
- }else
- {
- x=i/100;
- y=(i-x*100)/10;
- z=i%100;
- count(x,a);
- count(y,a);
- count(z,a);
- }
- }
- for(i=0;i<10;i++)
- printf("%d/n",a[i]);
- if((fp_output=fopen("D://text//output.txt","wt+"))==NULL)
- {
- printf("Error on open D://text//output.txt!");
- getch();
- exit(1);
- }
- /*把输出写到output.txt*/
- for(i=0;i<10;i++)
- {
- int x,y,z;
- if(a[i]<=9)
- {
- fputc(a[i]+'0',fp_output);
- fputc('/n',fp_output);
- }
- else if(a[i]<=99)
- {
- y=a[i]/10;
- z=a[i]%10;
- fputc(y+'0',fp_output);
- fputc(z+'0',fp_output);
- fputc('/n',fp_output);
- }
- else
- {
- x=a[i]/100;
- y=(a[i]-x*100)/10;
- z=a[i]%100;
- fputc(x+'0',fp_output);
- fputc(y+'0',fp_output);
- fputc(z+'0',fp_output);
- fputc('/n',fp_output);
- }
- }
- fclose(fp_output);
- }
总结:
1.写代码时要从简单到复杂,比如刚开始时,我把输出部分也写出来,不过,当程序出错时,我很难判断是哪部分有部分.所以,我先把它注释掉,然后用最简单的printf()来观察结果,而且,我认为在程序中间,多使用printf()来观察各部分的结果.
2.调试过程中,我发现了两个问题导致最后的输出结果不一样.
A.pow()函数,在没有用#include <math.h>时,结果出错.
B.整数数组int a[10],刚开始我以为如果没有初始化时,系统默认值为0.而且<<C语言教程>>上也是这样写的.
但,结果却是错的,
我实验一下:
main()
{
int a[10],i;
printf("at beginning i=%d/n",i);
for(i=0;i<10;i++)
printf("a[%d]=%d/n",i,a[i]);
}
结果却是:
at beginning i=1105
a[0]=-50
a[1]=1145
a[2]=72
a[3]=0
a[4]=0
a[5]=-40
a[6]=1346
a[7]=0
a[8]=64
a[9]=3129
看来,这本教程是垃圾,不能看的.还是去看<<thinging in c>>
3.这代码没有健壮性,我设计,也只是考虑了n为三位数时的情况.

4663

被折叠的 条评论
为什么被折叠?



