您的位置:首页 > 其它

PAT1065 A+B and C (64bit) (20)

2017-03-20 19:32 330 查看

1065. A+B and C (64bit) (20)

时间限制100 ms内存限制65536 kB代码长度限制16000 B判题程序Standard作者HOU, QimingGiven three integers A, B and C in [-263, 263], you are supposed to tell whether A+B > C.Input Specification:The first line of the input gives the positive number of test cases, T (<=10). Then T test cases follow, each consists of a single line containing three integers A, B and C, separated by single spaces.Output Specification:For each test case, output in one line "Case #X: true" if A+B>C, or "Case #X: false" otherwise, where X is the case number (starting from 1).Sample Input:
3
1 2 3
2 3 4
9223372036854775807 -9223372036854775808 0
Sample Output:
Case #1: false
Case #2: true
Case #3: false
题目:大整数加法+字符串比较大小
解法:按位操作
启示:string 取最后,边取边删除,尾部对齐
Code:
#include <iostream>#include <string>#include <stdio.h>std::string DelZero(std::string result) {int start_pos = -1;for (int i = 0; i < result.size(); i++) {if (result[i] != '0') {start_pos = i;break;}}if (start_pos == -1) {return "0";}else {result.erase(0, start_pos);return result;}}bool Compare(std::string a, std::string b) {if (a[0] == '-' && b[0] == '-') {a.erase(0, 1);b.erase(0, 1);return Compare(b, a);}else if (a[0] == '-' && b[0] != '-') {return false;}else if (b[0] == '-' && a[0] != '-') {return true;}else {if (a.size() > b.size()) {return true;}else if (a.size() < b.size()) {return false;}else {for (int i = a.size() - 1; i > 0; i--) {if (a[i] - '0' < b[i] - '0') {return false;}else if (a[i] - '0' > b[i] - '0') {return true;}}if (a[0] - '0' > b[0] - '0') {return true;}else {return false;}}}}std::string Add(std::string a, std::string b) {int size = a.size() > b.size() ? b.size() : a.size();std::string long_str = a.size() > b.size() ? a : b;std::string result = "";int mark = 0;int gap = a.size() > b.size() ? a.size() - size : b.size() - size;for (int i = 0; i < size; i++) {int num = (a.back() - '0') + (b.back() - '0') + mark;if (num >= 10) {mark = 1;char r[10];snprintf(r, sizeof(r), "%d", abs(num));result.insert(0, r);}else {mark = 0;char r[10];snprintf(r, sizeof(r), "%d", abs(num));result.insert(0, r);}a.erase(a.size() - 1, 1);b.erase(b.size() - 1, 1);long_str.erase(long_str.size() - 1, 1);}for (int i = 0; i < gap; i++) {int num = (long_str.back() - '0') + mark;if (num >= 10) {mark = 1;char r[10];snprintf(r, sizeof(r), "%d", abs(num));result.insert(0, r);}else {mark = 0;char r[10];snprintf(r, sizeof(r), "%d", abs(num));result.insert(0, r);}long_str.erase(long_str.size() - 1, 1);}return result;}std::string Minus(std::string a, std::string b) {std::string result = "";std::string long_str = a.size() > b.size() ? a : b;if (Compare(a, b)) {  // a > bint mark = 0;int size = a.size() > b.size() ? b.size() : a.size();int gap = a.size() > b.size() ? a.size() - size : b.size() - size;for (int i = 0; i < size; i++) {int num = (a.back() - '0') - (b.back() - '0') - mark;if (num < 0) {mark = 1;char r[10];snprintf(r, sizeof(r), "%d", abs(num));result.insert(0, r);}else {mark = 0;char r[10];snprintf(r, sizeof(r), "%d", abs(num));result.insert(0, r);}a.erase(a.size()-1, 1);b.erase(b.size() - 1, 1);long_str.erase(long_str.size() - 1, 1);}for (int i = 0; i < gap; i++) {int num = (long_str.back() - '0') - mark;if (num < 0) {mark = 1;char r[10];snprintf(r, sizeof(r), "%d", abs(num));result.insert(0, r);}else {mark = 0;char r[10];snprintf(r, sizeof(r), "%d", abs(num));result.insert(0, r);}long_str.erase(long_str.size() - 1, 1);}return result;}else {  // a <= bif (a == b) return "0";std::string result = Minus(b, a);result.insert(0, "-");return result;}}std::string Handle(std::string a, std::string b) {if (a[0] =='-' && b[0] == '-') {a.erase(0, 1);b.erase(0, 1);std::string result = Add(a, b);result = DelZero(result);result.insert(0, "-");return result;}else if (a[0] == '-' && b[0] != '-') {a.erase(0, 1);std::string result = Minus(b, a);result = DelZero(result);return result;}else if (b[0] == '-' && a[0] != '-') {b.erase(0, 1);std::string result = Minus(a, b);result = DelZero(result);return result;}else {std::string result = Add(a, b);// std::cout << result << "\n";result = DelZero(result);// std::cout << result << "\n";return result;}}int main() {int n;std::cin >> n;for (int i = 0; i < n; i++) {std::string a, b, c;std::cin >> a;std::cin >> b;std::cin >> c;std::string d = Handle(a, b);// printf("%s\n", d.c_str());if (Compare(d, c)) {printf("Case #%d: true\n", i+1);}else {printf("Case #%d: false\n", i+1);}}system("pause");}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: