10931 - Parity

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

Moderator: Board moderators

scidong
New poster
Posts: 45
Joined: Sat Jan 21, 2006 12:55 pm
Location: the four-dimensional world

10931 - Parity

Post by scidong » Tue Mar 07, 2006 1:41 pm

Code: Select all

#include<iostream.h>
#include<string.h>
char a[30];
void dtob(int c){
	int i=0;
	while(c){
		a[i]=c%2;
		c=c/2;
		i++;
	}
}

void main(){
	int c,i,p,cn;
	while(cin >> c){
		if(c==0) break;
		if(c==1) cout << "The parity of 1 is 1 (mod 2)." << endl;
		else{
			dtob(c);
			cout << "The parity of ";
			cn=0;
			for(i = 0; i<30; i++){
				if(a[i] == 1){
					p=i;
					cn++;
				}
			}for(i = p; i>=0; i--) cout << int(a[i]);
			cout << " is " << cn << " (mod 2)." << endl;
		}
	}
}
plz help me...
All living things are amazing thing.
一八???

jjtse
Learning poster
Posts: 80
Joined: Mon Aug 22, 2005 7:32 pm
Location: Nevada, US
Contact:

Post by jjtse » Sun May 07, 2006 11:46 pm

hey scidong,

your posts was a long time ago. I recently solved that problem. If you still need help on that problem, post a reply and I'll help you out. Otherwise I'll assume you already figured it out.

scidong
New poster
Posts: 45
Joined: Sat Jan 21, 2006 12:55 pm
Location: the four-dimensional world

Post by scidong » Mon May 15, 2006 10:00 am

Uh... plz help me sir.
All living things are amazing thing.
一八???

jjtse
Learning poster
Posts: 80
Joined: Mon Aug 22, 2005 7:32 pm
Location: Nevada, US
Contact:

Post by jjtse » Tue May 16, 2006 7:29 am

This is a simple problem actually. Since


1 ≤ I ≤ 2147483647


integers will not be big enough to hold that.
Take a look at this: http://home.att.net/~jackklein/c/inttypes.html#limits

I guess for a quick fix, just replace all your "int" types with "long" or "unsigned long" Either of those will work.

ayon
Experienced poster
Posts: 161
Joined: Tue Oct 25, 2005 8:38 pm
Location: buet, dhaka, bangladesh

Post by ayon » Tue May 16, 2006 8:06 am

int is enough for this problem, and for uva judge there is NO difference between int and long, my ac program everywhere used int data type.
ishtiak zaman
----------------
the world is nothing but a good program, and we are all some instances of the program

User avatar
Martin Macko
A great helper
Posts: 481
Joined: Sun Jun 19, 2005 1:18 am
Location: European Union (Slovak Republic)

