当前位置: 首页 > news >正文

【大数加法】POJ-1503、NYOJ-103

1503:Integer Inquiry

总时间限制: 
1000ms
内存限制: 
65536kB
描述
One of the first users of BIT's new supercomputer was Chip Diller. He extended his exploration of powers of 3 to go from 0 to 333 and he explored taking various sums of those numbers. 
``This supercomputer is great,'' remarked Chip. ``I only wish Timothy were here to see these results.'' (Chip moved to a new apartment, once one became available on the third floor of the Lemon Sky apartments on Third Street.) 
输入
The input will consist of at most 100 lines of text, each of which contains a single VeryLongInteger. Each VeryLongInteger will be 100 or fewer characters in length, and will only contain digits (no VeryLongInteger will be negative). 

The final input line will contain a single zero on a line by itself. 
输出
Your program should output the sum of the VeryLongIntegers given in the input.
样例输入
123456789012345678901234567890
123456789012345678901234567890
123456789012345678901234567890
0
样例输出
370370367037037036703703703670
  • 先说说为啥要用算法实现大数加法:

    我们来看看数据:

    bool型为布尔型,占1个字节,取值0或1。

    BOOL型为int型,一般认为占4个字节,取值TRUE/FALSE/ERROR。

    sbyte型为有符号8位整数,占1个字节,取值范围在128~127之间。

    bytet型为无符号16位整数,占2个字节,取值范围在0~255之间。

    short型为有符号16位整数,占2个字节,取值范围在-32,768~32,767之间。

    ushort型为无符号16位整数,占2个字节,取值范围在0~65,535之间。

    int型为有符号32位整数,占4个字节,取值范围在-2,147,483,648~2,147,483,647之间。

    uint型为无符号32位整数,占4个字节,取值范围在0~4,294,967,295之间。

    long型为64位有符号整数,占8个字节,取值范围在9,223,372,036,854,775,808~9,223,372,036,854,775,807之间。

    ulong型为64位无符号整数,占8个字节,取值范围在0~18,446,744,073,709,551,615之间。

    float型为32位单精度实数,占4个字节,取值范围3.4E+10的负38次方~3.4E+10的38次方之间。

    double型为64位实数,占8个字节,取值范围1.7E+10的负308次方~1.7E+10的正308次方。

 1 int num1[MAX],num2[MAX];
 2 void Add(char a[],char b[]){
 3     int i,j,len1,len2,max;    
 4     memset(num1,0,sizeof(num1));
 5     memset(num2,0,sizeof(num2));
 6     len1 = strlen(a);
 7     len2 = strlen(b);
 8     //获得最长数组的长度
 9     //两数相加最多不会超过最大数的位数+1,减少循环次数
10     max = len1 > len2 ? len1 : len2;
11     //将两字符串数组转换为数字数组
12     for(i = len1 - 1,j = 0;i >= 0;i--){
13         num1[j++] = a[i] - '0';
14     }
15     for(i = len2 - 1,j = 0;i >= 0;i--){
16         num2[j++] = b[i] - '0';
17     }
18     //模拟进位运算
19     for(i = 0;i <= max;i++){
20         num1[i] += num2[i];
21         if(num1[i] > 9){
22             num1[i] -= 10;
23             num1[i + 1] ++;
24         }
25     }
26     //输出
27     int ac = 0;
28     for(i = max;i >= 0;i--){//输出排除0
29         if(num1[i] != 0)
30             ac = 1;
31         if(ac){
32             printf("%d",num1[i]);        
33         }
34     }
35 }
  • 感觉学会了,于是乎,就找了题,继续练练手,就是POJ-1503.
  • 然后就陷入了深坑中,首先就是那个输入,竟然一直想要用二维数组,真是。。。只能怪练得还太少。
  • 解题过程中,脑中想的方法过于复杂,那肯定就是错误的,一定要跳出这个思维,想别的思路解决。
 1 //POJ-1503-1
 2 #include <cstdio>  
 3 #include <cstring>  
 4 const int MAXN = 200;  
 5 int main()  
 6 {  
 7     char s[MAXN]; 
 8     int i, j;
 9     int m;
10     scanf("%d",&m);
11     while(m--){
12         int sum[MAXN] = {0}; 
13         while(gets(s))  
14         {  
15             int len = strlen(s);  
16             if(s[0] == '0' && len == 1)  
17                 break;  
18             //sum[]保存所有组的各位的和
19             for(i = 110, j = len-1; j >= 0; i--, j--)  
20             {  
21                 sum[i] += s[j]-'0';  
22             }  
23         }  
24         //将sum[]每个元素,进行进位
25         for(i = 110; i > 0; i--)  
26         {  
27             sum[i-1] += sum[i] / 10;  
28             sum[i] %= 10;  
29         }  
30         //排除和为0的情况
31         for(i = 0; sum[i] == 0 && i < 111; i++)  
32         {  
33             if(i == 111)//意味着全为零  
34             {  
35                 printf("0\n");  
36             }  
37         }  
38         for( ; i < 111; i++)  
39         {  
40             printf("%d",sum[i]);  
41         }  
42         printf("\n\n"); 
43     }     
44     return 0;  
45 }

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 positive.

 
输入
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.
输出
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.
样例输入
2
1 2
112233445566778899 998877665544332211
样例输出
Case 1:
1 + 2 = 3
Case 2:
112233445566778899 + 998877665544332211 = 1111111111111111110
 1 //NYOJ-103
 2 #include<cstdio>
 3 #include<cstring>
 4 const int MAX = 1002;
 5 char str1[MAX],str2[MAX];
 6 int num1[MAX],num2[MAX];
 7 void Add(char a[],char b[]){
 8     int i,j,len1,len2,max;    
 9     memset(num1,0,sizeof(num1));
10     memset(num2,0,sizeof(num2));
11     len1 = strlen(a);
12     len2 = strlen(b);
13     //获得最长数组的长度
14     //两数相加最多不会超过最大数的位数+1,减少循环次数
15     max = len1 > len2 ? len1 : len2;
16     //将两字符串数组转换为数字数组
17     for(i = len1 - 1,j = 0;i >= 0;i--){
18         num1[j++] = a[i] - '0';
19     }
20     for(i = len2 - 1,j = 0;i >= 0;i--){
21         num2[j++] = b[i] - '0';
22     }
23     //模拟进位运算
24     for(i = 0;i <= max;i++){
25         num1[i] += num2[i];
26         if(num1[i] > 9){
27             num1[i] -= 10;
28             num1[i + 1] ++;
29         }
30     }
31     //输出
32     int ac = 0;
33     for(i = max;i >= 0;i--){//输出排除0
34         if(num1[i] != 0)
35             ac = 1;
36         if(ac){
37             printf("%d",num1[i]);        
38         }
39     }
40 }
41 int main(){
42     int n,i = 1;
43     scanf("%d",&n);
44     while(n--){
45         scanf("%s %s",str1,str2);    
46         printf("Case %d:\n%s + %s = ",i,str1,str2);
47         if(str1[0] == '0' && str2[0] == '0'){
48             printf("0\n");
49         }else{
50             Add(str1,str2);
51             printf("\n");        
52         }
53         i++;
54     }
55     return 0;
56 }
  • 减法、乘法、除法明天继续

 

