| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 584 人关注过本帖
标题:杭电1002问题求指点。
只看楼主 加入收藏
既生瑜何生亮
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2012-2-7
结帖率:100%
收藏
已结贴  问题点数:10 回复次数:3 
杭电1002问题求指点。
Problem Description
I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.

 

Input
The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line consists of two positive integers, A and B. Notice that the integers are very large, that means you should not process them by using 32-bit integer. You may assume the length of each integer will not exceed 1000.

 

Output
For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line is the an equation "A + B = Sum", Sum means the result of A + B. Note there are some spaces int the equation. Output a blank line between two test cases.

 

Sample Input
2
1 2
112233445566778899 998877665544332211
 

Sample Output
Case 1:
1 + 2 = 3

Case 2:
112233445566778899 + 998877665544332211 = 1111111111111111110
http://acm.hdu.

#include<stdio.h>
#include<string.h>
#define N 1001
char a[N], b[N];
int c[N]={0}, d[N]={0};
int main()
{
    int i, j, m, n, t, h, k, l1, l2;
    scanf("%d", &h);
    for(i = 1; i <= h; i++)
    {
        scanf("%s",a);
        scanf("%s",b);
        l1 = strlen(a);
        l2 = strlen(b);
        if(l1 < l2)
            k = l2;
        else
            k = l1; t = k;
        for(j = 0;j < l1; k--,j++)
            c[k] = a[l1-1-j] - '0';
        k = t;
        for(j = 0;j < l2; k--,j++)
            d[k] = b[l2-1-j] - '0';
        for(j = t; j >= 0; j--)
        {
            c[j] += d[j];
            if(c[j] >= 10)
            {
                c[j] -= 10;
                c[j-1]++;
            }
        }
        printf("Case %d:\n", i);
        printf("%s + %s = ", a, b);
        if(c[0] == 0)
            for(j = 1; j <= t; j++)
                printf("%d",c[j]);
            else
                for(j = 0; j <= t; j++)
                    printf("%d",c[j]);
                printf("\n");
                if(i != h)
                    printf("\n");
                }
    return 0;
}
为什么自己运行的对,但是提交后wrong呢。
搜索更多相关主题的帖子: process positive problem 1002 1000 
2012-08-04 08:47
demonleer
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:10
帖 子:483
专家分:1225
注 册:2012-6-4
收藏
得分:0 
程序代码:
#include <stdio.h>
#include <stdlib.h>

char target1[20][1001] = {0};
char target2[20][1001] = {0};
char sum[20][1002] = {0};

void foo(int i)
{
    int j = 0, t = 0, m, x, b, r;
    char f = '0';
    while (target1[i][j]!='\n'&&target1[i][j]!=0) j++;
    while (target2[i][t]!='\n'&&target2[i][t]!=0) t++;
    m = j<t?j:t, x = j>t?j:t, b = x, r = j>t?1:0;
    while((m--))
    {
        if (target1[i][--j]+target2[i][--t]>'9'+'0')
        {
            if (f=='0') {sum[i][--x] = target1[i][j]+target2[i][t]-'9'-'1'+'0',f = '1';}
            else {sum[i][--x] = target1[i][j]+target2[i][t]-'9',f = '1';}
        }
        else
        {
            if (f=='0') {sum[i][--x] = target1[i][j]+target2[i][t]-'0';}
            else if (target1[i][j]+target2[i][t]=='9'+'0')
            {sum[i][--x] = '0',f = '1';}
            else {sum[i][--x] = target1[i][j]+target2[i][t]+'1'-2*'0',f='0';}
        }
    }
    if (0==x&&f=='1')
    {
        while (b--) sum[i][b+1] = sum[i][b];
        sum[i][0] = '1';
    }
    else if(f=='1')
    {
        while (f=='1'&&x!=0)
        {
            if (0==r)
            {
                if (target2[i][--x]=='9')
                {
                    sum[i][x] = '0';
                    f = '1';
                } 
                else
                {
                    sum[i][x] = target2[i][x] + '1' - '0';
                    f = '0';
                }
            }
            else
            {
                if (target1[i][--x]=='9')
                {
                    sum[i][x] = '0';
                    f = '1';
                } 
                else
                {
                    sum[i][x] = target1[i][x] + '1' - '0';
                    f = '0';
                }
            }
        }
        if (0==x&&f=='1')
        {
            while (b--) sum[i][b+1] = sum[i][b];
            sum[i][0] = '1';
        }
        else
        {
            if (0==r) while (x--) sum[i][x] = target2[i][x];
            else while (x--) sum[i][x] = target1[i][x];
        }
    }
    else
    {
        if (0==r) while (x--) sum[i][x] = target2[i][x];
        else while (x--) sum[i][x] = target1[i][x];
    }
}

void main()
{
    int i, j, t;
    for (j=0,scanf("%d",&i), t=i; i--;foo(j++)) scanf("%s",target1[j]),scanf("%s",target2[j]);
    for (j=0; j<t;j++) printf("Case %d:\n",j+1),printf("%s + %s = %s\n\n",target1[j],target2[j],sum[j]);
}


写的比较仓促,你将就看吧,一直说Presentation Error,说我答案输出的格式有问题,懒的整了,回来优化下,这些的比较乱。
2012-08-04 13:16
demonleer
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:10
帖 子:483
专家分:1225
注 册:2012-6-4
收藏
得分:10 
AC了,上面的代码之所以不AC是因为最后一行多输出了个空行。优化了下,如下:

程序代码:
#include <stdio.h>
#include <stdlib.h>

#define CALCULATE(a,b,s,f) do{((a)+(b)>105?((s)=(a)+(b)+f-106+'0',f=1):\
            ((a)+(b)==105?(f==1?((s)='0',f=1):((s)='9')):\
            ((s)=(a)+(b)+f-'0',f=0)));}while (0)

char target1[20][1001] = {0};
char target2[20][1001] = {0};
char sum[20][1002] = {0};

void foo(int i)
{
    int j = 0, t = 0, m, x, b, r;
    int f = 0;
    while (target1[i][j]!='\n'&&target1[i][j]!=0) j++;
    while (target2[i][t]!='\n'&&target2[i][t]!=0) t++;
    m = j<t?j:t, x = j>t?j:t, b = x, r = j>t?1:0;
    while(m--)
    {
        --j, --t, --x;
        CALCULATE(target1[i][j],target2[i][t],sum[i][x],f);
    }
    if (x==0)
    {
        if (f==1)
        {
            while (b--) sum[i][b+1] = sum[i][b];
            sum[i][0] = '1';
        }
    }
    else
    {
        while (x--)
        {
            if (0==r) CALCULATE(target2[i][x],'0',sum[i][x],f);
            else CALCULATE(target1[i][x],'0',sum[i][x],f);
        }
        if (f==1)
        {
            while (b--) sum[i][b+1] = sum[i][b];
            sum[i][0] = '1';
        }    
    }
}

void main()
{
    int i, j, t;
    for (j=0,scanf("%d",&i), t=i; i--;foo(j++)) scanf("%s",target1[j]),scanf("%s",target2[j]);
    for (j=0; j<t;j++) printf("Case %d:\n",j+1),printf("%s + %s = %s\n",target1[j],target2[j],sum[j]),j==t-1?1:printf("\n");
}
2012-08-04 20:38
既生瑜何生亮
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2012-2-7
收藏
得分:0 
回复 3楼 demonleer
多谢了。回去仔细研读一下。
2012-08-05 17:45
快速回复:杭电1002问题求指点。
数据加载中...
 
   



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

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