2291: Sequential game
时间限制: 1 Sec 内存限制: 128 MB提交: 18 解决: 7
[ 提交][ 状态][ 论坛]
题目描述
Sequential detector is a very important device in Hardware experiments. But is very difficult for many students such as HeroWei. In nearest days, his teacher Miss Fang let him simulate sequence detector. But there many sequence here ,and Miss Fang add the difficulty that when she give HeroWei a sequence and a string ,you must tell her how many string here in this sequence. But now Miss Fang simplified the problem for HeroWei, she just give Herowei a sequence and let HeroWei play a game with it.
When comes a character , it must compare with the rightmost one of sequence, if it's the same with the coming characters ,the coming one just join the string. Most the right side. Otherwise all the characters in the sequence must change its value, 0 to 1 and 1 to 0,then the coming one add to its right side. Here is an example, if there is a sequence '110' when comes a character 0,the sequence become '1100' at last, if comes a character 1,the sequence become '0011' at last.
It's very difficult for HeroWei to play the game. Can you help him?
输入
Each line contains a composed by a string of 0s and 1s.the input is ended by EOF.
输出
For each given sequence you just play with this string as descripted above.Output a line contains two integers ,which are the number of 0s and 1s in the sequence after the game.
样例输入
011011111010
样例输出
1 03 00 44 0
错了n多遍,终于过了,看代码:
#include<cstdio>
#include<cstring>
using namespace std;
int main(){
char str1[1010],str2[1010];
while(scanf("%s",str1)!=EOF){
int i,j;
str2[0]=str1[0]; str2[1]='\0';
for(i=1;i<strlen(str1);i++){
if(str2[i-1]!=str1[i])
for(j=0;j<i;j++)
str2[j]=!(str2[j]-'0')+'0';
str2[i]=str1[i];
str2[i+1]='\0';
}
int sum0=0,sum1=0;
for(i=0;i<strlen(str2);i++)
if(str2[i]=='0') sum0++;
else sum1++;
printf("%d %d\n",sum0,sum1);
}
return 0;
}