Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <vector>
- #include <algorithm>
- #include <ctime>
- #include <cstdlib>
- #include <cstdio>
- #include <cmath>
- #include <deque>
- #include <queue>
- #include <string>
- #include <cstring>
- #include <map>
- #include <stack>
- #include <set>
- using namespace std;
- typedef unsigned long long ll;
- const int INF = 2147483647;
- bool compare(vector<int> a, vector<int> b) {
- // Returns true if a >= b
- if (a == b) return true;
- if (a.size() != b.size()) return (a.size() > b.size());
- assert(a.size() == b.size());
- // Compares most significant digit
- if (a[a.size() - 1] != b[b.size() - 1]) return (a[a.size() - 1] > b[b.size() - 1]);
- // Most significant digit same
- // Compare next significant digit
- return compare(vector<int>(a.begin(), a.end() - 1), vector<int>(b.begin(), b.end() - 1));
- }
- vector<int> add(vector<int> a, vector<int> b) {
- if (!compare(a, b)) swap(a, b);
- // Now a >= b
- for (int i = 0; i < b.size(); i ++) {
- // Naive addition
- a[i] += b[i];
- // Deals with carry
- if (a[i] > 9) {
- a[i] -= 10;
- // Ensures there are enough digits
- if (i + 1 < a.size())
- a[i + 1] ++;
- else
- a.push_back(1);
- }
- }
- return a;
- }
- void print(vector<int> a) {
- // Print digit by digit
- for (int i = a.size() - 1; i >= 0; i --) {
- cout << a[i];
- }
- cout << endl;
- }
- vector<int> readInt() {
- // Assuming reading from stdin
- vector<int> a;
- string s;
- cin >> s;
- for (char c : s) {
- // char reads character as it's ASCII code
- // ASCII code of characters '0' to '9' is 48(0x30) to 57(0x39)
- // Subtract 0x30 from ASCII code to get digit value
- a.push_back((int) (c - 48));
- }
- reverse(a.begin(), a.end());
- return a;
- }
- int main() {
- // Code currently only supports *positive integers*
- // 1234 represented as {4, 3, 2, 1}
- // 0 represented as {0}
- vector<int> a = readInt();
- vector<int> b = readInt();
- cout << "a's value: ";
- print(a);
- cout << "b's value: ";
- print(b);
- cout << "Is a > b? " << (compare(a, b) ? "Yes" : "No") << endl;
- cout << "Is b > a? " << (compare(b, a) ? "Yes" : "No") << endl;
- cout << "a + b = ";
- print(add(a, b));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement