博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu 1002 A + B Problem II
阅读量:5257 次
发布时间:2019-06-14

本文共 5053 字,大约阅读时间需要 16 分钟。

题目连接

A + B Problem II

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

测模板的。。

1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 using std::max; 11 using std::cin; 12 using std::cout; 13 using std::endl; 14 using std::swap; 15 using std::string; 16 using std::istream; 17 using std::ostream; 18 struct BigN { 19 typedef unsigned long long ull; 20 static const int Max_N = 2000; 21 int len, data[Max_N]; 22 BigN() { memset(data, 0, sizeof(data)), len = 0; } 23 BigN(const int num) { 24 memset(data, 0, sizeof(data)); 25 *this = num; 26 } 27 BigN(const char *num) { 28 memset(data, 0, sizeof(data)); 29 *this = num; 30 } 31 void cls() { len = 0, memset(data, 0, sizeof(data)); } 32 BigN& clean(){ while (len > 1 && !data[len - 1]) len--; return *this; } 33 string str() const { 34 string res = ""; 35 for (int i = len - 1; ~i; i--) res += (char)(data[i] + '0'); 36 if (res == "") res = "0"; 37 res.reserve(); 38 return res; 39 } 40 BigN operator = (const int num) { 41 int j = 0, i = num; 42 do data[j++] = i % 10; while (i /= 10); 43 len = j; 44 return *this; 45 } 46 BigN operator = (const char *num) { 47 len = strlen(num); 48 for (int i = 0; i < len; i++) data[i] = num[len - i - 1] - '0'; 49 return *this; 50 } 51 BigN operator + (const BigN &x) const { 52 BigN res; 53 int n = max(len, x.len) + 1; 54 for (int i = 0, g = 0; i < n; i++) { 55 int c = data[i] + x.data[i] + g; 56 res.data[res.len++] = c % 10; 57 g = c / 10; 58 } 59 while (!res.data[res.len - 1]) res.len--; 60 return res; 61 } 62 BigN operator * (const BigN &x) const { 63 BigN res; 64 int n = x.len; 65 res.len = n + len; 66 for (int i = 0; i < len; i++) { 67 for (int j = 0, g = 0; j < n; j++) { 68 res.data[i + j] += data[i] * x.data[j]; 69 } 70 } 71 for (int i = 0; i < res.len - 1; i++) { 72 res.data[i + 1] += res.data[i] / 10; 73 res.data[i] %= 10; 74 } 75 return res.clean(); 76 } 77 BigN operator * (const int num) const { 78 BigN res; 79 res.len = len + 1; 80 for (int i = 0, g = 0; i < len; i++) res.data[i] *= num; 81 for (int i = 0; i < res.len - 1; i++) { 82 res.data[i + 1] += res.data[i] / 10; 83 res.data[i] %= 10; 84 } 85 return res.clean(); 86 } 87 BigN operator - (const BigN &x) const { 88 assert(x <= *this); 89 BigN res; 90 for (int i = 0, g = 0; i < len; i++) { 91 int c = data[i] - g; 92 if (i < x.len) c -= x.data[i]; 93 if (c >= 0) g = 0; 94 else g = 1, c += 10; 95 res.data[res.len++] = c; 96 } 97 return res.clean(); 98 } 99 BigN operator / (const BigN &x) const {100 return *this;101 }102 BigN operator += (const BigN &x) { return *this = *this + x; }103 BigN operator *= (const BigN &x) { return *this = *this * x; }104 BigN operator -= (const BigN &x) { return *this = *this - x; }105 BigN operator /= (const BigN &x) { return *this = *this / x; }106 bool operator < (const BigN &x) const {107 if (len != x.len) return len < x.len;108 for (int i = len - 1; ~i; i--) {109 if (data[i] != x.data[i]) return data[i] < x.data[i];110 }111 return false;112 }113 bool operator >(const BigN &x) const { return x < *this; }114 bool operator<=(const BigN &x) const { return !(x < *this); }115 bool operator>=(const BigN &x) const { return !(*this < x); }116 bool operator!=(const BigN &x) const { return x < *this || *this < x; }117 bool operator==(const BigN &x) const { return !(x < *this) && !(x > *this); }118 };119 istream& operator >> (istream &in, BigN &x) {120 string src;121 in >> src;122 x = src.c_str();123 return in;124 }125 ostream& operator << (ostream &out, const BigN &x) {126 out << x.str();127 return out;128 }129 int main() {130 #ifdef LOCAL131 freopen("in.txt", "r", stdin);132 freopen("out.txt", "w+", stdout);133 #endif134 int t;135 char s1[1100], s2[1100];136 scanf("%d", &t);137 for (int i = 0; i < t; i++) {138 BigN A, B;139 scanf("%s %s", s1, s2);140 A = s1, B = s2;141 printf("Case %d:\n", i + 1);142 cout << s1 << " + " << s2 << " = " << A + B << endl;143 if (i < t - 1) cout << endl;144 }145 return 0;146 }
View Code

 

转载于:https://www.cnblogs.com/GadyPu/p/4541946.html

你可能感兴趣的文章
反弹SHELL
查看>>
关闭Chrome浏览器的自动更新和升级提示
查看>>
移动、尺寸改变
查看>>
poj2255Tree Recovery【二叉树重构】
查看>>
tcpcopy 流量复制工具
查看>>
vue和react的区别
查看>>
第十一次作业
查看>>
负载均衡策略
查看>>
微信智能开放平台
查看>>
ArcGIS Engine 中的绘制与编辑
查看>>
Oracle--通配符、Escape转义字符、模糊查询语句
查看>>
子网划分讲解及练习(一)
查看>>
c# 文件笔记
查看>>
第一页 - 工具的使用(webstorm)
查看>>
Linux 进程资源用量监控和按用户设置进程限制
查看>>
IE浏览器整页截屏程序(二)
查看>>
D3.js 之 d3-shap 简介(转)
查看>>
制作满天星空
查看>>
类和结构
查看>>
CSS3选择器(二)之属性选择器
查看>>