枚举:
/算法枚举:完美立方问题 /*缩小枚举范围, 变量顺序*/ #include<bits/stdc++.h> using namespace std; int main() { int N; cin>>N; for(int a=2;a<=N;++a) for(int b=2;b<a;++b) for(int c=b;c<a;++c) for(int d=c;d<a;++d) if(a*a*a==b*b*b+c*c*c+d*d*d) printf("Cube=%d,Triple=(%d %d %d)\n",a,b,c,d); return 0; }
//算法枚举:生理周期问题 //减少尝试的次数,跳着试 #include<bits/stdc++.h> using namespace std; #define N 21252 int main(){ int p,e,i,d,caseNo=0; while(cin>>p>>e>>i>>d&&p!=-1){ ++ caseNo; int k; for(k=d+1;(k-p)%23;++k); for(;(k-e)%28;k+=23); for(;(k-i)%33;k+=23*28); cout<<"Case" <<caseNo<< ":the next triple peak occurs in "<<k-d<<endl; } return 0; }
//算法枚举:抛硬币 /*思路:对于每一枚硬币先假设它是轻的,看这样是否符合 称量结果,如果符合,问题解决,如果不符合,就假设他是重的,看是否符合 结果,把所有硬币都试一遍,一定能找到特殊硬币*/ #include<bits/stdc++.h> #include<cstring> using namespace std; char Left[3][7];//天平左边硬币 char Right[3][7];//天平右边硬币 char result[3][7];//结果 bool IsFake(char c,bool light); //light为真表示假币为轻,否则假设假币为真 int main(){ int t; cin>>t; while(t--){ for(int i=0;i<3;++i)cin>>Left[i]>>Right[i]>>result[i]; for(char c='A';c<='L';c++){ if(IsFake(c,true)){ cout<<c<<"is the couterfeit coin and is light.\n"; break; } else if(IsFake(c,false)){ cout<<c<<"is the counterfeit and it is heavy.\n"; break; } } } return 0; } bool IsFake(char c,bool light) //light为真表示假设假币为轻,否则表示假设假币为重 { for(int i=0;i<3;++i){ char *pLeft,*pRight; //指向天平两边的字符串 if(light){ pLeft=Left[i]; pRight=Right[i]; } else {//如何假设假币是重的,就把称量结果左右对换 pLeft=Right[i]; pRight=Left[i]; } switch(result[i][0]){ case'u': if(strchr(pRight,c)==NULL) return false; break; case'e': if(strchr(pLeft,c)||strchr(pRight,c)) return false; break; case'd': if(strchr(pLeft,c)==NULL) return false; break; } } return true; }