Zeros and Ones

 Zeros and Ones

时间限制: 1 Sec   内存限制: 128 MB
提交: 23   解决: 8
[ 提交][ 状态][ 讨论版]

题目描述

Consider strings of characters made up by concatenating any number of the strings 0, 01 or 11.
For example 00011111 is one such string, as is 001011, but 1011 is not.   Your job is simply to determine if a given string can be constructed in this manner.

输入

 There will be multiple problem sets. Input for each problem will be on one line. Each line (except

the last) will be of the form
n s
where n is a non-negative integer no larger than 100 and s is a string of 0’s and 1’s of length n.
A value of n = 0 indicates end of input.

输出

 Each problem should generate one line of code, either,

String m can be generated.
or
String m can not be generated.
accordingly, where m is the number of the problem (stating at 1).

样例输入

8 000111116 0010114 10110

样例输出

String 1 can be generated.String 2 can be generated.String 3 can not be generated.

提示

判断字符串开头有多少个连续的1,奇数个则可以,偶数个则不可以。

#include<iostream>
 
using namespace std;
 
int main()
{
    int n,t=1;
    char ch[1000];
    while(cin>>n,n)
    {
        int ok=1;
        int s=0;
        cin>>ch;
        for(int i=0;i<=n;i++)
        {
            if(ch[i]=='0')
                break;
            if(ch[i]=='1')
            {
                s++;
            }
        }
        if(s%2)
            cout<<"String "<<t++<<" can not be generated.\n";
        else
            cout<<"String "<<t++<<" can be generated.\n";
    }
    return 0;
}


你可能感兴趣的:(Zeros and Ones)