Problem 1007 - 做一名正气的西电人

简介: Description   一天,wm和zyf想比比谁比较正气,但正气这种东西无法量化难以比较,为此,他们想出了一个方法,两人各写一个数字,然后转化为二进制,谁的数字中二进制1多谁就比较正气! Input   输入包含多组数据,EOF结束。
Description

  一天,wm和zyf想比比谁比较正气,但正气这种东西无法量化难以比较,为此,他们想出了一个方法,两人各写一个数字,然后转化为二进制,谁的数字中二进制1多谁就比较正气!

Input
  输入包含多组数据,EOF结束。 
  每组数据包含两行,代表两个非负整数a,b(0<=a,b<10^100,不含前导0),a为wm写的数字,b为zyf写的数字。
Output
  每组数据输出一行,输出正气的西电人名字"wm"或"zyf",如果两人的数字中二进制1一样多就输出"neither"。

Sample Input
15
16
17
18
20
19
Sample Output
wm
neither
zyf
Hint
Source
2010.04内部测试赛(Author: Qinz)

#include <stdio.h>
#include <string.h>


int count(char a[])
{     
    int cur, pre;
    int i = 0, j = 0, count = 0;
    pre = 0;
    int len_a = strlen(a);
    while (j < len_a){
        for(i = j; i < len_a; ++i)
        {
            cur = a[i] - '0';
            a[i] = (pre * 10 + cur)/2 + '0';
            pre = cur%2;
        }
        if(pre == 1)
            count++;
        if(a[j] == '0')
            j++;
        pre = 0;
    }
    return count;
}


int main()
{
    char a[101], b[101], result[101]="";
    int counta, countb;
    while(gets(a) != NULL && gets(b) != NULL)
    {
        counta = count(a);
        countb = count(b);
        if(counta > countb)
            strcat(result, "wm\n");
        else if(counta < countb)
            strcat(result, "zyf\n");
        else
            strcat(result, "neither\n");
    }
    printf("%s", result);
    return 0;
}

#include <stdio.h>
int x, y;
char a[101];
int operate()
{
    int l, i, j, ret = 0;
    for (l = 0; a[l] && a[l] != '\n'; ++l)
        a[l] -= '0';
    for (i = 0, j = --l; i < j; ++i, --j) {
        a[i] ^= a[j];
        a[j] ^= a[i];
        a[i] ^= a[j];
    }
    while (l || a[0]) {
        for (ret += a[0] & 1, i = l, j = 0; i >= 0; --i) {
            j = j * 10 + a[i];
            a[i] = j >> 1;
            j &= 1;
        }
        while (l && !a[l])
            --l;
    }
    return ret;
}
int main()
{
    while (gets(a)) {
        x = operate(a);
        gets(a);
        y = operate(a);
        if (x == y)
            puts("neither");
        else
            if (x > y)
                puts("wm");
            else
                puts("zyf");
    }
    return 0;
}

#include<stdio.h>
#define MAXSIZE 100

void record(char c[],int *zero,int *sum);

int main()
{
    char a[MAXSIZE+1],b[MAXSIZE+1];
    int suma,sumb;
    int zeroa,zerob;

    while (scanf("%s%s",a,b)!=EOF)
    {
        for (zeroa=0,suma=0,zerob=0,sumb=0;a[zeroa]!='\0' || b[zerob]!='\0';)
        {
            record(a,&zeroa,&suma);
            record(b,&zerob,&sumb);
        }
        if (suma == sumb)
            printf("neither\n");
        else
            if (suma > sumb)
                printf("wm\n");
            else
                printf("zyf\n");
    }
    return 0;
}
void record(char c[],int *zero,int *sum)
{
    int i=*zero,x=0,t;

    while (c[i]!='\0')
        {
            t=c[i]-'0'+x*10;
            x=t%2;
            t/=2;
            c[i]=t+'0';
            i++;
        }
    if (1==x)
        (*sum)++;
    while (c[*zero]=='0')
        (*zero)++;
}

#include<stdio.h>
#include<string.h>
char s1[300],s2[300];
int num[300];
int cals(char s[])
{
    int len,i,x,sum=0;
    len=strlen(s);
    for(i=0;i<len;i++)
        num[i]=s[len-i-1]-'0';  //逆序存入int型数组
    while(len!=0)    //数组长度为0时跳出循环
    {
        for(i=len-1;i>0;i--)  //模拟转化过程
        {
            x=num[i]%2;
            num[i]/=2;
            num[i-1]+=10*x;
        }
        sum+=num[0]%2;  //计数
        num[0]/=2;
        for(i=len-1;num[i]==0&&i>=0;i--) len--;  //若高位为0则缩短数组长度,如120->60,长度就缩了1
    }
    return sum;
}
int main()
{
    int len1,len2,i;
    while(scanf("%s%s",s1,s2)!=EOF)
    {
        len1=cals(s1);
        len2=cals(s2);
        printf("%s\n",len1>len2?"wm":len1==len2?"neither":"zyf");  //比较长度后按要求输出
    }
    return 0;
}

目录
相关文章
|
1月前
|
数据挖掘
Divisibility Problem
Divisibility Problem
122 0
|
算法框架/工具
|
人工智能 BI

热门文章

最新文章