Page **1** of **2**

### 10931 - Parity

Posted: **Tue Mar 07, 2006 1:41 pm**

by **scidong**

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

Posted: **Sun May 07, 2006 11:46 pm**

by **jjtse**

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.

Posted: **Mon May 15, 2006 10:00 am**

by **scidong**

Uh... plz help me sir.

Posted: **Tue May 16, 2006 7:29 am**

by **jjtse**

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.

Posted: **Tue May 16, 2006 8:06 am**

by **ayon**

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.

### Re: 10931! what`s wrong? WA!

Posted: **Sun May 21, 2006 12:04 am**

by **Martin Macko**

scidong wrote:plz help me...

Try the following input:

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.

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

Posted: **Fri Aug 20, 2010 1:37 pm**

by **hosnayen**

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

### Re: 10931 - Parity

Posted: **Sat Aug 21, 2010 4:42 pm**

by **helloneo**

Try this

My output

Remove your code after AC

### Re: 10931 - Parity

Posted: **Sat Nov 13, 2010 8:23 pm**

by **taufique**

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;

}

### Re: 10931 - Parity

Posted: **Sun Nov 14, 2010 6:17 am**

by **Jehad Uddin**

hello ghana,

change ur pointer calculation

Code: Select all

```
char bin[50];
void calculate(int n)
{
```

use code tags while posting code

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

Posted: **Mon Mar 14, 2011 1:02 am**

by **DD**

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.

### Re: 10931 - Parity

Posted: **Wed Mar 21, 2012 8:13 am**

by **NaiLuJ**

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 :)
```

### Re: 10931 - Parity

Posted: **Thu Mar 22, 2012 12:09 am**

by **brianfry713**

change n from long to int.

### Re: 10931 - Parity

Posted: **Thu Mar 22, 2012 10:20 am**

by **NaiLuJ**

thanks brian, i got AC

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

Posted: **Wed Apr 11, 2012 10:39 pm**

by **cse.mehedi**

Any one help me plz!!!