洛谷3——《暴力枚举》

文章涉及C++编程中的组合数学问题、数组操作(如矩形、正方形计数、素数判断)、全集和子集枚举、排序算法以及特定问题如哈希函数和火柴问题的解决方案。
摘要由CSDN通过智能技术生成
#include<iostream>
#include<iomanip>
#include<cmath>
#include<string.h>
#include<string>
#include <ctime>
#include<cstdlib>
#include<algorithm>
#include<cstdio>
using namespace std;

// //p2241
// // 1.所有矩形数目(n+1)中选两个,(m+1)中选两个
// // 2.算长度为i正方形数目(n+1-i)*(m+1-i)
// // int main()
// // {
// //     int n,m;
// //     cin>>n>>m;
// //     int min =(n>m)?m:n;
// //     long long S = 1;
// //     long long Z = 0;
// //     for(int i =n+1,j = m+1;i>n-1&&j>m-1;i--,j--)
// //     {
// //         S *=(i*j);
// //     }
// //     S/=4;
// //     for(int i = 1;i<=min;i++)
// //     {
// //         Z +=(n+1-i)*(m+1-i);
// //     }
// //     cout<<Z<<' '<<S-Z;
// // }

// //p2089
// int main()
// {
//     int n;
//     cin>>n;
//     int count = 0;
//     for(int i =1;i<=3;i++)
//     {
//         for(int j =1;j<=3;j++)
//         {
//             for(int k =1;k<=3;k++)
//             {
//                 for(int w =1;w<=3;w++)
//                 {
//                     for(int l =1;l<=3;l++)
//                     {
//                         for(int o =1;o<=3;o++)
//                         {
//                             for(int p =1;p<=3;p++)
//                             {
//                                 for(int q =1;q<=3;q++)
//                                 {
//                                     for(int r =1;r<=3;r++)
//                                     {
//                                         for(int s =1;s<=3;s++)
//                                         {
//                                             if(i+j+k+w+l+o+p+q+r+s==n)
//                                             {
//                                                 count++;
//                                             }
//                                         }
//                                     }
//                                 }
//                             }
//                         }
//                     }
//                 }
//             }
//         }
//     }
//     cout<<count<<endl;
//     for(int i =1;i<=3;i++)
//     {
//         for(int j =1;j<=3;j++)
//         {
//             for(int k =1;k<=3;k++)
//             {
//                 for(int w =1;w<=3;w++)
//                 {
//                     for(int l =1;l<=3;l++)
//                     {
//                         for(int o =1;o<=3;o++)
//                         {
//                             for(int p =1;p<=3;p++)
//                             {
//                                 for(int q =1;q<=3;q++)
//                                 {
//                                     for(int r =1;r<=3;r++)
//                                     {
//                                         for(int s =1;s<=3;s++)
//                                         {
//                                             if(i+j+k+w+l+o+p+q+r+s==n)
//                                             {
                                                
//                                                 cout<<i<<' '<<j<<' '<<k<<' '<<w<<' '<<l<<' '<<o<<' '<<p<<' '<<q<<' '<<r<<' '<<s<<endl;
//                                             }
//                                         }
//                                     }
//                                 }
//                             }
//                         }
//                     }
//                 }
//             }
//         }
//     }
// }

//p1618
// 1.每个数字不重复---使用数组arr[9] = {0}
// 2.三个数字保证A:B:C

// int main()
// {
//     int A,B,C;
//     cin>>A>>B>>C;
//     int j;
//     int time = 0;
//     for(int i =1;i<=1000;i++)
//     {
//         int arr[10] = {0};//每次要重置0
//         int flag =1;
//         int one = i*A;
//         if(one<100||one>=1000)continue;
//         int x = one;
//         while(x)
//         {
//             arr[x%10]++;
//             x/=10;
//         }

//         int two = i*B;
//         if(two<100||two>=1000)continue;
//         int y = two;
//         while(y)
//         {
//             arr[y%10]++;
//             y/=10;
//         }
        
//         int tid = i*C;
//         if(tid<100||tid>=1000)continue;
//         int z = tid;
//         while(z)
//         {
//             arr[z%10]++;
//             z/=10;
//         }
//         for(j = 1;j<10;j++)
//         {
//             if(arr[j]==0)flag =0;
//         }

//         if(flag)
//         {
//             cout<<one<<' '<<two<<' '<<tid<<endl;
//             time++;
//         }
//     }
//     if(time==0)cout<<"No!!!\n";
// }

