POJ2359(约瑟夫环)

解题思路:题目虽然很长,其实就是一个约瑟夫环问题,对于输入的字符串,从开始依次进行报数,当报到N=1999时,删除对应的字符,字符串可以看作是首尾相连的环,直到剩余最后一个字符。如果剩余字符为'?',则输出结果:Yes;如果剩余字符为' ',则输出结果:No;否则输出结果:No comments .

注意:本题只有一组测试数据,如果写成输入多组的形式,会超时的。

View Code
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 const int N=1999;
 6 char str[30005];
 7 int main()
 8 {
 9     int i,t,k=0;
10     char ch;
11     while((ch=getchar())!=EOF)
12     {
13         if(ch!='\n')
14             str[k++]=ch;
15     }
16     t=0;
17     for(i=2;i<=k;i++)
18        t=(t+N)%i;
19     if(str[t]=='?') cout<<"Yes"<<endl;
20     else if(str[t]==' ') cout<<"No"<<endl;
21     else cout<<"No comments"<<endl;
22     return 0;
23 }
posted @ 2012-04-13 11:53  笑巧  阅读(376)  评论(0编辑  收藏  举报