try this input:
Code: Select all
0 11
11 0
0 0
Code: Select all
0
11
--
0
0
--
0
11
0
--
00
--
0
Moderator: Board moderators
Code: Select all
0 11
11 0
0 0
Code: Select all
0
11
--
0
0
--
0
11
0
--
00
--
0
Code: Select all
0 11
11 0
11 111
0 0
Code: Select all
0
11
--
0
0
--
0
11
0
--
00
--
0
11
111
---
11
11
11
-----
10101
Code: Select all
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
string binary_sum(string x,string y){
int i,j,c=0,sum,l1=x.length(),l2=y.length();
string s1,s2;
if(l1>l2){
j=l1;l1=l2;l2=j;
s2=x;
s1=y;
}
else{
s2=y;
s1=x;
}
for(i=l1-1,j=l2-1;i>=0;i--,j--){
sum=(s1[i]-'0')+(s2[j]-'0')+c;
if(sum<2){
s2[j]=(char)(sum+'0');
c=0;
}
else{
c=1;
s2[j]=(char)(sum%2+'0');
}
}
while(j>=0&&c>0){
sum=(s2[j]-'0')+c;
if(sum<2){
s2[j]=(char)(sum+'0');
c=0;
}
else{
c=1;
s2[j]='0';
}
j--;
}
if(c>0) s2=(char)('1')+s2;
if(s2.length()==0) return "0";
return s2;
}
int main(){
int i,j,k,l,m;
string s1,s2,s;
bool f=1;
while(cin>>s1>>s2&&(s1[0]!='0'||s2[0]!='0')){
if(f) f=0;
else cout<<endl;
vector<string> v;
k=s1.length();
l=s2.length();
s=string(k,'0');
string ans=string(k,'0');
for(i=l-1;i>=0;i--){
if(s2[i]=='1'){
v.push_back(s1);
ans=binary_sum(ans,s1+string(l-i-1,'0'));
}
else{
v.push_back(s);
}
}
m=ans.length();
j=v.size();
cout<<string(m-k,' ')+s1<<endl;
cout<<string(m-l,' ')+s2<<endl;
s=string(max(l,k),'-');
cout<<string(m-s.length(),' ')+s<<endl;
for(i=0;i<j;i++){
cout<<string(m-v[i].length()-i,' ')<<v[i]<<endl;
}
cout<<string(m,'-')<<endl;
if(ans<"01"){
cout<<string(m-1,' ')+"0"<<endl;
}
else
cout<<ans<<endl;
}
return 0;
}
Code: Select all
S0
11
--
S0
0
--
S0
11
S0
--
00
--
S0
SSS11
SS111
SS---
SSS11
SS11
S11
-----
10101
But none of above worked.(1) Scanner.nextInt()
(2) Scanner.next()
(3) Scanner.nextLine() + String.split(" ")
(4) BufferedReader.readLine() + String.split(" ")
(5) using System.in.read() to read each byte of all input into a StringBuilder
It works for me, two possible problems I can think of:It is really strange that I got lots of 'Runtime error' even if my Java code reads in the input data without doing anything.
Thanks quol !!quol wrote:It works for me, two possible problems I can think of:It is really strange that I got lots of 'Runtime error' even if my Java code reads in the input data without doing anything.
1) As was written earlier in this thread, the input is terminated by EOF, not by 0 0!
2) If you use scanner.nextInt or Integer.parseInt or something similar, be sure to pass the correct radix.
Code: Select all
removed
There is a clarification in this forum about this problem:I thing these clarification is not valid now. I got WA for this clarification. Problem statement is correct now.
- 1. The program will terminate with EOF
2. An empty line after each test case