P2241 统计方形(数据加强版)
1 #include<iostream> 2 using namespace std; 3 long long n,m,rec,sqr; 4 int main() { 5 cin>>n>>m; 6 for(int i=1; i<=n; i++)//循环一条边长 7 for(int j=1; j<=m; j++) {//循环另一条边长0 8 if(i==j) sqr+=(n-i+1)*(m-j+1);//如果i==j,说明是正方形 9 else rec+=(n-i+1)*(m-j+1);//如果不等说明是矩形 10 } 11 cout<<sqr<<" "<<rec<<endl;//输出 12 return 0; 13 }
P2089 烤鸡
1 #include<iostream> 2 using namespace std; 3 int main() 4 { 5 int a,b,c,d,e,f,g,h,i,j,in,x=0; 6 cin>>in; 7 for (a=1;a<=3;a++) 8 { 9 for (b=1;b<=3;b++) 10 { 11 for (c=1;c<=3;c++) 12 { 13 for (d=1;d<=3;d++) 14 { 15 for (e=1;e<=3;e++) 16 { 17 for (f=1;f<=3;f++) 18 { 19 for (g=1;g<=3;g++) 20 { 21 for(h=1;h<=3;h++) 22 { 23 for (i=1;i<=3;i++) 24 { 25 for (j=1;j<=3;j++) 26 { 27 if (a+b+c+d+e+f+g+h+i+j==in) 28 { 29 x++; 30 } 31 } 32 } 33 } 34 } 35 } 36 } 37 } 38 } 39 } 40 } 41 cout<<x<<endl; 42 for (a=1;a<=3;a++) 43 { 44 for (b=1;b<=3;b++) 45 { 46 for (c=1;c<=3;c++) 47 { 48 for (d=1;d<=3;d++) 49 { 50 for (e=1;e<=3;e++) 51 { 52 for (f=1;f<=3;f++) 53 { 54 for (g=1;g<=3;g++) 55 { 56 for(h=1;h<=3;h++) 57 { 58 for (i=1;i<=3;i++) 59 { 60 for (j=1;j<=3;j++) 61 { 62 if (a+b+c+d+e+f+g+h+i+j==in) 63 { 64 cout<<a<<" "; 65 cout<<b<<" "; 66 cout<<c<<" "; 67 cout<<d<<" "; 68 cout<<e<<" "; 69 cout<<f<<" "; 70 cout<<g<<" "; 71 cout<<h<<" "; 72 cout<<i<<" "; 73 cout<<j<<endl; 74 } 75 } 76 } 77 } 78 } 79 } 80 } 81 } 82 } 83 } 84 } 85 return 0; 86 }
P1618 三连击(升级版)
1 #include<iostream> 2 #include<cstring> 3 using namespace std; 4 int a,b,c; 5 int num[15]; 6 int n=0; 7 bool work (int t) { 8 int ge=t%10; 9 int shi=t/10%10; 10 int bai=t/100; 11 12 if(num[ge]==0&&ge!=0) num[ge]=1; 13 else return false; 14 15 if(num[shi]==0&&shi!=0) num[shi]=1; 16 else return false; 17 18 if(num[bai]==0&&bai!=0) num[bai]=1; 19 else return false; 20 return true; 21 } 22 23 int main() { 24 cin>>a>>b>>c; 25 if(a==0||b==0||c==0) { 26 cout<<"No!!!"; 27 return 0; 28 } 29 int l=100/a+1; 30 for(int i=l; i; i++) { //a:b:c的i倍 保证比例正确 31 memset(num,0,sizeof(num)); 32 int x=a*i; 33 int y=b*i; 34 int z=c*i; 35 //cout<<x<<' '<<y<<' '<<z<<endl; 36 //在三位数范围内 a<b<c 37 if(x>100&&z<1000) { 38 //验证三个数的每一位 39 if(work(x)&&work(y)&&work(z)) { 40 cout<<x<<' '<<y<<' '<<z<<endl; 41 n++; 42 } 43 } 44 if(z>1000) break; 45 } 46 if(n==0) { 47 cout<<"No!!!"; 48 } 49 return 0; 50 }
P1036 [NOIP2002 普及组] 选数
1 #include<iostream> 2 #include<cstring> 3 using namespace std; 4 const int N=25; 5 int a[N],n,k; 6 int sum=0; 7 int cot(int x) { //返回x中有几位是1,sum保存取到数的和 8 sum=0; 9 int t=0,idx=0; 10 while(x>0) { 11 idx++;//记录运行到哪一位了 12 if(x&1==1) { 13 t++; 14 sum+=a[idx]; 15 } 16 x>>=1; 17 } 18 return t; 19 } 20 21 bool is_prime(int x) { 22 if(x<=1) return false; 23 for(int i=2; i*i<=x; i++) { 24 if(x%i==0) return false; 25 } 26 return true; 27 } 28 29 int main() { 30 cin>>n>>k; 31 for(int i=1; i<=n; i++) cin>>a[i]; 32 int s=(1<<n)-1; 33 int ans=0; 34 for(int i=1; i<=s; i++) { //每一个数转化为二进制表示一种状态 35 //1表示取,0表示不取 36 if(cot(i)==k&&is_prime(sum)) { //验证取了几个数&& 和是素数 37 ans++; 38 } 39 } 40 cout<<ans; 41 return 0; 42 }
P1157 组合的输出
1 #include<iostream> 2 #include<cstring> 3 #include<iomanip> 4 using namespace std; 5 int n,r; 6 int ans[25]; 7 int cot(int x) { //返回x中有几位是1 8 int t=0; 9 int idx=0; 10 while(x>0) { 11 idx++; 12 if(x&1==1) { 13 t++; 14 ans[t]=n-idx+1; 15 } 16 x>>=1; 17 } 18 return t; 19 } 20 21 int main() { 22 cin>>n>>r; 23 int s=(1<<n)-1; 24 for(int i=s; i>=0; i--) { 25 int k=cot(i); 26 if(k==r) { 27 for(int j=k; j>=1; j--) 28 cout<<setw(3)<<ans[j]; 29 cout<<endl; 30 } 31 } 32 return 0; 33 }
标签:std,return,暴力,idx,int,枚举,num,include From: https://www.cnblogs.com/mantou20210331/p/17239226.html