poj 水题系列

题目:http://poj.org/problem?id=3006

筛选法求素数

 1 #include <iostream>
 2  #include<cstdio>
 3  #include<cstring>
 4  #include<cstdlib>
 5  #include<stack>
 6  #include<queue>
 7  #include<cmath>
 8  #include<algorithm>
 9  using namespace std;
10  
11  int prime[1000005];//代表是否是素数
12  int main()
13  {
14      int s,max=1000005;
15      int x,y,z,i;
16      
17      s=0;
18      prime[0]=prime[1]=0;
19      prime[2]=1;
20      
21      for(int i=3; i<max; i++)
22          prime[i]=i%2==0?0:1;
23      int t=(int)sqrt(max*1.0);
24      
25      for(int i=3; i<=t; i++)
26          if(prime[i])
27              for(int j=i*2; j<max; j+=i)
28                  prime[j]=0;
29                  
30      while(cin>>x>>y>>z&&(x!=0||y!=0||z!=0))
31      {
32          for(i=x; z; i+=y)
33          {
34              if(prime[i])
35                  z--;
36          }
37          cout<<i-y<<endl;
38      }
39      return 0;
40  }
41  
42  
43  

http://poj.org/problem?id=2105

函数参考:http://www.cnblogs.com/sunyubo/archive/2009/07/21/2282256.html

 1 #include <iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cstdlib>
 5 #include<stack>
 6 #include<queue>
 7 #include<cmath>
 8 #include<algorithm>
 9 using namespace std;
10 char str[9];
11 
12 int main()
13 {
14     int t,i,j,x;
15     cin>>t;
16     for(j=1; j<=t; j++)
17     {
18         for(i=1; i<=4; i++)
19         {
20             scanf("%8s",str);
21         x=strtol(str,0,2);//strtol函数将字符串转换为n进制数,这里是2
22         if(i!=4)
23         printf("%d.",x);
24         else
25         printf("%d
",x);
26         }
27     }
28     return 0;
29 }

http://poj.org/problem?id=1013

参考:http://www.cppblog.com/guyuecanhui/articles/88302.html

 1 #include <iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cstdlib>
 5 #include<stack>
 6 #include<queue>
 7 #include<cmath>
 8 #include<algorithm>
 9 using namespace std;
10 
11 int a[200],b[200];//a为当前状态,b为上一个状态
12 int main()
13 {
14     int t,i,j,f;
15     char s1[100],s2[100],s3[100];
16     scanf("%d",&t);
17     getchar();
18     while(t--)
19     {
20         memset(a,-1,sizeof(a));
21         for(i=1; i<=3; i++)
22         {
23             scanf("%s%s%s",s1,s2,s3);
24             getchar();
25             if(strcmp(s3,"even")==0)
26             {
27                 for(j=0; j<strlen(s1); j++)
28                 {
29                     a[s1[j]]=a[s2[j]]=0;
30                 }
31             }
32             if(strcmp(s3,"up")==0)
33             {
34                 memcpy(b,a,sizeof(a));
35                 memset(a,0,sizeof(a));//与下面联系,对其他的添加信任
36                 for(j=0; j<strlen(s1); j++)
37                 {
38                     a[s1[j]]=2;a[s2[j]]=1;
39                 }
40                 for(j=65; j<=76; j++)
41                 {
42                     if(a[j]!=b[j] && b[j]>=0)//如果上一个状态有怀疑,而且两次怀疑不一样,添加信任
43                     a[j]=0;
44                 }
45             }
46 
47             if(strcmp(s3,"down")==0)
48             {
49                 memcpy(b,a,sizeof(a));
50                 memset(a,0,sizeof(a));
51                 for(j=0; j<strlen(s1); j++)
52                 {
53                     a[s1[j]]=1; a[s2[j]]=2;
54                 }
55                  for(j=65; j<=76; j++)
56                 {
57                     if(a[j]!=b[j] && b[j]>=0)
58                     a[j]=0;
59                 }
60             }
61         }
62         for(i=65; i<=76; i++)
63         {
64             if(a[i]>0)
65             {
66                 f=a[i];
67                 break;
68             }
69         }
70         if(f==1)
71         printf("%c is the counterfeit coin and it is light.
",i);
72         else if(f==2)
73         printf("%c is the counterfeit coin and it is heavy.
",i);
74     }
75     return 0;
76 }
原文地址:https://www.cnblogs.com/bfshm/p/3230424.html