首页 > 代码库 > ZOJ水题专业户 ==|| (2)

ZOJ水题专业户 ==|| (2)

1216   1240   1241   1251  1331  1337  1350  1382  1712   1730  1760  1763   1797


//1797
技术分享
 1 #include<iostream> 2 #include<vector> 3   4 #include<stdio.h>//getchar()的文件 5 using namespace std; 6 int main() 7 { 8 int n,k,a,big; 9 cin>>n;10 vector<int> s;11 for(int i=1;i<=n;i++)12 {13 s.clear();//每次循环都要先清空栈。否则,push的位置会在之前一个循环的后面14    k=0;//归零15 while(true)16 {  cin>>a;17   char t=getchar();18  s.push_back(a);19    k++;20  21  if(t==\n)22  break;23 }24 for(int i=0;i<k-1;i++)25 {26  if(s[i]>s[i+1])27  28     big=s[i]; 29  else30 big=s[i+1];31  32 }33  34 int temp=1;35 while(true)36 {37   38 int bo=1;39  // cout<<"big "<<big<<endl;40   for(int i=0;i<k;i++)41   {42      if(big%s[i]!=0)43  bo=0;44   }45  // cout<<"bo "<<bo<<endl;46   if(bo==1)47   break;48        49   big=big/temp;//要还原!50   temp++;51   big=big*temp;52 }53  54 cout<<big<<endl;55 }56 return 0;57  58 }
View Code

 











//1763
技术分享
 1 #include<iostream> 2 #include<vector> 3 #include<iomanip> 4 using namespace std; 5 int main() 6 { 7     vector<float> s; 8 int n=0; 9 while(true)10    {11   12       float a;13  cin>>a;14  if(a==999)15  break;16  else17  {18  s.push_back(a);19  n++;20  }21    }22    for(int i=0;i<=n-2;i++)23    {24       float b=s[i+1]-s[i];25  cout<<fixed <<setprecision (2)<<b<<endl;26    }27    cout<<"End of Output"<<endl;28    29  30 return 0;31 }
View Code

 









//1760
技术分享
 1 #include<iostream> 2 #include<vector> 3 using namespace std; 4 int main() 5 { 6 while(true) 7 { 8        int a; 9   int m=0,n=0;10   vector<int> s;11   cin>>a;12   if(a==-1)13   break;14   else15   {s.push_back(a);16       m++;17     }18  19  20  21 while(true)22 {23  24    25 cin>>a;26 if(a==0)27 break;28 else29 {s.push_back(a);30       m++;31     }32 }33 for(int i=0;i<m;i++)34   { 35   for(int j=i+1;j<m;j++)36  {37    if(s[i]==2*s[j]||s[j]==2*s[i])38 n++;39  }40   }41 cout<<n<<endl;42  43 }44 return 0;45  46 }
View Code

 













//1730
技术分享
 1 #include <iostream> 2 using namespace std; 3 int main() 4 { 5     int k,sum; 6 cin>>k; 7     for(int i=1;i<=k;i++) 8 { 9    int n;10 cin>>n;11 if(n%2==0)12 {13 sum=0;14   for(int j=1;j<=n/2-1;j++)15   {16      sum=sum+j;17   }18   cout<<2*sum<<endl;19 }20 else21 {22   sum=0;23   for(int j=1;j<=(n+1)/2-1;j++)24   {25      sum=sum+j;26   }27   for(int j=1;j<=(n+1)/2-2;j++)28   {29      sum=sum+j;30   }31   cout<<sum<<endl;32 }33 }34 return 0;35 }
View Code

 
















//1712
技术分享
 1 #include<iostream> 2 #include<string> 3 #include<math.h> 4 using namespace std; 5 int main() 6 { 7 while(true) 8 { 9    string s;10 cin>>s;11 if(s[0]==0&&s.length() ==1)12 break ;13  14 int sum=0;15         for(int i=0;i<s.length() ;i++)16 { 17    sum+=(s[i]-0)*(pow(2,s.length()-i )-1) ;18 }19  20 cout<<sum<<endl;21  22 }23 }
View Code

 











