博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UVA465:Overflow
阅读量:6306 次
发布时间:2019-06-22

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

WA了三次;仅仅是因为输出输入数据时,先去了了前导0.罪过啊!!

 

1 #include
2 #include
3 #include
4 #include
5 using namespace std; 6 7 inline string reverse(const string a) 8 { 9 string t(a.rbegin(), a.rend()); 10 return t; 11 } 12 string operator + (const string& a, const string& b) 13 { 14 string sum = ""; 15 int c = 0, i = 0; 16 for( ; i < a.size() && i < b.size(); ++i) 17 { 18 c += a[i] - '0' + b[i] - '0'; 19 sum += c%10 + '0'; 20 c /= 10; 21 } 22 while(i < a.size()) 23 { 24 c += a[i] - '0'; 25 sum += c % 10 + '0'; 26 c /= 10; 27 ++i; 28 } 29 while(i < b.size()) 30 { 31 c += b[i] - '0'; 32 sum += c % 10 + '0'; 33 c /= 10; 34 ++i; 35 } 36 if(c) 37 sum += 1 + '0'; 38 return sum; 39 } 40 string operator +=(string& a, string& b) 41 { 42 return a = a+b; 43 } 44 string operator * (string& a, string& b) 45 { 46 if(a.size() > b.size()) 47 { 48 string t(a); 49 a = b; 50 b = t; 51 } 52 string tot = "0"; 53 for(int i = 0; i != a.size(); ++i) 54 { 55 string sum(i, '0'); 56 int c = 0; 57 for(int j = 0; j != b.size(); ++j) 58 { 59 c += (a[i] - '0') * (b[j] - '0'); 60 sum += (c % 10 + '0'); 61 c /= 10; 62 } 63 while(c) 64 { 65 sum += (c%10 + '0'); 66 c /= 10; 67 } 68 tot += sum; 69 } 70 return tot; 71 } 72 73 string del_pre_zero(string& a) 74 { 75 int i = 0; 76 for(; a[i] == '0' && i != a.size(); ++i); 77 if(i == a.size()) 78 a = "0"; 79 else 80 { 81 string b(a.begin()+i, a.end()); 82 a = b; 83 } 84 return a; 85 } 86 87 inline bool is_bigger(const string& a, const string& b) 88 { 89 if(a.size() != b.size()) 90 return a.size() > b.size(); 91 return a > b; 92 } 93 int main() 94 { 95 string a, b, c, sum; 96 int n = (numeric_limits
::max)(); 97 stringstream ss; 98 ss << n; 99 string max_int;100 ss >> max_int;101 //cout << max_int << endl;//2147483647102 while(cin >> a >> c >> b)//getline(cin, a), a != "0")103 {104 cout << a << " " << c << " " << b << endl;105 a = del_pre_zero(a);106 b = del_pre_zero(b);107 if(is_bigger(a, max_int))108 cout << "first number too big" << endl;109 if(is_bigger(b, max_int))110 cout << "second number too big" << endl;111 sum = "";112 a = reverse(a);113 b = reverse(b);114 if(c == "+")115 sum = a + b;116 else if(c == "*")117 sum = a * b;118 sum = reverse(sum);119 sum = del_pre_zero(sum);120 if(is_bigger(sum, max_int))121 cout << "result too big" << endl;122 }123 return 0;124 }
465:Overflow

 

 

1 #include
2 #include
3 #include
4 5 using namespace std; 6 7 string del_pre_zero(const string& a) 8 { 9 int i = 0; 10 for(; a[i] == '0' && i != a.size(); ++i); 11 if(i == a.size()) 12 return "0"; 13 string t(a.begin()+i, a.end()); 14 return t; 15 } 16 bool is_not_small(const string& a, const string& b) 17 { 18 if(a.size() != b.size()) 19 return a.size() > b.size(); 20 return a >= b; 21 } 22 23 string reverse(const string& a) 24 { 25 string t(a.rbegin(), a.rend()); 26 return t; 27 } 28 string operator - (const string& a, const string& b) 29 { 30 //cout << "a: " << a << " " << "b: " << b << endl; 31 string result = ""; 32 string x(a), y(b); 33 bool flag = true; 34 if(!is_not_small(a, b)) 35 { 36 flag = false; 37 string t(x); 38 x = y; 39 y = t; 40 } 41 x = reverse(x); 42 y = reverse(y); 43 int i, cur, c; 44 i = cur = c = 0; 45 for(; i < y.size(); ++i) 46 { 47 cur = x[i] - y[i] + c; 48 (cur < 0) ? (cur += 10, c = -1) : (c = 0); 49 result +=(cur + '0'); 50 } 51 while(i < x.size()) 52 { 53 cur = x[i] - '0' + c; 54 (cur < 0) ? (cur += 10, c = -1) : (c = 0); 55 result += (cur + '0'); 56 ++i; 57 } 58 if(!flag) 59 { 60 string t = "-"; 61 t += result; 62 result = t; 63 } 64 result = reverse(result); 65 return del_pre_zero(result); 66 } 67 68 string operator -= (string& a, const string& b) 69 { 70 return a = a - b; 71 } 72 string operator / (const string& a, const string& b) 73 { 74 if(!is_not_small(a, b)) 75 return "0"; 76 if(a == b) 77 return "1"; 78 string x(a), y(b); 79 int count = 0; 80 while(is_not_small(x, y)) 81 { 82 x -= y; 83 ++count; 84 } 85 stringstream ss; 86 ss << count; 87 string result = ""; 88 ss >> result; 89 return result; 90 } 91 string operator % (const string& a, const string& b) 92 { 93 if(!is_not_small(a, b)) 94 return a; 95 if(a == b) 96 return "0"; 97 string x(a), y(b); 98 while(is_not_small(x, y)) 99 {100 x -= y;101 // cout << "***" << x << "***" << endl;102 }103 return x;104 }105 106 int main()107 {108 string a, b, c, result;109 std::ios::sync_with_stdio(false);110 while(cin >> a >> c >> b, b != "0")111 {112 a = del_pre_zero(a);113 b = del_pre_zero(b);114 if(c == "%")115 result = a % b;116 else if(c == "/")117 result = a / b;118 //cout << a - b << endl;119 cout << result << endl;120 }121 return 0;122 }
10494 - If We Were a Child Again

 

 

posted on
2013-05-21 12:17 阅读(
...) 评论(
...)

转载于:https://www.cnblogs.com/sanghai/archive/2013/05/21/3090506.html

你可能感兴趣的文章
结构化程序设计03 - 零基础入门学习Delphi12
查看>>
今天才知道怎么插入代码!!!!!!!!!
查看>>
D2007在64位Win7出现 delphi 2007 assertion failure thread32.cpp 的解决办法
查看>>
STM32的TAMPER-RTC管脚作为Tamper的使用[转]
查看>>
[记]一个逐步“优化”的范例程序
查看>>
2012-01-09_2
查看>>
Visual Studio 2015 开发MVC4出现错误
查看>>
MongoDB 学习笔记之 批处理
查看>>
orcale 之 存储过程
查看>>
A程序是B程序的输入
查看>>
java基础数组(带基础排序法)
查看>>
[20180316]理解db file parallel read等待事件.txt
查看>>
文件系统02 - 零基础入门学习Delphi35
查看>>
8.转型
查看>>
php类中调用array_walk()函数
查看>>
在线一键生成安卓证书keystore文件
查看>>
GITHUB(2.1-2.5)Git的导入
查看>>
判断奇偶
查看>>
android NDK 编译hellojni 例子文件
查看>>
os模块
查看>>