当前位置: 代码迷 >> C语言 >> 一道ACM题:The Counting Problem
  详细解决方案

一道ACM题:The Counting Problem

热度:823   发布时间:2007-08-19 22:35:55.0
一道ACM题:The Counting Problem

题目如下:

The Counting Problem
Submit: 57 Accepted:18
Time Limit: 3000MS Memory Limit: 65536K

Description
Given two integers a and b, we write the numbers between a and b, inclusive, in a list. Your task is to calculate the number of occurrences of each digit. For example, if a = 1024 and b = 1032, the list will be
1024 1025 1026 1027 1028 1029 1030 1031 1032
there are ten 0s in the list, ten 1s, seven 2s, three 3s, and etc.


Input
The input consists of up to 500 lines. Each line contains two numbers a and b where 0 < a, b < 100000000. The input is terminated by a line “0 0”, which is not considered as part of the input.


Output
For each pair of input, output a line containing ten numbers separated by single spaces. The first number is the number of occurrences of the digit 0, the second is the number of occurrences of the digit 1, etc.


Sample Input

1 10
44 497
346 542
1199 1748
1496 1403
1004 503
1714 190
1317 854
1976 494
1001 1960
00


Sample Output

1 2 1 1 1 1 1 1 1 1
85 185 185 185 190 96 96 96 95 93
40 40 40 93 136 82 40 40 40 40
115 666 215 215 214 205 205 154 105 106
16 113 19 20 114 20 20 19 19 16
107 105 100 101 101 197 200 200 200 200
413 1133 503 503 503 502 502 417 402 412
196 512 186 104 87 93 97 97 142 196
398 1375 398 398 405 499 499 495 488 471
294 1256 296 296 296 296 287 286 286 247
下面是我的代码:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define END "00"

long int a[500][2]={0};

void change(char x[],int j )//把读取的字符窜转化成一组数 存入a[500][2]中
{
int m=0,n=0;
int i=0;
while(x[i]!=32)
{
m=m*10+(x[i]-'0');
i++;
}
i++;
while(x[i]!='\0')
{
n=n*10+(x[i]-'0');
i++;
}
a[j][0]=m;
a[j][1]=n;
}

void calculate(long int a,long int b)//计算每一组数的结果 并打印之
{
long int i,j;
long int temp;
long int num;
long int result[10]={0};

if(a<=b)
{
for(i=a;i<=b;i++)
{
num=i;
while(num!=0)
{
temp=num%10;
num=num/10;
result[temp]++;
}
}
}
else
{
for(i=b;i<=a;i++)
{
num=i;
while(num!=0)
{
temp=num%10;
num=num/10;
result[temp]++;
}
}
}
for(i=0;i<10;i++)
printf("%d ",result[i]);
}

int main()
{
int sum=0;//计算输入的测试数据的数量
long int i=0;
char c[20];//存放每一行输入的字符窜
gets(c);
while(strncmp(c,END,2))
{
change(c,sum);
gets(c);
sum++;
}
for(i=0;i<sum;i++)
{
calculate(a[i][0],a[i][1]);
printf("\n");
}
system("pause");
return 0;
}

测试了很多组数据都没错,但是一提交就RUNNING ERROR
那位朋友帮帮忙,谢谢

搜索更多相关的解决方案: Counting  ACM  Problem  The  

----------------解决方案--------------------------------------------------------
这样做即使不RE
也要TLE
这个题目用第归或者直接第推的LOG(N);

----------------解决方案--------------------------------------------------------
  相关解决方案