poj 2359 || ural 1098 Questions
题目链接:http://poj.org/problem?id=2359
这题主要在于输入上,处理好了 之后就是约瑟夫环了。
多组数据,不过这个的约瑟夫环写的有点不好 会超时的。O(N^2)
View Code
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 const int N=1999; 6 int main() 7 { 8 int k,i,len,kk,t,ans=1,gg; 9 char str1[30006]; 10 char str2[30006]; 11 char tt[30006]; 12 while (ans) 13 { 14 k=0; 15 ans=0; 16 gg=0; 17 while (cin.getline(tt,30006)) 18 { 19 gg++; 20 len=strlen(tt); 21 for (i=0;i<len;i++) 22 { 23 str1[++k]=tt[i]; 24 } 25 if(tt[len-1]=='?'||tt[len-1]=='.'||tt[len-1]=='!') 26 { 27 ans=1; 28 break; 29 } 30 } 31 while (k>1) 32 { 33 t=0; 34 kk=N%k; 35 if(kk==0)k--; 36 else{ 37 for(i=kk+1;i<=k;i++) 38 str2[++t]=str1[i]; 39 for (i=1;i<kk;i++) 40 str2[++t]=str1[i]; 41 k--; 42 for(i=1;i<=k;i++) 43 str1[i]=str2[i]; 44 } 45 } 46 if(gg){ 47 if(str1[k]=='?')printf("Yes\n"); 48 else if(str1[k]==' ')printf("No\n"); 49 else printf("No comments\n"); 50 } 51 } 52 return 0; 53 }
一组数据 快速 O(N)
View Code
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 const int N=1999; 6 int main() 7 { 8 int k,i,ans=1,gg; 9 char str1[306]; 10 k=0; 11 char ch; 12 while ((ch=getchar())!=EOF) 13 { 14 if(ch!='\n') 15 { 16 str1[k++]=ch; 17 } 18 } 19 gg=0; 20 for (i=2;i<=k;i++) 21 { 22 gg=(gg+N)%i; 23 } 24 25 if(str1[gg]=='?')printf("Yes\n"); 26 else if(str1[gg]==' ')printf("No\n"); 27 else printf("No comments\n"); 28 29 return 0; 30 }