#38424: #7: 11% RE (SIGABRT)


abc092049061299@gmail.com (ㄓㄓㄓ)

學校 : 不指定學校
編號 : 181570
來源 : [118.168.39.24]
最後登入時間 :
2023-12-23 16:42:25
a021. 大數運算 | From: [140.138.242.111] | 發表日期 : 2023-11-21 22:01

#include <iostream>
#include <cstdlib>
#include <string>
#include <algorithm>
using namespace std;
string add(string str1, string str2) {
string output;
int length = str1.length() - str2.length();
if (length > 0) {
str2.insert(0, length, '0');
}
else {
str1.insert(0, -length, '0');
}
 
int advance = 0;
for (int i = str1.length() - 1; i >= 0; i--) {
int temp = (str1[i] - '0') + (str2[i] - '0');
if (temp + advance >= 10) {
char ch = (temp + advance) % 10 +'0';
output.insert(0, 1, ch);
advance = 1;
}
else {
char ch = temp + advance + '0';
output.insert(0, 1, ch);
advance = 0;
}
}
if (advance == 1)
output.insert(0, 1, '1');
return output;
 
}
 
string reduce(string str1,string str2) {
string output;
if (str1 == str2) 
return "0";
int length = str1.length() - str2.length();
if (length > 0) {
str2.insert(0, length, '0');
}
else {
str1.insert(0, -length, '0');
}
bool is_negtive = false;
for (int i = str1.length() - 1; i >= 0; i--) {
if (str1[i] > str2[i]) {
break;
}
if (str1[i] < str2[i]) {
swap(str1, str2);
is_negtive = true;
break;
}
}
 
 
for (int i = str1.length() - 1; i >= 0; i--) {
if (str1[i] < str2[i]) {
str1[i - 1]--;
char ch = str1[i] - str2[i] + 10 + '0';
output.insert(0, 1, ch);
}
else {
char ch = str1[i] - str2[i] + '0';
output.insert(0, 1, ch);
 
}
}
if (output[0] == '0')
output.erase(0, 1);
if (is_negtive)
output.insert(0, 1, '-');
return output;
 
}
 
string mul(string str1, string str2) {
string output = "0";
for (int i = 0; i < str2.length(); i++) {
string temp = "0";
for (int j = 0; j < str2[str2.length() - 1 - i] - '0'; j++) {
temp = add(temp, str1);
}
for (int k = 0; k < i; k++) {
temp = temp + '0';
}
output = add(temp, output);
}
return output;
}
 
 
 
 
int main()
 {
string input;
getline(cin, input);
string str1;
string str2;
char operation;
for (int i = 0; i < input.length(); i++) {
if (input[i] == ' ') {
str1 = input.substr(0, i);
operation = input[i + 1];
str2 = input.substr(i + 3);
break;
}
}
 
 
string output;
if (operation == '+') {
output = add(str1, str2);
}
else if (operation == '-') {
output = reduce(str1, str2);
}
else if (operation == '*') {
output = mul(str1, str2);
}
else if (operation == '/'){
bool str2_big = false;
if (str2.length() > str1.length()) {
cout << " 0";
str2_big = true;
}
else if (str2.length() == str1.length() ){
for (int i = 0; i < str1.length(); i++) {
if (str2[i] < str1[i]) {
break;
}
else if (str2[i] > str1[i]) {
str2_big = true;
cout << "0";
break;
}
}
}
 
 
if (!str2_big) {
string temp;
temp = str1.substr(0, str2.length());
for (int i = 0; i < str1.length() - str2.length() + 1; i++) {
int ans;
for (int j = 1; j <= 9; j++) {
string a = reduce(temp, mul(to_string(j), str2));
if (a[0] == '-'){
ans = j - 1;
break;
}
else if (a[0] == '0') {
ans = j;
break;
}
}
output = output + (char)(ans + '0');
temp = reduce(temp, mul(to_string(ans), str2));
if ((i + 1) < str1.length() - str2.length() + 1) {
temp = temp + str1[str2.length() + i];
}
}
 
 
if (output[0] == '0')
output.erase(0, 1);
 
}
}
 
 
cout << output;
return 0;
}
 
 
系統呼叫了 abort 函式!
double free or corruption (out)
Aborted (core dumped
 
 
 
ZeroJudge Forum