| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 963 人关注过本帖
标题:求大神指点这个程序格式错在哪里了。
只看楼主 加入收藏
高茜
Rank: 1
等 级:新手上路
帖 子:46
专家分:1
注 册:2015-1-12
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:14 
求大神指点这个程序格式错在哪里了。
杭电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

这个是我的程序代码
#include <stdio.h>
#include <string.h>
int main ()
{
    char a[21][1001],b[21][1001];
    int c[21][2001]={{0}};
    int k[21]={0};
    int n,i,p,q,t,m;
    scanf ("%d",&n);
    for (i=1;i<=n;i++)
    {
        scanf ("%s %s",a[i],b[i]);
        p=strlen(a[i])-1;
        q=strlen(b[i])-1;
        while ((p>=0)&&(q>=0))
        {
            if(((a[i][p]-'0')+(b[i][q]-'0')+c[i][k[i]])>=10)
            {
                c[i][k[i]]=(a[i][p]-'0')+(b[i][q]-'0')+c[i][k[i]]-10;
                c[i][k[i]+1]++;
            }
            else
            c[i][k[i]]=a[i][p]-'0'+b[i][q]-'0'+c[i][k[i]];
            k[i]++;
            p--;
            q--;
        }

        if (p>=0)
        {
            for (t=p;t>=0;t--)
            {
                c[i][k[i]]=(a[i][t]-'0')+c[i][k[i]];
                k[i]++;
            }
        }
        else if (q>=0)
        {
            for (t=q;t>=0;t--)
            {
                c[i][k[i]]=(b[i][t]-'0')+c[i][k[i]];
                k[i]++;
            }
        }
        if (c[i][k[i]]!=0)
        k[i]++;
  }
        for (i=1;i<=n;i++)
        {
          printf ("Case %d:\n",i);
          printf ("%s + %s = ",a[i],b[i]);
          for (m=k[i]-1;m>=0;m--)
          printf("%d",c[i][m]);
          if(i<n)
          printf ("\n\n");
        }
    return 0;
}
搜索更多相关主题的帖子: positive problem process number simple 
2015-02-09 13:19
aneeg
Rank: 1
等 级:新手上路
帖 子:75
专家分:8
注 册:2014-4-10
收藏
得分:4 
scanf那里缺少取址符吧
2015-02-09 13:31
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:4 
如果完全按照题意,“assume the length of each integer will not exceed 1000”来说,那么结果最大应该是 1001位。
虽然你写的也是[1001],但你却是从1开始技术的。(想不通为什么会有从1开始计数)

你的代码好复杂,尤其那个k数组,不看了,直接运行你的代码,输入题目中的测试用例,发现『最后少了一个'\n'』

2015-02-09 14:42
高茜
Rank: 1
等 级:新手上路
帖 子:46
专家分:1
注 册:2015-1-12
收藏
得分:0 
回复 2楼 aneeg
字符串传递的就是首地址值吧,应该不需要取地址符也可以。
2015-02-09 20:39
高茜
Rank: 1
等 级:新手上路
帖 子:46
专家分:1
注 册:2015-1-12
收藏
得分:0 
回复 3楼 rjsp
应该怎么改格式啊?我运行了觉得跟给出的格式一样呀😳
2015-02-09 20:40
高茜
Rank: 1
等 级:新手上路
帖 子:46
专家分:1
注 册:2015-1-12
收藏
得分:0 
回复 3楼 rjsp
c数组是我后来添加的,开始写时候i从1计数的,后来也没改,这个不重要
2015-02-09 20:42
sefeeee
Rank: 2
等 级:论坛游民
帖 子:11
专家分:25
注 册:2015-2-9
收藏
得分:0 
scanf ("%s %s",a[i],b[i]); 这里编译是没有出错 但是该数组里面的数据绝对不是你输入的数据  你可以试试  在一个数组的下标是从0开始的 你从1开始那么就会造成内存浪费了。
2015-02-09 22:18
wmf2014
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:216
帖 子:2039
专家分:11273
注 册:2014-12-6
收藏
得分:4 
经实验,可运行,有些错误如下:
1,scanf ("%d",&n);根据题意要做0<n<20的判断。
2,scanf ("%s %s",a[i],b[i]);scanf不能过滤非数字字符,你后面的代码也没有过滤
3,不能得到正确结果,如9999999999+99=1000000000098,你程序的运算结果为999999991098,主要是没有处理连续进位导致的。
4,显示格式错误,应该把显示运行结果放到总循环里显示。不过,你总循环是使用变量i,你用于显示结果的循环变量就不能再用i
5,花了点时间帮你做了下,应该可以交作业,代码如下:
程序代码:
//杭电1002题大数据加法运算  wmf2014 2015-2-10
#include<stdio.h>
int inab(char *p)
{//加数或被加数输入,返回数据长度
    int i=0;
    char c;
    fflush(stdin);
    while(((c=getchar())!=10&&i<1000)||i==0)
    {//持续等待有效输入(未溢出,是数字),直到有回车
        if(c>47&&c<58)
        {
            p[i]=c;
            i++;
        }
        p[i]=0;
    }
    return i;
}
void moveab(char *p,int r)
{//将字符串向右移动r位,前面用字符‘0’填充,主要是为了让加数和被加数对齐,方便运算
    int i,l;
    for(l=0;p[l]!=0;l++);//首先获取字符串长度
    for(i=l;i>=0;i--)p[i+r]=p[i];//右移r位
    for(i=0;i<r;i++)p[i]='0';//前面填充r个0
}
void main()
{
    char a[1000],b[1000],sum[1001];
    int i,j,k,l,al,bl,s=0;
    while(s<1||s>19)
    {//本循环确保你输入一个大于0小于20的case数量
        fflush(stdin);//清空输入,否则如输错会死循环
        printf("请输入case数量(1<数量<20):");
        scanf("%d",&s);
        if(s<1||s>19)printf("case数量错误!");
        printf("\n");
    }
    for(i=0;i<s;i++)
    {
        printf("\n****Case%d****\n",i+1);//显示题头
        printf("请输入被加数A:");
        al=inab(a);
        printf("请输入加数B:");
        bl=inab(b);
        printf("%s+%s=",a,b);//显示算式
        if(al>bl)//将相加的两个数按最长的对齐,方便按位相加
        {
            l=al-1;
            moveab(b,al-bl);
        }
        else
        {
            l=bl-1;
            moveab(a,bl-al);
        }
        k=0;//k暂存进位
        sum[0]=0;//sum字符串初始化,用于存储相加后的结果
        while(l>=0||k>0)
        {
            if(l<0)
                j=k;
            else
                 j=k+a[l]+b[l]-96;//按位相加
            if(j>9)  //处理进位
            {
                k=1;
                j=j-10;
            }
            else
                k=0;
            moveab(sum,1);
            sum[0]=j+48;
            l--;
        }
        printf("%s\n",sum);//显示运算结果
    }
}

能编个毛线衣吗?
2015-02-10 06:47
高茜
Rank: 1
等 级:新手上路
帖 子:46
专家分:1
注 册:2015-1-12
收藏
得分:0 
回复 8楼 wmf2014
在杭电上提交了一下,显示Output Limit Exceeded错误。
(┬_┬)
2015-02-10 10:56
高茜
Rank: 1
等 级:新手上路
帖 子:46
专家分:1
注 册:2015-1-12
收藏
得分:0 
回复 7楼 sefeeee
我试了一下a[i]输出的字符串就是我输入的字符串。
2015-02-10 11:01
快速回复:求大神指点这个程序格式错在哪里了。
数据加载中...
 
   



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

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