A:枚举所有可能的情况,判断能否组成两个三角形。
1 #include<iostream> 2 #include<vector> 3 using namespace std; 4 bool three_one(int a){ 5 int cnt0=0,cnt1=0; 6 for(int i=0;i<6;i++){ 7 if(a>>i&1){ 8 cnt1++; 9 }else{ 10 cnt0++; 11 } 12 } 13 return cnt0==3&&cnt1==3; 14 } 15 bool check(vector<int> v){ 16 int a=v[0],b=v[1],c=v[2]; 17 if(a+b>c&&b+c>a&&a+c>b){ 18 return true; 19 } 20 return false; 21 } 22 int main(void){ 23 int t; 24 cin>>t; 25 while(t--){ 26 int a[6]; 27 for(int i=0;i<6;i++){ 28 cin>>a[i]; 29 } 30 bool flag=false; 31 for(int i=0;!flag&&i<1<<6;i++){ 32 if(three_one(i)){ 33 vector<int> v1,v2; 34 for(int j=0;j<6;j++){ 35 if(i>>j&1) 36 v1.push_back(a[j]); 37 else 38 v2.push_back(a[j]); 39 } 40 if(check(v1)&&check(v2)){ 41 flag=true; 42 } 43 } 44 } 45 if(flag) cout<<"Yes"<<endl; 46 else cout<<"No"<<endl; 47 } 48 return 0; 49 }
B:分两种情况,一种是没有括号的话就直接字符串处理,如果有括号的话就递归处理。
1 #include<iostream> 2 #include<unordered_map> 3 using namespace std; 4 typedef long long LL; 5 unordered_map<string,int> val; 6 int cal(string& s,int l,int r){//找到和l匹配的),返回他的位置 7 int cnt=0; 8 for(int i=l;i<=r;i++){ 9 if(s[i]=='('){ 10 cnt++; 11 }else if(s[i]==')'){ 12 cnt--; 13 } 14 if(cnt==0){ 15 return i; 16 } 17 } 18 return -1; 19 } 20 int dfs(string& s,int l,int r){ 21 if(l==r) return val[string(1,s[l])]; 22 LL res=0; 23 for(int i=l;i<=r;i++){ 24 LL sum=0; 25 LL to=i; 26 if(s[i]=='('){ 27 to=cal(s,i,r); 28 sum=dfs(s,i+1,to-1); 29 }else{ 30 string tmp=string(1,s[to]); 31 if(s[to+1]>='a'&&s[to+1]<='z'){//这里必须这样写,因为他有可能出现BaS这种情况 32 tmp+=s[to+1]; 33 to++; 34 } 35 sum=val[tmp]; 36 } 37 LL c=0; 38 while(to+1<=r&&isdigit(s[to+1])){ 39 c=c*10+s[to+1]-'0'; 40 to++; 41 } 42 if(c) sum*=c; 43 res+=sum; 44 i=to; 45 } 46 return res; 47 } 48 int main(void){ 49 int m,n; 50 cin>>m>>n; 51 for(int i=0;i<m;i++){ 52 string name; 53 int weight; 54 cin>>name>>weight; 55 val[name]=weight; 56 } 57 for(int i=0;i<n;i++){ 58 string s; 59 cin>>s; 60 cout<<dfs(s,0,s.size()-1)<<endl; 61 } 62 return 0; 63 }
C:将n转换成2进制,如果当前为1,就减掉右移,否则就只减掉。
1 #include<iostream> 2 using namespace std; 3 int main(void){ 4 int t; 5 cin>>t; 6 while(t--){ 7 int h; 8 cin>>h; 9 int res=0; 10 while(h){ 11 if(h&1){ 12 res++; 13 h>>=1; 14 }else{ 15 res++; 16 h-=1; 17 } 18 } 19 cout<<res<<endl; 20 } 21 return 0; 22 }
D:模拟题,因为数据范围很小,直接枚举哪些字母是需要留下的(二进制),之后判断留下这些字母是否有一个可行的方案。
判断的方法较为巧妙,从字母入手,如果一个字母被确定了是不能受影响的,那么如果一个格子是这个字母,那么这一行一列都不能有星星。
1 #include<iostream> 2 #include<cstring> 3 using namespace std; 4 const int N=25; 5 char a[N][N]; 6 int n,m,k; 7 int count(int n){ 8 int res=0; 9 while(n){ 10 if(n&1){ 11 res++; 12 } 13 n>>=1; 14 } 15 return res; 16 } 17 bool check(int x){ 18 int X[N],Y[N]; 19 memset(X,0,sizeof X); 20 memset(Y,0,sizeof Y); 21 for(int i=0;i<m;i++){ 22 if(x>>i&1){ 23 for(int j=0;j<n;j++){ 24 for(int k=0;k<n;k++){ 25 if(a[j][k]=='A'+i){ 26 X[j]=true,Y[k]=true; 27 } 28 } 29 } 30 } 31 } 32 for(int i=0;i<n;i++){ 33 for(int j=0;j<n;j++){ 34 if(a[i][j]=='*'&&X[i]&&Y[j]){ 35 return false; 36 } 37 } 38 } 39 return true; 40 } 41 int main(void){ 42 int t; 43 cin>>t; 44 while(t--){ 45 cin>>n>>m>>k; 46 for(int i=0;i<n;i++){ 47 for(int j=0;j<n;j++){ 48 cin>>a[i][j]; 49 } 50 } 51 bool flag=false; 52 for(int i=0;i<1<<m;i++){//枚举哪些字母需要保留 53 if(count(i)>=m-k&&check(i)){ 54 cout<<"yes"<<endl; 55 flag=true; 56 break; 57 } 58 } 59 if(!flag){ 60 cout<<"no"<<endl; 61 } 62 } 63 return 0; 64 }
E:一通分析之后可以发现如果人数是6的倍数的话,肯定选择性价比最高的那个。
如果有多余的人需要分情况讨论。
1 //假设都完美契合,那肯定直接选性价比高的 2 //但是会有例外的,因为不是每次都能完美装完 3 //假设双人船的性价比高 4 // 那么三人船只有可能0或者1,因为2个三人船不如3个双人船 5 //假设三人船性价比高 6 // 那么双人船有可能是0,1,2 7 #include<iostream> 8 using namespace std; 9 typedef long long LL; 10 int main(void){ 11 int t; 12 cin>>t; 13 while(t--){ 14 LL n,a,b; 15 cin>>n>>a>>b; 16 //全都开双人船 17 LL t1=(n+1)/2*a; 18 //双人船+一个三人船 19 LL t2=(n-3+1)/2*a+b;//-3意味着分出三个人去三人船,+1意味着不漏人 20 //全都开三人船 21 LL t3=(n+2)/3*b; 22 //三人船+一个双人船 23 LL t4=(n-2+2)/3*b+a;//-2意味着分出两个人,+2意味着不漏人 24 //三人船+两个双人船 25 LL t5=(n-4+2)/3*b+2*a;//-4意味着分出两个人去双人船,+2意味着不漏人 26 cout<<min(min(t1,t2),min(t3,min(t4,t5)))<<endl; 27 } 28 return 0; 29 }
F:大模拟题,给出每个人轮流下的位置,判定是否会有五子连珠的情况。
1 #include<iostream> 2 #include<cstring> 3 using namespace std; 4 const int N=5; 5 int a[N][N],b[N][N]; 6 bool sa[N][N],sb[N][N]; 7 int q[25]; 8 void fun(int x){ 9 for(int i=0;i<N;i++){ 10 for(int j=0;j<N;j++){ 11 if(a[i][j]==x){ 12 sa[i][j]=true; 13 } 14 if(b[i][j]==x){ 15 sb[i][j]=true; 16 } 17 } 18 } 19 } 20 bool check(bool a[][5]){ 21 for(int i=0;i<N;i++){ 22 bool flag=true; 23 for(int j=0;j<N;j++){ 24 if(a[i][j]==false) 25 flag=false; 26 } 27 if(flag){ 28 return true; 29 } 30 } 31 32 for(int j=0;j<N;j++){ 33 bool flag=true; 34 for(int i=0;i<N;i++){ 35 if(a[i][j]==false) 36 flag=false; 37 } 38 if(flag){ 39 return true; 40 } 41 } 42 43 bool flag=true; 44 for(int i=0;i<N;i++){ 45 if(a[i][i]==false) 46 flag=false; 47 } 48 if(flag) return true; 49 50 for(int i=0;i<N;i++){ 51 if(a[N-i-1][i]==false) 52 flag=false; 53 } 54 if(flag) return true; 55 56 return false; 57 } 58 int main(void){ 59 int t; 60 cin>>t; 61 while(t--){ 62 memset(sa,false,sizeof sa); 63 memset(sb,false,sizeof sb); 64 for(int i=0;i<N;i++){ 65 for(int j=0;j<N;j++){ 66 cin>>a[i][j]; 67 } 68 } 69 for(int i=0;i<N;i++){ 70 for(int j=0;j<N;j++){ 71 cin>>b[i][j]; 72 } 73 } 74 for(int i=0;i<N*N;i++){ 75 cin>>q[i]; 76 } 77 bool f1=false,f2=false; 78 for(int i=0;i<N*N;i++){ 79 fun(q[i]); 80 f1=check(sa); 81 f2=check(sb); 82 if(f1||f2) break; 83 } 84 if(f1&&f2) cout<<0<<endl; 85 else if(f1) cout<<1<<endl; 86 else if(f2) cout<<2<<endl; 87 } 88 return 0; 89 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端