//1382
技术分享
 1 #include<iostream> 2 using namespace std; 3 int main() 4 { 5   int k; 6   cin>>k; 7   for(int i=1;i<=k;i++) 8   { 9      int o1,p=0,tmep=1;10 long n;11 cin>>n;12 o1=1;13 // if(n==1)14      //  {cout<<1<<" "<<0<<endl;15 // break;16 // }17 //if(n==2)18 //{ cout<<1<<" "<<1<<endl;19   //    break ;20  //   }21  22 while(o1<=n)23 {24    p=0;25 tmep=1;26 while (true)27  {28   if(n<=o1*tmep )29     break;30        tmep=2*tmep;31          p++;32  33 }34 //cout<<o1<<"  ";  35 if(n==o1*tmep )36     break;37 o1=o1+2;38 }39  40 if(n==o1*tmep )41 cout<<o1<<" "<<p<<endl;42   43   }44   return 0;45 }
View Code

 














//1350
技术分享
 1 #include<iostream> 2 #include<vector> 3 using namespace std; 4 int main() 5 { 6 int m,k,l; 7   8 cin>>m; 9 for(int i=1;i<=m;i++)10 {  11 int n;12 int num=0;13 cin>>n;14 vector<int> c(n);15 for(int j=0;j<n;j++)16  c[j]=1;//全上锁17 for(l=1;l<=n;l++)18 { k=l-1;//这里的初始位置要注意19 while(k<n) 20 {21 if(c[k]==0)22  c[k]=1;23   else24  c[k]=0;25 k=k+l;26 } 27 }28 for(int p=0;p<n;p++)29 {30 if(c[p]==0)31 num++;32 }33 cout<<num<<endl;34 }35  36 return 0;37 }
View Code

 
















//1337
技术分享
 1 //have no common factor other than one : 2 //没有公共元素除了1——没有公约数——互质 3 #include<iostream> 4 #include<vector> 5 #include<math.h> 6 #include<iomanip> 7 using namespace std; 8   9 int IfNoCommon(int x,int y)10 {11 if(x==0)12 return y; //最后1肯定能被1整除,所以返回的y==1,说明Common只有1,即互质。13 return IfNoCommon(y%x,x); 14 }15  16  17 int main()18 {19   int n,total;20   21  22   while(true)23   {24     25  int m=0;//m要在这里定义 确保每次循环都初始值为零26  cin>>n;27 total =n*(n-1)/2;28 vector<int> a(n);29  30 if (n==0)31 break;32 for(int i=0;i<n;i++)33 { 34    int temp;35 cin>>temp;36 a[i]=temp;37  38 }39 for(int i=0;i<n;i++)40 {41     42  43 for(int j=i+1;j<n;j++)44 { 45 int  b=0;//b要在这里定义 确保每次循环都初始值为零(这个问题经常遇到)46 int x,y;47 if(a[i]<=a[j])48 {49   x=a[i];50   y=a[j];51 }52 else53 {54   y=a[i];55   x=a[j];56 }57 b=IfNoCommon(x,y);58 //cout<<"b="<<b;59        if(b==1)60                 m++;61  62 }63 }64  65 if(m==0)66 cout<<"No estimate for this data set."<<endl;67 else68 {   double Pi;69     //cout<<m<<endl;70 // cout<<total<<endl;71     Pi=sqrt(6/(double)m*(double)total) ;72     cout<<fixed<<setprecision(6) <<Pi<<endl;73 }74 //cout<<endl;75   }76   return 0;77 }
View Code

 












//1331
 
技术分享
 1 #include<iostream> 2 using namespace std; 3 int main() 4 { 5 int a,b,c,d; 6 for(a=3;a<=200;a++) 7 { 8   for(b=2;b<a;b++)   9      for(c=b;c<a;c++)  //很经典的循环10          for(d=c;d<a;d++)11  {12     if(b*b*b+c*c*c+d*d*d==a*a*a)13         cout<<"Cube = "<<a<<", Triple = ("<<b<<","<<c<<","<<d<<")"<<endl;14  }15  16   17 }18   19 return 0;20  21 }
View Code

 

 
 









