Big vs Big(链表)
2016-03-27 17:32
190 查看
【题目描述】
Calculate the addtion of any two positive big integers.
Requirements:
Test data can be more than 64 digits, therefore you MUST use a linked list to store an integer (any big).
【输入】
The first line contains the number of test cases, N.
In the next 2*N lines, each line contains a string of number.
【输出】
Out put N lines. Each line represent the sum of A and B.
【我的程序】
Calculate the addtion of any two positive big integers.
Requirements:
Test data can be more than 64 digits, therefore you MUST use a linked list to store an integer (any big).
【输入】
The first line contains the number of test cases, N.
In the next 2*N lines, each line contains a string of number.
【输出】
Out put N lines. Each line represent the sum of A and B.
【我的程序】
#include <stdlib.h> #include <iostream> using namespace std; typedef struct node{ int num; node *av; }* wei; wei newWei(int x) { wei y=(wei)malloc(sizeof(node)); y->num=x; y->av=NULL; return y; } int main() { int n; char ch; cin>> n; cin.get(); for (int i=0;i<n;i++) { wei x=newWei(cin.get()-'0'); while ((ch=cin.get())!='\n'){ wei p=newWei(ch-'0'); p->av=x; x=p; } wei y=newWei(cin.get()-'0'); while ((ch=cin.get())!='\n'){ wei p=newWei(ch-'0'); p->av=y; y=p; } wei re=newWei(x->num+y->num); int jw=(re->num)/10; re->num%=10; x=x->av; y=y->av; while (x!=NULL && y!=NULL) { wei p=newWei(x->num+y->num+jw); jw=(p->num)/10; p->num%=10; p->av=re; re=p; x=x->av; y=y->av; } while (y!=NULL) { wei p=newWei(y->num+jw); jw=p->num/10; p->num%=10; p->av=re; re=p; y=y->av; } while (x!=NULL) { wei p=newWei(x->num+jw); jw=p->num/10; p->num%=10; p->av=re; re=p; x=x->av; } if (jw>0) cout<< jw; while (re!=NULL){ cout<< re->num; re=re->av; } cout<< endl; } return 0; }
相关文章推荐
- 进程和线程
- httpd.conf配置后php7,出现apache无法启动的现象
- UILable,UIButton字体加粗
- 关于c#中获取listbox中选中项的数量和所有选项的数量
- 乱码终极解决大全
- 基本算法C#实现
- ASP.NET-前台view返回model集合
- HTML5存储—LocalStorage 和 sessionStroage
- BZOJ 3612 HEOI2014 大工程 树链剖分求LCA的优越
- Cannot delete or update a parent row
- spring3 struts2 利用aop在actioin层捕获异常
- Gallery的整体架构
- jquery重要知识点
- 给TextView加背景
- Xcode项目依赖
- PHP为什么慢?
- 多线程之NSOperation
- 《数据结构》2.10设计一个算法,删除顺序表中值为item的元素,要求算法的时间复杂度是O(n),空间复杂度是O(1)
- TCP连接为什么要三次握手
- STL与泛型编程<九>:迭代器相关辅助函数