PAT1065 A+B and C (64bit) (20)
来源:互联网 发布:java文本框不可编辑 编辑:程序博客网 时间:2024/06/15 09:42
1065. A+B and C (64bit) (20)
时间限制
100 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
HOU, Qiming
Given 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:31 2 32 3 49223372036854775807 -9223372036854775808 0Sample Output:
Case #1: falseCase #2: trueCase #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 > b int 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 <= b if (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");}
0 0
- pat1065 A+B and C (64bit)
- PAT1065 A+B and C (64bit) (20)
- A+B and C (64bit) (20)
- PAT A 1065. A+B and C (64bit) (20)
- PAT-A 1065. A+B and C (64bit) (20)
- PAT-A-1065. A+B and C (64bit) (20)
- 1065. A+B and C (64bit)
- 1065. A+B and C (64bit)
- 1065. A+B and C (64bit)
- 1065.A+B and C (64bit)
- PAT A+B and C (64bit)
- 1065. A+B and C (64bit)
- 1065 A+B and C(64bit)
- 1065. A+B and C (64bit)
- 1065. A+B and C (64bit)
- 1065. A+B and C (64bit)
- 1065. A+B and C (64bit)
- 1065. A+B and C (64bit)
- PAT (Basic Level) Practise (中文)---1004 成绩排名 (20)
- LeetCode107. Binary Tree Level Order Traversal II题解
- 周志华《机器学习》课后习题解答系列(四):Ch3.4
- 93. 中序遍历序列
- Activity的四种启动模式
- PAT1065 A+B and C (64bit) (20)
- 一步一步搞清排序之冒泡排序(JAVA实现)
- 简单介绍支持向量机(SVM)与核函数
- 初探AC自动机
- 漫步数学分析番外六(下)
- centos7 搭建zookeeper集群
- MOM
- centos服务器上(腾讯云)安装oracleXE
- Merge into 语句用法学习