鱼C论坛

 找回密码
 立即注册
查看: 2229|回复: 6

[已解决]求助,c语言问题

[复制链接]
发表于 2019-1-19 20:52:59 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 NYJYA 于 2019-1-20 09:17 编辑

题目描述:
谷学长有一个非常简单的问题给你,给你两个整数A和B,你的任务是计算A+B。



输入:
输入的第一行包含一个整数T(T<=20)表示测试实例的个数,然后2*T行,分别表示A和B两个正整数。注意整数非常大,那意味着你不能用32位整数来处理。你可以确定的是整数的长度不超过1000。



输出:
对于每一个样例,你应该输出两行,第一行是"Case #:",#表示第几个样例,第二行是一个等式"A+B=Sum",Sum表示A+B的结果。注意等式中有空格。
可以解释一下这个代码怎样运行的吗?
<div class="blockcode"><blockquote>
#include "stdio.h"

#include "string.h"

int main()

{

    int i,t,j=1,n,len,k;

    char sum[1009],sa[1009],sb[1009];

    scanf("%d",&t);

    getchar();

    while(t--)

    {    

        for(i=0;i<1009;i++)

            sum[i]='0';

        scanf("%s%s",sa,sb);

        len=strlen(sa);

        for(i=len-1,k=0;i>=0;i--,k++)

        {

            sum[k]=sum[k]-'0'+sa[i];

             n=k;

            while(sum[n]>'9')

            {

                sum[n+1]=sum[n+1]+(sum[n]-'0')/10;

                sum[n]=(sum[n]-'0')%10+'0';

                n++;

            }

        }

        len=strlen(sb);

        for(i=len-1,k=0;i>=0;k++,i--)

        {

            sum[k]=sum[k]-'0'+sb[i];

            n=k;

            while(sum[n]>'9')

            {

                sum[n+1]=sum[n+1]+(sum[n]-'0')/10;

                sum[n]=(sum[n]-'0')%10+'0';

                n++;

            }

        }

        printf("Case %d:\n%s + %s = ",j,sa,sb);

        j++;

        for(i=1008;sum[i]=='0';i--);

        for(;i>=0;i--)

            printf("%c",sum[i]);

        //printf("\n");

        if(t>0)

            printf("\n");

    }

    return 0;

}

最佳答案
2019-1-24 14:22:29
NYJYA 发表于 2019-1-24 12:00
想问一下字符串怎样模拟加减法?谢谢

想想小学数学课是怎么用竖式算加减法的
1、从低位加起,
2、满10就向高一位进1
然后你模拟这个过程就对了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-1-19 21:37:00 | 显示全部楼层
||=== 构建: Debug 在 4 中 (编译器: GNU GCC Compiler) ===|
G:\C++代码\4\main.cpp||In function 'int main()':|
G:\C++代码\4\main.cpp|12|warning: unknown conversion type character 'l' in format [-Wformat=]|
G:\C++代码\4\main.cpp|12|warning: unknown conversion type character 'l' in format [-Wformat=]|
G:\C++代码\4\main.cpp|12|warning: too many arguments for format [-Wformat-extra-args]|
G:\C++代码\4\main.cpp|14|error: incompatible types in assignment of 'long long int' to 'long long int [10]'|
G:\C++代码\4\main.cpp|16|error: incompatible types in assignment of 'long long int' to 'long long int [10]'|
G:\C++代码\4\main.cpp|18|error: incompatible types in assignment of 'long long int' to 'long long int [10]'|
G:\C++代码\4\main.cpp|24|warning: left operand of comma operator has no effect [-Wunused-value]|
G:\C++代码\4\main.cpp|30|warning: unknown conversion type character 'l' in format [-Wformat=]|
G:\C++代码\4\main.cpp|30|warning: unknown conversion type character 'l' in format [-Wformat=]|
G:\C++代码\4\main.cpp|30|warning: unknown conversion type character 'l' in format [-Wformat=]|
G:\C++代码\4\main.cpp|30|warning: too many arguments for format [-Wformat-extra-args]|
||=== 构建 失败: 3 error(s), 8 warning(s) (0 分, 0 秒) ===|

你的代码在
a=A+B;

  b=A;

  c=B;
出现了问题,a是一个数组,你想将a+b的long long类型的值赋给一个数组??
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-1-19 22:01:35 | 显示全部楼层
本帖最后由 NYJYA 于 2019-1-19 22:08 编辑
想都不要想 发表于 2019-1-19 21:37
||=== 构建: Debug 在 4 中 (编译器: GNU GCC Compiler) ===|
G:\C++代码\4\main.cpp||In function 'int m ...

代码是这个,不知道为什么复制到提问那个框[i]都不见了,谢谢你的解答,下面还有没有问题呢?
#include<stdio.h>
int main()
{
 int T,i,n;
 long long  A,B,a[10],b[10],c[10];
 scanf("%d",&T);
 for(i=0;i<T;i++)
 {
  scanf("%lld %lld",&A,&B);
  a[i]=A+B;
  b[i]=A;
  c[i]=B;
  
 }
 for(i=0,n=1;i<T,n<=T;i++,n++)
 {
  printf("Case %d:\n",n);
  printf(" %lld + %lld = %lld\n",b[i],c[i],a[i]);
 }
 return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-1-19 23:09:13 | 显示全部楼层
..1000位,这题明显是要你用字符串模拟加减法。long long也不够长啊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-1-20 12:11:26 | 显示全部楼层
Croper 发表于 2019-1-19 23:09
..1000位,这题明显是要你用字符串模拟加减法。long long也不够长啊

谢谢你啊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-1-24 12:00:39 | 显示全部楼层
Croper 发表于 2019-1-19 23:09
..1000位,这题明显是要你用字符串模拟加减法。long long也不够长啊

想问一下字符串怎样模拟加减法?谢谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-1-24 14:22:29 | 显示全部楼层    本楼为最佳答案   
NYJYA 发表于 2019-1-24 12:00
想问一下字符串怎样模拟加减法?谢谢

想想小学数学课是怎么用竖式算加减法的
1、从低位加起,
2、满10就向高一位进1
然后你模拟这个过程就对了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-3 06:29

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表