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

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:
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 > 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");
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章