10469 - To Carry or not to Carry

All about problems in Volume 104. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

BestIvan
New poster
Posts: 3
Joined: Thu Apr 24, 2003 6:18 am

10469 - To Carry or not to Carry

Post by BestIvan »

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"

kmhasan
Problemsetter
Posts: 107
Joined: Fri Oct 26, 2001 2:00 am
Location: Canada
Contact:

Post by kmhasan »

The problem statement clearly states,
Input ends at EOF.
If you're writing the solution in C, you could use:
[c]
int main() {
while (scanf("%d %d",&a,&b)==2) {
/* do whatever is needed */
}
return 0;
}
[/c]

BestIvan
New poster
Posts: 3
Joined: Thu Apr 24, 2003 6:18 am

Thanks

Post by BestIvan »

It's alright, thank you very much
Actually i've just tried to use C++ and stream I/O instead of C
so i was not sure how to deal with EOF then
:lol:

Adil
Learning poster
Posts: 57
Joined: Sun Sep 29, 2002 12:00 pm
Location: in front of the monitor :-)
Contact:

Post by Adil »

for C++ and stream I/O, i think you can use:
[cpp]while(cin >> a >> b)
{
...
}[/cpp]

nikhil
New poster
Posts: 11
Joined: Wed Oct 08, 2003 1:37 pm

hiiiiiiiii

Post by nikhil »

just print...
(a^b)
get accccccccccccccc
thx.

Deny Sutani
New poster
Posts: 6
Joined: Fri Jun 01, 2007 7:20 am

Re: hiiiiiiiii

Post by Deny Sutani »

nikhil wrote:just print...
(a^b)
get accccccccccccccc
thx.
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.

mario
New poster
Posts: 5
Joined: Sat Jul 07, 2007 4:46 pm

I keep getting wrong answer

Post by mario »

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
Last edited by mario on Wed Jul 18, 2007 4:37 am, edited 1 time in total.

mmonish
Experienced poster
Posts: 109
Joined: Sun Mar 11, 2007 2:55 pm
Location: SUST

Post by mmonish »

>>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.

Code: Select all

while( !cin.eof() )
instead of using this u can use

Code: Select all

while(cin >> x >> y)
Hope this helps.

mario
New poster
Posts: 5
Joined: Sat Jul 07, 2007 4:46 pm

Post by mario »

Thanks, that was all. I got AC when I changed to cin >> x >> y.

helloneo
Guru
Posts: 516
Joined: Mon Jul 04, 2005 6:30 am
Location: Seoul, Korea

Post by helloneo »

If you got AC, remove your code plz.. :-)

Tarif
New poster
Posts: 3
Joined: Wed Mar 26, 2008 6:44 am
Location: Bangladesh
Contact:

Re: 10469 - To Carry or not to Carry

Post by Tarif »

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?

:(
Tarif Ezaz
Undergraduate student
North South University
12 Kamal Ataturk Avenue, Dhaka

andmej
Experienced poster
Posts: 158
Joined: Sun Feb 04, 2007 7:45 pm
Location: Medellin, Colombia

Re: 10469 - To Carry or not to Carry

Post by andmej »

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.
Runtime errors in Pascal are reported as Wrong Answers by the online judge. Be careful.

Are you dreaming right now?
http://www.dreamviews.com

aliahmed
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

Post by aliahmed »

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;
}

andmej
Experienced poster
Posts: 158
Joined: Sun Feb 04, 2007 7:45 pm
Location: Medellin, Colombia

Re: 10469 - To Carry or not to Carry

Post by andmej »

You are over complicating the problem.

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

omarking05
New poster
Posts: 3
Joined: Sat Dec 08, 2012 5:16 pm

10469 - To Carry or not to Carry

Post by omarking05 »

please can anyone tell me whats wrong with this code ?

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;
}
thanks in advance .

Post Reply

Return to “Volume 104 (10400-10499)”