A + B Problem II(利用字符串进行四则运算)

【题目讲解】Sum Problem & A + B Problem II 这道英文题看起来很唬人,其实就是个垃圾,废话不多说,上代码! Sum Problem 显示标签 题目讨论题目统计全部提交 时间限制:C/C++ 500MS,其他语言 1000MS 内存限制:C/C++ 32MB,其他语言 64MB 难度:普及+/提高 出题人:深学 描述 Hey, welcome to HDOJ(Hangzhou Dianzi University Online Judge). In this problem, your task is to calculate SUM... 阅读详情

A + B Problem II

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 337902 Accepted Submission(s): 65560

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


由于超过整型能计算的范围,所以需要用字符串来特殊处理


AC代码
#include <stdio.h>
#include <string.h>

char s1[1000+10];
char s2[1000+10];
char s[1000+10];
int main()
{
    int n;
    scanf("%d",&n);
    int k=0;//输出样例序号
    int flag=0;//输出格式控制标记
    while(n--)
    {
        memset(s2,0,sizeof s1);
        memset(s2,0,sizeof s2);
        memset(s,0,sizeof s);
        scanf("%s",s1);
        getchar();
        scanf("%s",s2);
        getchar();
        int len1=strlen(s1);
        int len2=strlen(s2);
        int cnt=0;
        int ok=0;//用来记录是否进位
        if(len1>len2)
        {
            while(len1)
            {
                int cp;
                if(len2!=0)
                {
                    cp=ok+s1[--len1]+s2[--len2]-'0'-'0';
                }
                else
                    cp=ok+s1[--len1]-'0';
                if(cp<10)
                    {s[cnt++]=cp+'0';ok=0;}
                else
                    {s[cnt++]=cp%10+'0';ok=1;}
              //  printf("%c++len1=%d len2=%d++\n",s[cnt],len1,len2);
            }
            if(ok==1)
            s[cnt++]='1';
            s[cnt]='\0';
        }
        else if(len1<=len2)
        {
            while(len2)
            {
                int cp;
                if(len1!=0)
                {
                    cp=ok+s1[--len1]+s2[--len2]-'0'-'0';
                }
                else
                    cp=ok+s2[--len2]-'0';
                if(cp<10)
                    {s[cnt++]=cp+'0';ok=0;}
                else
                    {s[cnt++]=cp%10+'0';ok=1;}
                //printf("%c++len1=%d len2=%d++\n",s[cnt],len1,len2);
            }
            if(ok==1)
            s[cnt++]='1';
            s[cnt]='\0';
        }
        if(flag++!=0)
            printf("\n");//输出格式控制
        printf("Case %d:\n",++k);
        printf("%s + %s = ",s1,s2);
        int i;
        for(i=cnt-1; i>=0; i--)//正向储存,反向输出
            printf("%c",s[i]);
        printf("\n");
    }
    return 0;
}

我基本是按思路来写,看了一下别人的代码,简洁不少,算法一样

#include<stdio.h>
#include<string.h>
int main()
{
    char a[1000],b[1000],c[1001];
    int i,j=1,p=0,n,n1,n2;
    scanf("%d",&n);
    while(n)
    {
        scanf("%s %s",a,b);
        printf("Case %d:\n",j);
        printf("%s + %s = ",a,b);
        n1=strlen(a)-1;
        n2=strlen(b)-1;
        for(i=0;n1>=0||n2>=0;i++,n1--,n2--)//把我的二合一了,很厉害
        {
            if(n1>=0&&n2>=0){c[i]=a[n1]+b[n2]-'0'+p;}
            if(n1>=0&&n2<0){c[i]=a[n1]+p;}
            if(n1<0&&n2>=0){c[i]=b[n2]+p;}
            p=0;
            if(c[i]>'9'){c[i]=c[i]-10;p=1;}
        }
        if(p==1){printf("%d",p);}
        while(i--)
        {printf("%c",c[i]);}
        j++;
        if(n!=1){printf("\n\n");}
        else {printf("\n");}
        n--;
    }
}

这题还有个思路,就是把两个串变成等长,短串前面补0,最后直接加
例如123456 + 546 ,可以变成123456 + 000456 ,这样直接加起来,不用考虑长度问题
但是呢又要多一步把短串补0,也挺麻烦
目前还米有更好的思路。。。

杭电ACM第1002题(C语言版)答案——A + B Problem II #include #include int shu(char a) { return (a-'0'); } int main(){ char a[1000],b[1000]; int num[1001]; int n,i,j=1,al,bl,k,t; scanf("%d",&n); while(n--) { if(j!=1) 阅读详情

相关推荐

基于Ultralytic实现多路摄像头目标检测

基于Ultralytic实现多路摄像头目标检测前面的文章分享了单路摄像头画面的YOLO目标检测,这篇文章主要分享一下多路摄像头画面的实时YOLO目标检测。测试环境在Windows系统下进行。

qq_57674776的博客 956