相关文章:

  • MySQL锁机制总结(二)
  • CentOS 7关闭图形桌面开启文本界面
  • excel VLOOKUP函数的用法
  • ubuntu 查看和关闭端口
  • LCS最长公共子序列java实现
  • 正则表达式的概述
  • 防止APK反编译和二次加密
  • where 1=-1 and 1=1 会不会影响查询效率?
  • 你正在用却不知道它们让微信比其它社交App更强大的6大功能
  • 毕业后两个月的日子--奋斗的菜鸟
  • 关于C++类型检查的一点小挫折
  • MySQL基础(一)
  • rest api方式实现对文档库的管理
  • 程序员的苦与乐
  • 【算法学习笔记】27.动态规划 解题报告 SJTU OJ 1254 传手绢
  • 002-读书笔记-JavaScript高级程序设计 在HTML中使用JavaScript
  • Dubbo 整合 Pinpoint 做分布式服务请求跟踪
  • JavaScript新鲜事·第5期
  • Java应用性能调优
  • node.js
  • SegmentFault 2015 Top Rank
  • 阿里中间件开源组件:Sentinel 0.2.0正式发布
  • 闭包,sync使用细节
  • 编写高质量JavaScript代码之并发
  • 从0搭建SpringBoot的HelloWorld -- Java版本
  • 发布国内首个无服务器容器服务,运维效率从未如此高效
  • 将 Measurements 和 Units 应用到物理学
  • 离散点最小(凸)包围边界查找
  • 使用parted解决大于2T的磁盘分区
  • 因为阿里,他们成了“杭漂”
  • 用Python写一份独特的元宵节祝福
  • 原生Ajax
  • ​批处理文件中的errorlevel用法
  • (C#)获取字符编码的类
  • (Mirage系列之二)VMware Horizon Mirage的经典用户用例及真实案例分析
  • (ros//EnvironmentVariables)ros环境变量
  • (超详细)2-YOLOV5改进-添加SimAM注意力机制
  • (六)c52学习之旅-独立按键
  • (三) prometheus + grafana + alertmanager 配置Redis监控
  • (三)模仿学习-Action数据的模仿
  • .net 4.0 A potentially dangerous Request.Form value was detected from the client 的解决方案
  • .net web项目 调用webService
  • .NET 的静态构造函数是否线程安全?答案是肯定的!
  • .NET 应用架构指导 V2 学习笔记(一) 软件架构的关键原则
  • .NET/C# 解压 Zip 文件时出现异常:System.IO.InvalidDataException: 找不到中央目录结尾记录。
  • .NET企业级应用架构设计系列之技术选型
  • .NET下ASPX编程的几个小问题
  • .Net下的签名与混淆
  • .secret勒索病毒数据恢复|金蝶、用友、管家婆、OA、速达、ERP等软件数据库恢复
  • ??如何把JavaScript脚本中的参数传到java代码段中
  • ??在JSP中,java和JavaScript如何交互?
  • [ 隧道技术 ] 反弹shell的集中常见方式(二)bash反弹shell
  • [BZOJ4010]菜肴制作
  • [COI2007] Sabor
  • [CTF]2022美团CTF WEB WP