Trans bought a calculator at creatnx's store. Unfortunately, it is fake. It has many bugs. One of them is adding two numbers without carrying. Example expression: 12 + 9 will have result 11 in his calculator. Given an expression in the form a + b, please output result from that calculator. Constraints: 1 ≤ T ≤ 100 1 ≤ a, b ≤ 109
My code pass the test cases where 1 ≤ a, b ≤ 9 But failed for the original contraints. Could not find the case where my code giving wrong answer.
My Code
#include <iostream>
#include<string>
#define ll long long int
using namespace std;
int main()
{
ll t;
cin>>t;
while(t--){
string a, b;
cin>>a>>b;
ll max_last = max(a.size(), b.size());
ll min_last = min(a.size(), b.size());
ll result[max_last];
ll i=max_last-1;
for(ll j= min_last-1; j>=0; i--, j--){
ll sum = a[i]-'0' + b[j]-'0';
result[i] = sum ;
}
while(i>=0){
result[i] = a[i]-'0';
i--;
}
ll sum = 0;
for(ll i=0; i<max_last; i++){a
sum = sum*10 + result[i]%10;
}
cout<<sum<<"\n";
}
return 0;
}
Tester's Code
#include <bits/stdc++.h>
using namespace std;
signed main() {
int t;
cin >> t;
assert(1 <= t && t <= 100);
while(t--) {
int a, b;
cin >> a >> b;
assert(1 <= a && a <= 1'000'000'000);
assert(1 <= b && b <= 1'000'000'000);
vector<int> A, B;
while(a) {
A.push_back(a % 10);
a /= 10;
}
while(b) {
B.push_back(b % 10);
b /= 10;
}
while(A.size() < B.size()) A.push_back(0);
while(B.size() < A.size()) B.push_back(0);
for(int i = 0; i < A.size(); i++) {
A[i] += B[i];
}
int ans = 0;
reverse(begin(A), end(A));
for(auto it: A) {
ans = ans * 10 + it % 10;
}
cout << ans << endl;
}
}
Aucun commentaire:
Enregistrer un commentaire