由用户输入A+B形式的四则运算,让电脑输出答案;

由用户输入A+B形式的四则运算,让电脑输出答案;

m0_64951177的博客 3644

利用CubeMX复现正点原子TFTLCD驱动例程

①准备delay.c、delay.h、sys.c和sys.h四个文件。FMC的工作原理暂时先欠着,先记录一下CRUD的过程。这里我们使用的是BANK1 的第一个分区。点击 Configuration->FMC。②把fmc自动生成的代码注释掉一部分。配置参数如下图 17.5.1。③这个printf语句要屏蔽掉。选项卡之下配置相关参数。④整体代码,按需裁剪。

qq_59757948的博客 1620

ACM/HDU A B Problem II 大数定理 的坑点

坑点在于:当最后一行数据输出时只有一个换行符 而其他行的数据的输出有两个换行符!!! 贴上代码:我认为将a数组和b数组都memset的操作很好,因为后面不用比较a和b数组的大小了 #include<iostream> #include<cstdio> #include<string> #include<cstdlib> #include<...

hyanhuianqing的博客 217

Java使用easyExcel生成excel文件直接写入邮件附件并发送

1.引入pom依赖 <!-- email --> <dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4.7</version> </dependency> <!-- easyexcel需要依赖poi jar包 --> <depe

weixin_50493320的博客 3916

Windows-Anaconda3+TensorFlow安装教程

测试环境:win10-64位和win7-64位 (系统必须是64位,因为TensorFlow目前只支持64位) 一、安装Anaconda3 1.下载Anaconda3 Anaconda官网链接:link 这里我们选择Windows版本,python3.6-version-64位,如下图所示。 2.下载完成之后,点击安装Anaconda3 注意:安装过程中要选择添加环境变量,如图 待安装完成...

DoctorCuiLab的博客 3093

A + B Problem II (大数加模拟,,坑点在格式错误)

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&lt;=T&lt;=20) which means the ...

red_red_red的博客 761

芯片详解——AD7606

AD7606详解

weixin_45661757的博客 5046

A + B Problem II

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&lt;=T&lt;=20) which means the ...

Angeliaaa的博客 244

一文入门BMS(电池管理系统)

本文章是基于曾师兄的文稿《具有组网扩展功能的新能源汽车电池管理系统》而写的文章。该文章主要是对BMS(Battery Management System,电池管理系统)做一些简要的介绍,借此助各位也助我入门BMS。有纰漏请指出,转载请说明。学习交流请发邮件 1280253714@qq.com。

weixin_45817947的博客 2万+

手把手教您同花顺如何设置大盘分时均价线

本文将手把手教你如何在同花顺PC端开启大盘指数分时图📈

XiaoqiangClub的博客 4650

杭电OJ 1002 A + B Problem II

A + B Problem II Time Limit: 2000/1000 MS (Java/Others)Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 464212Accepted Submission(s): 89876...

huang 445

AIPex:AI + 自然语言驱动的浏览器自动化扩展

AIPex是一款革命性的Chrome扩展,通过AI技术实现自然语言驱动的浏览器自动化。它支持30多种自动化工具,包括智能标签页管理(分组、切换、批量操作)、页面内容提取(文本、链接、元数据)、交互元素操作(点击、滚动)以及表单智能填写等功能。用户只需用自然语言下达指令,即可快速完成复杂的浏览器操作,大幅提升工作效率。AIPex特别适合需要处理大量重复性网页操作的用户,如数据分析师、产品经理等。

blues_C的博客 1150

HDU-1002 A + B Problem II

HDU 1002 A + B Problem II I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B...

你呢的博客 900

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

【杭电OJ1002】A + B Problem II

A + B Problem II Time Limit: 2000/1000 MS (Java/Others)Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 488297Accepted Submission(s): 94234 Problem Description I have a very...

纸短情长的博客 293

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

103 A+B Problem II

A+B Problem II 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述 I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B. A,B must be positi

syklar&miracle 538

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

HPU 1002 A + B Problem II【大数】

A + B Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 261413    Accepted Submission(s): 50581 Problem Description I have a

dxx_111的博客 702

杭电ACM A + B Problem II

A + B Problem II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2300 Accepted Submission(s): 886   Problem Descript

琴声悠扬 688

A+B Problem II 大数加法

原文链接: A+B Problem II 大数加法 ...

阿豪 107

HDOJ~~A + B Problem II

A + B Problem II    Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)  Problem Description    I have a very simple problem for you. Given two integers A and

lv_pg的博客 498

NYOJ - A+B Problem II

A+B Problem II 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述 I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B. A,B must be positi

SevenMIT的专栏 523

HDU----A + B Problem II

A + B Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 359992    Accepted Submission(s): 69867 Problem Description I have a v

yan 312
上一篇: UVa - 227 Puzzle
下一篇: C++中引用&、const的用法
Coder_Jeremy Wu
博客等级 码龄10年 12粉丝 53原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值