| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 465 人关注过本帖
标题:POJ 2282求教大牛
只看楼主 加入收藏
james9102
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2010-8-15
结帖率:0
收藏
已结贴  问题点数:20 回复次数:4 
POJ 2282求教大牛
POJ 2282
The Counting Problem
Time Limit: 3000MS        Memory Limit: 65536K
Total Submissions: 2199        Accepted: 1156
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 0's in the list, ten 1's, seven 2's, three 3's, 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
0 0
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
Source
Shanghai 2004



#include<stdio.h>
#include<string.h>
void cntdigit(long n,int cnt[],int t)   //以132为例
{
    int i,x,y;
    if(n<=0) return ;
    x=n/10;y=n%10;      
    n=x;
    for(i=0;i<=y;i++)     //计数130,131,132的个位0,1,2
        cnt[i]+=t;
    for(;x!=0;x/=10)     //计数130,131,132的前两位13
        cnt[x%10]+=(y+1)*t;
    for(i=0;i<=10;i++)    //计数0-9,10-19,20-29….120-129的个位数
        cnt[i]+=n*t;
    cnt[0]=cnt[0]-t;//注意要减1(去掉0)
    cntdigit(n-1,cnt,t*10);//计数0-12(注意:乘以10倍)
}
void main()
{
    long a,b,t;
    int i=-1,j,sum1[10]={0},sum2[10]={0};
    while(scanf("%d %d",&a,&b))
    {
        if(a==0&&b==0)  break;
        if(a>b)
        {
            t=a;
            a=b;
            b=t;
        }
        memset(sum1,0,sizeof(sum1));
        memset(sum2,0,sizeof(sum2));//设断点处
        cntdigit(a-1,sum1,1);
        cntdigit(b,sum2,1);
        for(j=0;j<=9;j++)
            printf("%d ",sum2[j]-sum1[j]);
        printf("\n");
    }
}


向高手求教:为什么上述代码在VC中运行是错误输出,在Dev C++是正确输出?关键的问题在于:在VC中函数sum2传值时为什么会将sum1[0]的值改变?本菜鸟喜欢用VC,这个问题让我很困惑。请各路大牛F5试试,在代码设断点处观察为何这样(函数实参传向形参时sum2传值时为什么会将sum1[0]的值改变)?
搜索更多相关主题的帖子: POJ 
2010-08-15 15:12
jack10141
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:陕西西安
等 级:小飞侠
威 望:6
帖 子:706
专家分:2271
注 册:2010-8-10
收藏
得分:20 
你的算法感觉不是很科学呢呵呵!为什么在函数调用的时候,存在类型不一致的地方?

[ 本帖最后由 jack10141 于 2010-8-15 15:52 编辑 ]

Coding就像一盒巧克力,你永远不会知道你会遇到什么BUG
别跟我说你是不能的,这让我愤怒,因为这侮辱了你的智慧
2010-08-15 15:49
jack10141
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:陕西西安
等 级:小飞侠
威 望:6
帖 子:706
专家分:2271
注 册:2010-8-10
收藏
得分:0 
回复 楼主 james9102
程序代码:
#include<stdio.h>
#include<string.h>
void cntdigit(int n,int m, int cnt[])   //以132为例
{
    int i,x;
    for(i=n;i<=m;i++)
    {
        x=i;
        cnt[x%10]++;
        while(x/=10)
            cnt[x%10]++;
    }
}
int main()
{
    int a,b,t;
    int i=-1,j,sum2[10]={0};
    while(scanf("%d %d",&a,&b))
    {
        if(a==0&&b==0)  break;
        if(a>b)
        {
            t=a;
            a=b;
            b=t;
        }
        memset(sum2,0,sizeof(sum2));
        cntdigit(a,b,sum2);
        for(j=0;j<=9;j++)
            printf("%d ",sum2[j]);
        printf("\n");
    }
}
看看我在你的基础上改写的 可以满足要求么?

Coding就像一盒巧克力,你永远不会知道你会遇到什么BUG
别跟我说你是不能的,这让我愤怒,因为这侮辱了你的智慧
2010-08-15 16:08
jack10141
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:陕西西安
等 级:小飞侠
威 望:6
帖 子:706
专家分:2271
注 册:2010-8-10
收藏
得分:0 
个人感觉是你的算法太繁琐,看了个大概,有些细节还不是很明白,所以在两个环境下会有不同的结果,这个我也说不清楚!
所以我能做的就是帮你简化算法!

Coding就像一盒巧克力,你永远不会知道你会遇到什么BUG
别跟我说你是不能的,这让我愤怒,因为这侮辱了你的智慧
2010-08-15 16:14
james9102
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2010-8-15
收藏
得分:0 
回复 2楼 jack10141
谢谢您的回复。但请注意题目中a,b的取值范围。a,b小于等于100000000,如果取a=1;b=100000000;程序算不出来。你可以用你的程序试试。我的繁琐算法可以保证能算出来。
2010-08-16 12:02
快速回复:POJ 2282求教大牛
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018781 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved