hdu_1013_A + B Problem II_(模拟)
来源:互联网 发布:淘宝刷单 青青岛 编辑:程序博客网 时间:2024/05/29 09:20
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1002
解题思路:利用数组进行大数的相加。内置数据类型不能满足整数位数的要求。
样例:
<span style="white-space:pre"></span>1 999
<span style="white-space:pre"></span>999 1
#include <iostream>#include <cstring>using namespace std;int num1[1010],num2[1010];int t[1010];int result[1010];char str1[1010],str2[1010];void add(int len1,int len2){int i,j;//字符转换成数字,再反转 for(i = 0;i < len1;i++)t[i] = str1[i] - 48;for(i = len1 - 1,j = 0;i >= 0;i--,j++)num1[j] = t[i];for(i = 0;i < len2;i++)t[i] = str2[i] - 48;for(i = len2 - 1,j = 0;i >= 0;i--,j++)num2[j] = t[i];int k = 0,temp,cnt = 0;i = 0;while(i < len1 || i < len2){temp = num1[i] + num2[i] + cnt;//cnt表示进位 if(temp >= 10){cnt = 1;result[i] = temp % 10;}else{result[k] = temp;cnt = 0;}i++;k++;}result[k] = cnt;//假如是99+99,则最后的进位1要记录下来 for(i = 0;i < len1;i++)cout << str1[i];cout << " + ";for(i = 0;i < len2;i++)cout << str2[i];cout << " = ";if(result[k] != 0) cout << result[k];int ii;for(ii = k-1;ii >= 0;ii--){ cout << result[ii];}cout << endl;}int main(int argc, char *argv[]){int s,cnt = 1;cin >> s; int a = s; while(s--){memset(num1,0,sizeof(num1));memset(num2,0,sizeof(num2));//每次都要初始化num1和num2 的数组,没有则会WA memset(result,0,sizeof(result));cin >> str1 >> str2;cout << "Case " << cnt << ":" << endl;add(strlen(str1),strlen(str2));if(cnt < a) //最后的样例没有空行 cout << endl;cnt++;}return 0;}
0 0
- hdu_1013_A + B Problem II_(模拟)
- hdu-1002 A + B Problem II_(高精度加法)
- HDU1023_Train Problem II_卡特兰数
- poj_1000 A+B Problem 模拟
- HDU 1002 A + B Problem II(模拟)
- NYOJ 513 & HDU 1753 A+B Problem IV(模拟)
- 【hdoj1002】 A + B Problem II(模拟加法)
- hdu 1002 A + B Problem II(大数模拟加法)
- Codeforces 202B Brand New Easy Problem(模拟)
- Codeforces--629B--Far Relative’s Problem(模拟)
- 【NOIP2017模拟赛】构造 A+B Problem(好题)
- 北邮09复试模拟problem B
- 北邮08复试模拟problem B
- 【MAP模拟】SDUT 1471 A + B problem
- 【模拟】洛谷 P1001 A+B Problem
- CSU1642-Problem B-前缀和数组/模拟
- HDU1027_Ignatius and the Princess II_非模拟做法
- Codeforces Round #343 (Div. 2) -B. Far Relative’s Problem(模拟)
- Linux一些学习资源
- Mac下下载android4.2源码,进行源码调试
- 使用Intellij Idea 搭建tomcat 6源码分析环境
- JAVA知识点总结(三)成员变量和局部变量的区别
- 求最小环(floyd,dijkstra)
- hdu_1013_A + B Problem II_(模拟)
- 4.2 File
- SQL还原数据库代码
- iOS 动态计算UITableViewCell 高度 以及计算UILable 高度 自定义Cell
- Redhat 6 安装mysql 5.6.22遇到的问题总结
- 谷歌“我的足迹”源码
- HPU1250: HH的米5 【并查集】+【欧拉图】
- DBInputFormat的用法
- 人生选择与程序算法