c++初心者です。
以下の問題を解いており、自分のパソコン(visual studio 2013)では正常に動作するのですが、
オンラインジャッジに提出するとランタイムエラーというものが出ます。
http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=1511631#1
原因がわかる方がいましたら教えていただきたく存じます。
#include <iostream>
#include <vector>
#include<string>
#include<sstream>
#define CAPACITY 9
namespace stoi{//stoiのエラーを回避するため名前空間で宣言した
int stoi(std::string str){ int ret; std:: stringstream ss; ss << str; ss >> ret; return ret; }
}
class OverDigit{
public:
int num_a[CAPACITY]; int num_b[CAPACITY]; OverDigit(std::string str_a, std::string str_b); void ChangeDigit(std::string str,int num[]); void Add();
};
OverDigit::OverDigit(std::string str_a,std::string str_b){
ChangeDigit(str_a, num_a); ChangeDigit(str_b, num_b);
}
void OverDigit::ChangeDigit(std::string str, int num[]){
std::vector <int> num_str(CAPACITY, 0); int loc = CAPACITY, k = 8; int str_Max, str_rest; str_Max = str.length() / CAPACITY;// str_rest = str.length() % CAPACITY; for (int j = str.length() - CAPACITY; str_Max > 0; j = j-CAPACITY){ num_str[k] = stoi::stoi(str.substr(j, CAPACITY)); loc = k; k--; str_Max--; } //最後に余り分の文字を配列に入れる if(str_rest!=0)num_str[loc - 1] = stoi::stoi(str.substr(0, str_rest)); for (int j = 0; j < CAPACITY; j++){ num[j]=num_str[j]; }
}
//80桁までの足し算
void OverDigit::Add(){
int num_ans[CAPACITY] = {}; int fat,overflow=0,count=0; for (int j = CAPACITY-1; j >0; j--){ if (num_a[j] + num_b[j]<=999999999){ num_ans[j] = num_a[j] + num_b[j]; } else if (num_a[j] + num_b[j] > 999999999){ fat = (num_a[j] + num_b[j]) - 1000000000; num_ans[j] = fat; num_a[j - 1]++; } } if (num_a[0] + num_b[0] > 99999999){ overflow = 1; } else{ num_ans[0]= num_a[0] + num_b[0]; } if (overflow == 1){ std::cout << "overflow" << std::endl; } else { for (int j = 0; j < CAPACITY; j++){ if (num_ans[j] != 0){ count++; std::cout << num_ans[j]; } else if (num_ans[j] == 0 && count >= 1){ std::cout << "000000000"; } }std::cout << std::endl; }
};
int main(){
//vector配列を用いた80桁の計算 int N; std::string str_a, str_b; std::cin >> N; std::cin.ignore(); for (int i = 0; i < N; i++){ getline(std::cin, str_a); getline(std::cin, str_b); OverDigit OverDigit_a(str_a,str_b);//オブジェクト OverDigit_a.Add(); } return 0;
}
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。