// p1036
// 1.判断素数
// 2.把要输入数字放在数组中
// 3.取多少数字相加,S= 在这个数组取三个数字相加
// 4.子集全部枚举

// bool Is_prime(int num)
// {
//     if(num<=1)return 0;
//     for(int i =2;i<=sqrt(num);i++)
//     {
//         if(num%i==0)return 0;
//     }
//     return 1;
// }

// int arr[100];

// int main()
// {
//     int n,k;
//     int count = 0;
//     cin>>n>>k;
//     for(int i= 0;i<n;i++)cin>>arr[i];
//     //找到全集
//     int a = (1<<n)-1;
//     //枚举所有子集
//     for(int j =0;j<a;j++)
//     {
//         int sum = 0;
//         if(__builtin_popcount(j)==k)//判断个数
//         {
//             for(int i =0;i<n;i++)
//             {
//                 if(j&(1<<i))sum +=arr[i];
//             }
//             if(Is_prime(sum))count++;
//         }
//     }
//     cout<<count;
// }


//p1157
// 1.将n个元素放入数组
// 2.获得全集后枚举所有子集
// 3.判断枚举个数
// 4.放在二维数组中,排序(50分)
// int arr[1000];
// string arr_2[1000];
// bool cmp(string a,string b)
// {
//     return a.compare(b)<0;
// }
// int main()
// {
//     int n,r;
//     int count = 0;
//     cin>>n>>r;
//     for(int i =0;i<n;i++)arr[i] = i+1;
//     int a = (1<<n)-1;//全集
//     for(int i =0;i<a;i++)
//     {
//         if(__builtin_popcount(i)==r)
//         {
//             for(int j =0;j<n;j++)
//             {
//                 if(i&(1<<j))
//                 arr_2[count] +=to_string(arr[j]);
//             }
//             count++;
//         }
//     }
//     sort(arr_2,arr_2+count,cmp);
//     for(int i =0;i<count;i++)
//     {
//         for(int j=0;j<r;j++)
//         cout<<setw(3)<<arr_2[i][j];
//         cout<<endl;
//     }
// }

//p1706
// int arr[10];
// int main()
// {
//     int n;
//     cin>>n;
//     for(int i =0;i<n;i++)arr[i]=i+1;
//     do
//     {
//         for(int i =0;i<n;i++)
//         {
//             cout<<setw(5)<<arr[i];
//         }
//         cout<<endl;
//     } while (next_permutation(arr,arr+n));
    
// }

// p1088
// int arr[100000];
// int main()
// {
//     int n,m;
//     cin>>n>>m;
//     for(int i = 0;i<n;i++)cin>>arr[i];
//     while(m--)
//     {
//         next_permutation(arr,arr+n);
//     }
//     for(int i =0;i<n;i++)cout<<arr[i]<<' ';
// }

//p3392
// char arr[100][100];
// int main()
// {
//     int N,M;
//     cin>>N>>M;
//     for(int i =0;i<N;i++)
//     {
//         for(int j =0;j<M;j++)cin>>arr[i][j];
//     }
//     int min = 10000;
//     for(int i =0;i<N-2;i++)//white和blue边界
//     {
//         for(int j =i+1;j<N-1;j++)//blue和red 边界
//         {
//             int count = 0;

//             for(int a =0;a<=i;a++)
//             {
//                 for(int k =0;k<M;k++)
//                 if(arr[a][k]!='W')count++;
//             }
//             for(int a =i+1;a<=j;a++)
//             {
//                 for(int k =0;k<M;k++)
//                 if(arr[a][k]!='B')count++;
//             }
//             for(int a =j+1;a<=N-1;a++)
//             {
//                 for(int k =0;k<M;k++)
//                 if(arr[a][k]!='R')count++;
//             }
//             if(count<min)min = count;
//         }
//     }
//     cout<<min;
// }

//p3654
// 1.分别统计行列
// 2.行统计,枚举每行元素,如果是'.'则判断后面是否有足够的'.'count++