//1251
技术分享
 1 #include<iostream> 2 #include<vector> 3 //#include<math.h> abs(float) 4 #include <stdlib.h> //abs(int) 5 using namespace std; 6 int main() 7 { 8     int n=0,k=0; 9 int avg;10  11 while(true)12 {13  14 int sum=0,temp=0;//确保循环初值归零15 bool B=false;16 cin>>n;17 if(n==0)18 break;19 vector<int> s(n); //s(len)20 if( n>=1 && n<= 50)21  {22  for( int i=0;i<n;i++)23       {24               25  26          27  cin>>s[i];28        29          if(s[i]<1||s[i]>100)30  {31  B=true;32  break;33          34      }35  sum+=s[i];36  37  }38  if(B)39  break;40  if(sum%n==0)41  { 42  avg=sum/n;43  for(int i=0;i<n;i++)44  temp=temp+abs(s[i]-avg);45  k++;46  cout<<"Set #"<<k<<endl;47  cout<<"The minimum number of moves is "<<temp/2<<"."<<endl;48  cout<<endl;49 }50 }51 else52 break;53 }54  55 return 0;56 }
View Code

 

 











//1241
技术分享
 1 #include<iostream> 2 #include<iomanip> 3 #include <math.h> 4   5 using namespace std; 6   7 int main(){ 8    int a,b,c,temp; 9 int n=0;10    while(true)11    {12    cin>>a>>b>>c;13    n++;14    if(a==0&&b==0&&c==0)15   break ;16    else if(a==-1)17        {cout<<"Triangle #"<<n<<endl;18     temp=c*c-b*b;19      if(temp<=0)20             cout<<"Impossible."<<endl;21  else22  cout<<"a = "<<fixed <<setprecision(3)<<sqrt(temp)<<endl; 23  cout<<endl;24   }25    else if(b==-1)26    {27        cout<<"Triangle #"<<n<<endl;28     temp=c*c-a*a;29      if(temp<=0)30             cout<<"Impossible."<<endl;31  else32  cout<<"b = "<<fixed <<setprecision(3)<<sqrt(temp)<<endl; 33  cout<<endl;34    }35    else if(c==-1)36    {37         cout<<"Triangle #"<<n<<endl;38 temp=a*a+b*b;39 cout<<"c = "<<fixed <<setprecision(3)<<sqrt(temp)<<endl; 40  cout<<endl;41    }42    }43    return 0;44        45 }46  
View Code

 










//1240
技术分享
 1 #include<iostream> 2 #include<string> 3 using namespace std; 4 int main() 5 { 6 int n=0,m=0; 7 string s; 8 char t; 9  cin>>n;10 for(int i=1;i<=n;i++)11     { 12  cin>>s;13  cout<<"String #"<<i<<endl;14  for(int j=0;j<s.length();j++)15  {16       17  if(s[j]==Z)18  {19     cout<<"A";20  }21  else{22      m=s[j]+1;23  t=(char)m;24  cout<<t;25  }26  }27  cout<<endl;28  cout<<endl;29 }30 }
View Code

 
















//1216
 
技术分享
 1 #include<iostream> 2 #include<iomanip> 3 using namespace std; 4 int main() 5 { 6   cout<<"# Cards  Overhang"<<endl; 7   int a; 8   double  sum; 9   while(cin)10   {11 sum=0;12      cin>>a;13 for(int i=1;i<=a;i++)14 {15    sum+=1.0/(2*i);16 }17 cout<<"    "<<a<<"     "<<fixed<<setprecision (3)<<sum<<endl;18 //printf("%5d%10.3f\n",a,sum);19   }20   return 0;21 }
View Code

 

 
/*用c++编 上传时总是会 报错 ,可能是因为需要判断空格的数量。 这里给出 C的代码:
   
 
 
技术分享
 1   #include<stdio.h> 2 int main() 3 { 4 int n,i; 5 double sum; 6   7 printf("# Cards  Overhang\n"); 8 while(scanf("%d",&n)!=EOF) 9 {10 sum=0.0;11 for(i=1;i<=n;i++)12 sum=sum+(1.0/i);13 printf("%5d%10.3f\n",i-1,0.5*sum);14 }15 return 0;16 }
View Code

 

ZOJ水题专业户 ==|| (2)