10469 - To Carry or not to Carry
Moderator: Board moderators
10469 - To Carry or not to Carry
Problem 10469 is quite simple, but how would judge input the test number?
should i keep on reading until it terminate my problem anyway?
it hasn't been announced as a "multiple input"
should i keep on reading until it terminate my problem anyway?
it hasn't been announced as a "multiple input"
The problem statement clearly states,
[c]
int main() {
while (scanf("%d %d",&a,&b)==2) {
/* do whatever is needed */
}
return 0;
}
[/c]
If you're writing the solution in C, you could use:Input ends at EOF.
[c]
int main() {
while (scanf("%d %d",&a,&b)==2) {
/* do whatever is needed */
}
return 0;
}
[/c]
K M Hasan
http://www.cs.umanitoba.ca/~kmhasan/
http://www.cs.umanitoba.ca/~kmhasan/
hiiiiiiiii
just print...
(a^b)
get accccccccccccccc
thx.
(a^b)
get accccccccccccccc
thx.
-
- New poster
- Posts: 6
- Joined: Fri Jun 01, 2007 7:20 am
Re: hiiiiiiiii
OMG, I wasted my time to convert a and b to binary number and XoR them. With my previous code, I got WA. But with only one line code, I got AC. Thank u very much.nikhil wrote:just print...
(a^b)
get accccccccccccccc
thx.
I keep getting wrong answer
My code is in C++, is really simple. It works well in my computer but maybe I should input some critical inputs. I just keep getting WA and I have no idea why.
Here is my code:
/*
problem# 10469
*/
#include <iostream>
....
.....deleted
.....
.....
int main ()
{
while( !cin.eof() )
{
.....
.....
..deleted...
................
cout << result << endl;
}
return 0;
}
Thanks
Here is my code:
/*
problem# 10469
*/
#include <iostream>
....
.....deleted
.....
.....
int main ()
{
while( !cin.eof() )
{
.....
.....
..deleted...
................
cout << result << endl;
}
return 0;
}
Thanks
Last edited by mario on Wed Jul 18, 2007 4:37 am, edited 1 time in total.
>>mario
I generate some random test case & compare ur output with my AC code output. ur code generate one extra output.Problem in ur input terminating condition.
instead of using this u can use
Hope this helps.
I generate some random test case & compare ur output with my AC code output. ur code generate one extra output.Problem in ur input terminating condition.
Code: Select all
while( !cin.eof() )
Code: Select all
while(cin >> x >> y)
Re: 10469 - To Carry or not to Carry
I'm having WA for this problem but don't know why. My answers seem to be alright. I've used a^b for output and
while(scanf("%d %d", &a, &b);
what should i do?
![:(](./images/smilies/icon_frown.gif)
while(scanf("%d %d", &a, &b);
what should i do?
![:(](./images/smilies/icon_frown.gif)
Tarif Ezaz
Undergraduate student
North South University
12 Kamal Ataturk Avenue, Dhaka
Undergraduate student
North South University
12 Kamal Ataturk Avenue, Dhaka
Re: 10469 - To Carry or not to Carry
Make sure you use unsigned integers. Which also means you need to use "%u" instead of "%d" in both scanf and printf.
Edit: Fixed mistake.
Edit: Fixed mistake.
Runtime errors in Pascal are reported as Wrong Answers by the online judge. Be careful.
Are you dreaming right now?
http://www.dreamviews.com
Are you dreaming right now?
http://www.dreamviews.com
-
- New poster
- Posts: 24
- Joined: Fri Oct 24, 2008 8:37 pm
- Location: CUET, Chittagong, Bangladesh
- Contact:
Re: 10469 - To Carry or not to Carry
I got runtime error with this code. re is common for me. I'm newbie. can someone help me to find the mistake in my code.
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
int main()
{
long long f,f1,f2,j,i,n,b,l1,l2,l,sum,mul,k,l3;
char str[100],str1[100],res[100],h[100],ch,ch1;
while(1)
{
sum=0;
for(j=0; j<2; j++)
{
if(scanf("%lld",&n)==EOF)
exit(1);
for(i=0; ; i++)
{
if(n==0)
{
str='0';
str[i+1]='\0';
break;
}
if(n==1)
{
str='1';
str[i+1]='\0';
break;
}
if(n%2==0)
str='0';
else
{
str='1';
}
n=n/2;
}
if(j==0)
{
strcpy(str1,str);
str[0]='\0';
}
}
l1=strlen(str);
l2=strlen(str1);
if(l1>l2)
b=l1;
else
b=l2;
f=1;
f1=1;
f2=0;
k=0;
for(i=0; i<b; i++)
{
ch=str-48;
ch1=str1-48;
if(ch<0 || ch>1 || f==0)
{
ch=0;
f=0;
}
else if(ch1<0 || ch1>1 || f1==0)
{
ch1=0;
f1=0;
}
res[k++]=ch^ch1+48;
}
res[k]='\0';
l3=strlen(res);
k=0;
for(i=0; ; i++)
{
l3--;
if(res=='\0')
break;
if(res[l3]=='0' && f2==0)
continue;
f2=1;
h[k++]=res[l3];
}
h[k]='\0';
l=strlen(h)-1;
for(i=0; ;i++)
{
if(h=='\0')
break;
mul=(h[l--]-48)*pow(2,i);
sum+=mul;
}
str[0]='\0';
str1[0]='\0';
h[0]='\0';
res[0]='\0';
printf("%lld\n",sum);
}
return 0;
}
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
int main()
{
long long f,f1,f2,j,i,n,b,l1,l2,l,sum,mul,k,l3;
char str[100],str1[100],res[100],h[100],ch,ch1;
while(1)
{
sum=0;
for(j=0; j<2; j++)
{
if(scanf("%lld",&n)==EOF)
exit(1);
for(i=0; ; i++)
{
if(n==0)
{
str='0';
str[i+1]='\0';
break;
}
if(n==1)
{
str='1';
str[i+1]='\0';
break;
}
if(n%2==0)
str='0';
else
{
str='1';
}
n=n/2;
}
if(j==0)
{
strcpy(str1,str);
str[0]='\0';
}
}
l1=strlen(str);
l2=strlen(str1);
if(l1>l2)
b=l1;
else
b=l2;
f=1;
f1=1;
f2=0;
k=0;
for(i=0; i<b; i++)
{
ch=str-48;
ch1=str1-48;
if(ch<0 || ch>1 || f==0)
{
ch=0;
f=0;
}
else if(ch1<0 || ch1>1 || f1==0)
{
ch1=0;
f1=0;
}
res[k++]=ch^ch1+48;
}
res[k]='\0';
l3=strlen(res);
k=0;
for(i=0; ; i++)
{
l3--;
if(res=='\0')
break;
if(res[l3]=='0' && f2==0)
continue;
f2=1;
h[k++]=res[l3];
}
h[k]='\0';
l=strlen(h)-1;
for(i=0; ;i++)
{
if(h=='\0')
break;
mul=(h[l--]-48)*pow(2,i);
sum+=mul;
}
str[0]='\0';
str1[0]='\0';
h[0]='\0';
res[0]='\0';
printf("%lld\n",sum);
}
return 0;
}
Re: 10469 - To Carry or not to Carry
You are over complicating the problem.
Study the bitwise XOR operator.
Study the bitwise XOR operator.
Runtime errors in Pascal are reported as Wrong Answers by the online judge. Be careful.
Are you dreaming right now?
http://www.dreamviews.com
Are you dreaming right now?
http://www.dreamviews.com
-
- New poster
- Posts: 3
- Joined: Sat Dec 08, 2012 5:16 pm
10469 - To Carry or not to Carry
please can anyone tell me whats wrong with this code ?
thanks in advance .
Code: Select all
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
string convert (unsigned long long int n)
{
string s;
stringstream out;
out << n;
s = out.str();
return s;
}
int bit (unsigned long long int n)
{
unsigned long long int rem=0,sum=0,i=1;
while (n>0)
{
rem=n%2;
sum+=(i*rem);
n/=2;
i=i*10;
}
return sum;
}
int b_d(string w)
{
unsigned long long int result = 0, pow = 1;
for ( int i = w.length() - 1; i >= 0; --i, pow <<= 1 ) //pow=pow<<1; shifting pow to the left
result += (w[i] - '0') * pow;
return result;
}
int main()
{
string w1,w2,k,w3,w4;
unsigned long long int n1,n2,l1,l2,l3;
while(cin>>n1>>n2)
{
w1=w2=w3=w4=k="";
l1=l2=0;
n1=bit(n1);
n2=bit(n2);
cout<<n1<<" "<<n2<<endl;
w1=convert(n1);
w2=convert(n2);
l1=w1.length();
l2=w2.length();
if (l2>l1)
for (int i=l1;i<l2;i++)
w3+="0";
else
for (int i=l2;i<l1;i++)
w4+="0";
w3+=w1;
w4+=w2;
for (int i=0;i<w3.length();i++)
{
if ( (w3[i]=='1' && w4[i]=='0') || (w3[i]=='0' && w4[i]=='1') )
k+="1";
else k+="0";
}
cout<<w3<<" "<<w4<<" "<<k<<endl;
cout<<b_d(k)<<endl;
}
return 0;
}