// char arr[100][100];
// int main()
// {
//     int R,C,K;
//     cin>>R>>C>>K;
//     int count =0;
//     for(int i =0;i<R;i++)
//     {
//         for(int j =0;j<C;j++)cin>>arr[i][j];
//     }
//     //枚举行
//     for(int i =0;i<R;i++)
//     {
//         for(int j =0;j<C;j++)
//         {
//             if(arr[i][j]=='.')
//             {
//                 int flag = 1;
//                 for(int p = j+1;p<j+K;p++)
//                 {
//                     if(arr[i][p]=='#'||p>=C)
//                     {
//                         flag =0;
//                         break;
//                     }
//                 }
//                 if(flag)count++;
//             }
//         }
//     }
//     if(K==1)//注意考虑k=1情况
//     {
//         cout<<count;
//         return 0;
//     }
//     //枚举列
//     for(int j =0;j<C;j++)
//     {
//         for(int i =0;i<R;i++)
//         {
//             if(arr[i][j]=='.')
//             {
//                 int flag = 1;
//                 for(int p = i+1;p<i+K;p++)
//                 {
//                     if(arr[p][j]=='#'||p>=R)
//                     {
//                         flag =0;
//                         break;
//                     }
//                 }
//                 if(flag)count++;
//             }
//         }
//     }
//     cout<<count;
// }

//p1217(90)
// bool Is_prime(long long num)
// {
//     if(num<=1)return 0;
//     for(int i =2;i<=sqrt(num);i++)
//     {
//         if(num%i==0)return 0;
//     }
//     return 1;
// }
// bool is_huiwen(string num)
// {
//     int len = num.length();
//     if(len==1)return 1;
//     int left = 0,right = len-1;
//     while(left<right)
//     {
//         if(num[left]!=num[right])return 0;
//         left++;
//         right--;
//     }
//     return 1;
// }
// string arr[100000];
// int main()
// {
//     long long a,b;
//     int k =0;
//     cin>>a>>b;
//     for(long long i =a;i<=b;i++)
//     {
//         if(is_huiwen(to_string(i)))
//         {
//             if(Is_prime(i))
//             {
//                 arr[k] = to_string(i);
//                 k++;
//             }
//         }
//     }
//     for(int i =0;i<k;i++)cout<<arr[i]<<endl;
// }

//p1149
//1.数组存放每个数字的火柴数目
//2.函数统计火柴数
//3.枚举A,B
// int arr[10] = {6,2,5,5,4,5,6,3,7,6};
// int count(int num)
// {
//     if(num<10)return arr[num];
//     int sum =0;
//     while(num)
//     {
//         sum+=arr[num%10];
//         num/=10;
//     }
//     return sum;
// }
// int main()
// {
//     int S;
//     cin>>S;
//     int time =0;
//     for(int A =0;A<1000;A++)
//     {
//         for(int B =0;B<1000;B++)
//         {
//             int C = A+B;
//             int a = count(A);
//             int b = count(B);
//             int c = count(C);
//             if(a+b+c+4==S)time++;
//         }
//     }
//     cout<<time;
// }

//p3799
// 1.计数数组
// 2.枚举两个相同的边
// 3.枚举一个边,进而得到另一个边
// long long arr[100000] = {0};
// long long C(long long n,long long x)
// {
//     if(x==1)return n;
//     if(x==2)return n*(n-1)/2;
// }
// int main()
// {
//     int n;
//     int max = 0;
//     int j;
//     long long sum = 0;
//     long long k = 1e9+7;
//     cin>>n;
//     for(int i =0;i<n;i++)
//     {
//         cin>>j;
//         if(j>max)max = j;
//         arr[j]++;
//     }
//     for(int i =1;i<=max;i++)//枚举所有边
//     {
//         if(arr[i]>1)
//         {
//             long long a = C(arr[i],2);
//             for(int j =1;j<=i/2;j++)//枚举其中一个
//             {
//                 int k = (i-j);
//                 if(arr[j]>1&&k==j)
//                 {
//                     long long b = C(arr[j],2);
//                     sum +=a*b;
//                 }
//                 else if(arr[j]>0&&arr[k]>0&&j!=k)
//                 {
//                     long long b = C(arr[j],1);
//                     long long c = C(arr[k],1);
//                     sum +=a*b*c;
//                 }
//             }
//         }
//     }
//     cout<<sum%k;
// }

//2392
//1.把数据放在一维数组中
//2.判断一个数组所花时间函数(0分)
// int arr_1[5],arr_2[1000];
// int main()
// {
//     //初始化
//     int sum =0;
//     for(int i = 0;i<4;i++)cin>>arr_1[i];
//     for(int i = 0;i<4;i++)
//     {
//         memset(arr_2,0,1000*sizeof(int));
//         int j;
//         for(j =0;j<arr_1[i];j++)cin>>arr_2[j];
//         sort(arr_2,arr_2+j);
//         int left =0;
//         int right =0;
//         for(int k =j-1;k>=0;k--)
//         {
//             if(left<right)left +=arr_2[k];
//             else right +=arr_2[k];
//         }
//         sum+=max(left,right);

//     }
//     cout<<sum;
// }

//p2036

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值