北邮OJ 1005. 16校赛-Hawei Learning C

来源:互联网 发布:网络广告制作软件 编辑:程序博客网 时间:2024/05/01 18:56

题目描述

    Hawei is learning C programming language recently, but he is so naive that he mistakes the  symbol in C for the ¬ symbol in mathemetics. In math calculation, the answer to ¬4 is the bitwise NOT of (100)2, ie., ¬(4)10=(011)2=(3)10. Thus, ¬4=3. But in C programming, the compiler will explain calculation ans=4 as the bitwise NOT of (00000000000000000000000000000100)2 because 4 is a 32-bit integer, ie., ans=(00000000000000000000000000000100)2=(11111111111111111111111111111011)2 =(5)10.

    Unaware of this slight difference, Hawei checked for the bug for a whole day. When he finally found it, he was so angry that he decided to define two new functions f(x) and G(x). Function f(x) returns the answer of ¬x, in other words, it is the bitwise NOT of xsince the most significant digit of x. For instance, we have: 
    f((4)10)=f((100)2)=(011)2=(3)10
    , and
    f((22)10)=f((10110)2)=(01001)2=(9)10

    On the other hand, function G(x) is defined in following way:

输入格式

    The input starts with a single line containing an integer T (1T10), indicating the number of test cases.

For each test case, the first line contains one integers N(1N100000), indicating the length of the string. The next lines contains a string S, representing the integer x of the corresponding test case in its binary form. It is guaranteed that there is no leading zeroes in the input.

 

输出格式

    For each test case, output a single line containing a 01 string, indicating the answer of G(x) in binary form. No leading zeroes are allowed.

输入样例

13100

输出样例

1
找规律,统计输入串中1的数量然后输出相应数量的1即可

#include<cstdio>#define N 100050using namespace std;int main(){    int t,n,i;    char str[N];    for(scanf("%d",&t);t--;){        scanf("%d",&n);        scanf("%s",str);        for(i=0;str[i];i++){            if(str[i]=='1'){                printf("1");            }        }        printf("\n");    }    return 0;}







0 0
原创粉丝点击