Re: 10931! what`s wrong? WA!

Post by Martin Macko » Sun May 21, 2006 12:04 am

scidong wrote:plz help me...
Try the following input:

Code: Select all

2147483647
0
The correct answer is:

Code: Select all

The parity of 1111111111111111111111111111111 is 31 (mod 2).
However, your solution says that the parity of 2147483647 is 30.

hosnayen
New poster
Posts: 4
Joined: Mon Dec 28, 2009 8:41 pm

Re: 10931 - Parity [b] why wrong answer? can any1 tell me..

Post by hosnayen » Fri Aug 20, 2010 1:37 pm

Code: Select all

/*
	Author : Hosnayen Alam Siddiquee.
	University of Science & Technology Chittagong (USTC)
	CSE- 11th Batch
	E-mail: hosnayen_alam@yahoo.com
	Bangladesh	                          Date:	20/08/2010
*/

#include<cstdio>
#include<sstream>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<algorithm>
#include<set>
#include<queue>
#include<stack>
#include<list>
#include<iostream>
#include<fstream>
#include<numeric>
#include<string>
#include<vector>
#include<cstring>
#include<map>
#include<iterator>

using namespace std;

int main()
{
 long I,count=0;

 while(scanf("%ld",&I)==1 && I){
   long res[100];
    long i=0,j=0;
	 while(I>0){
		   res[i]=I%2;
		   j++;
		   if(res[i]==1){
			count++;
		   }
        I=I/2;
      i++;
	 }

     printf("The parity of ");

	 if(res[0]==0){
          reverse(res,res+j);
	 }
	 
	 for(i=0;i<j;i++){
        printf("%ld",res[i]);
	 }
	 printf(" is %ld (mod 2).\n",count);
	 count=0;
	 
 }

return 0;
}

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

Re: 10931 - Parity

Post by helloneo » Sat Aug 21, 2010 4:42 pm

Try this

Code: Select all

11
My output

Code: Select all

The parity of 1011 is 3 (mod 2).

Remove your code after AC :)

taufique
New poster
Posts: 3
Joined: Fri Nov 05, 2010 10:19 am

Re: 10931 - Parity

Post by taufique » Sat Nov 13, 2010 8:23 pm

I just can't understand what can be the reason of WA here.
Its a simple code but WA. :-(
anybody can help me??
Here is my code

#include <iostream>
#include <cstring>
using namespace std;

int c;

char* calculate(int n)
{
int i = 0,j;
c = 0;
char bin[50];

if(n%2) c++;
bin[i++] = (n%2)+48;
n /= 2;

while(n)
{
if(n%2) c++;
bin[i++] = (n%2)+48;
n /= 2;
}
bin = '\0';

for(i = 0,j = strlen(bin)-1 ; i < j ; i++,j-- )
{
char temp = bin;
bin = bin[j];
bin[j] = temp;
}

return bin;
}

int main()
{
int n;
char bin_n[50];
while(cin >> n)
{
if(n == 0) break;
else
{
strcpy(bin_n,calculate(n));
cout << "The parity of " << bin_n << " is " << c << " (mod 2)." << endl;
}
}
return 0;
}

Jehad Uddin
Learning poster
Posts: 74
Joined: Fri May 08, 2009 5:16 pm

Re: 10931 - Parity

Post by Jehad Uddin » Sun Nov 14, 2010 6:17 am

hello ghana, ;)
change ur pointer calculation

Code: Select all

char bin[50];
void calculate(int n)
{
use code tags while posting code

DD
Experienced poster
Posts: 145
Joined: Thu Aug 14, 2003 8:42 am
Location: Mountain View, California
Contact:

Re: 10931 - Parity [b] why wrong answer? can any1 tell me..

Post by DD » Mon Mar 14, 2011 1:02 am

hosnayen wrote:

Code: Select all

/*
	Author : Hosnayen Alam Siddiquee.
	University of Science & Technology Chittagong (USTC)
	CSE- 11th Batch
	E-mail: hosnayen_alam@yahoo.com
	Bangladesh	                          Date:	20/08/2010
*/

#include<cstdio>
#include<sstream>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<algorithm>
#include<set>
#include<queue>
#include<stack>
#include<list>
#include<iostream>
#include<fstream>
#include<numeric>
#include<string>
#include<vector>
#include<cstring>
#include<map>
#include<iterator>

using namespace std;

int main()
{
 long I,count=0;

 while(scanf("%ld",&I)==1 && I){
   long res[100];
    long i=0,j=0;
	 while(I>0){
		   res[i]=I%2;
		   j++;
		   if(res[i]==1){
			count++;
		   }
        I=I/2;
      i++;
	 }

     printf("The parity of ");

	 if(res[0]==0){
          reverse(res,res+j);
	 }
	 
	 for(i=0;i<j;i++){
        printf("%ld",res[i]);
	 }
	 printf(" is %ld (mod 2).\n",count);
	 count=0;
	 
 }

return 0;
}
Your code has some problems when reversing. You may try to print res in reverse order.
Have you ever...
  • Wanted to work at best companies?
  • Struggled with interview problems that could be solved in 15 minutes?
  • Wished you could study real-world problems?
If so, you need to read Elements of Programming Interviews.

NaiLuJ
New poster
Posts: 2
Joined: Wed Mar 21, 2012 8:08 am

Re: 10931 - Parity

Post by NaiLuJ » Wed Mar 21, 2012 8:13 am

can someone help me? i don't know why i get WA for this
thanks before :D

Code: Select all

   
code removed , got AC thansk to brian :)
Last edited by NaiLuJ on Thu Mar 22, 2012 10:21 am, edited 1 time in total.

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10931 - Parity

Post by brianfry713 » Thu Mar 22, 2012 12:09 am

change n from long to int.
Check input and AC output for thousands of problems on uDebug!

NaiLuJ
New poster
Posts: 2
Joined: Wed Mar 21, 2012 8:08 am

Re: 10931 - Parity

Post by NaiLuJ » Thu Mar 22, 2012 10:20 am

thanks brian, i got AC :D

cse.mehedi
New poster
Posts: 36
Joined: Sun Mar 18, 2012 8:18 am

10931 i can't understand why WA!!!

Post by cse.mehedi » Wed Apr 11, 2012 10:39 pm

Any one help me plz!!! :(

Code: Select all

AC
Last edited by cse.mehedi on Thu Apr 12, 2012 12:04 am, edited 1 time in total.

Post Reply

Return to “Volume 109 (10900-10999)”