[ZJa132][UVA10931] Parity

題目:
http://zerojudge.tw/ShowProblem?problemid=a132
-----------------------------------------------------------------------------------------------------------------

#include<cstdio>
#include<cstdlib>
#include<cstring>
#define N 1000
int stack[N];
int top=-1;
void push(int);
int pop(void);
void printStack(void);
int main(void)
{
int x,y;
while(scanf("%d",&x)!=EOF && x != 0)
{
while(x>=0)
{
y=x%2;
push(y);
x=x/2;

if(x==0)
break;
}
int ans = 0;
for(int i=0;i<=top;i++)
{
if(stack[i] == 1)
ans++;
}
char s[1000]={};
int j = top;
for(int i=0;i<=top;i++)
{
s[i] = stack[j] + 48;
j--;
}
printf("The parity of %s is %d (mod 2).\n",s,ans);
while(top!=-1)
            top=-1;
}
return 0;
}
void push(int y)
{
stack[++top]=y;
}
int pop(void)
{
return stack[top--];
}

留言

這個網誌中的熱門文章

[ZJ]b513: 判斷質數-商競103

[ZJ]d212: 東東爬階梯