## 10931 - Parity

Moderator: Board moderators

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

### 10931 - Parity

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
Contact:
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
Uh... plz help me sir.
All living things are amazing thing.

jjtse
Learning poster
Posts: 80
Joined: Mon Aug 22, 2005 7:32 pm
Contact:
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
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

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!

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

Code: Select all

``````2147483647
0``````

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

Code: Select all

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

#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

Try this

Code: Select all

``11``
My output

Code: Select all

``The parity of 1011 is 3 (mod 2).``

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

### Re: 10931 - Parity

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

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

### Re: 10931 - Parity

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

hosnayen wrote:

Code: Select all

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

#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

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

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

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

thanks brian, i got AC

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

### 10931 i can't understand why WA!!!

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.