HZNU摸底测试题2

第一题:
双峰插云
在这里插入图片描述
在这里插入图片描述
代码实现:

#include<bits/stdc++.h>
using namespace std;
int cloud[200][200];//储存云朵的信息
int ve_1[200][200];//一共多少种方案
set<int> se_1[200];//存储每种方案的颜色
int ret[200];//保存每种方案所剩余的那个云
int main(){
    int n,m;
    cin>>n>>m;
    for(int i=0;i<n;i++){
        int t;
        cin>>t;
        cloud[i][0]=t;//第一个数存每朵云的颜色数量
        for(int j=1;j<=t;j++){
            int ai;
            cin>>ai;
            cloud[i][j]=ai;
        }
    }

    for(int i=0;i<n;i++){//组合,因为是在n里面选n-1种,所以共有n中可能的选择,在n中选择里面进行筛选符合条件的
        int j=0;
        int a=0;
        while(j<n){
            if(j==i){
                ret[i]=j;
                j++;
                continue;
            }else{
            ve_1[i][a++]=j;
            for(int k=1;k<=cloud[j][0];k++)
            se_1[i].insert(cloud[j][k]);//将每朵云的颜色存储到set中过滤重复的颜色
             j++;
            }
        }
    }
    //寻找颜色种类最大的组合
    int max=0,num=1,r;//存储颜色的最大值和个数,以及第几种选择
    for(int i=0;i<n;i++){
        if(se_1[i].size()>max){
            max=se_1[i].size();
            r=i;
        }
    }
    for(int i=0;i<n;i++){
        if(se_1[i].size()==max){
            num++;
        }
    }
    if(num==1){//如果只有一种选择,输出
        cout<<"KK:";
        for(int j=0;j<n-1;j++){
            cout<<ve_1[r][j]+1;
        if(j<n-2)cout<<" ";
        }
        cout<<" Xuexue:";
        for(int j=0;j<n;j++){
            if(j!=ve_1[r][j]){
            cout<<j+1;
          break;
            }
        }
    }else{//如果不止一种选择
        int x=0,t;//x找到符合条件的选择中剩下的一朵云颜色最多
     for(int i=0;i<n;i++){
        if(se_1[i].size()==max){
            if(cloud[ret[i]][0]>x){
                x=cloud[ret[i]][0];
                t=i;
            }
        }
     }
      cout<<"KK:"<<endl;
        for(int j=0;j<n-1;j++){
            cout<<ve_1[t][j]+1;
        if(j<n-2)cout<<" ";//判断空格输出
        }
        cout<<endl;
        cout<<"Xuexue:"<<ret[t]+1<<endl;
    }
 	return 0;
}

思路:其实就是在N种选择中排列组合N-1,数学排列组合C(n,n-1)=C(N,1),那么一共有N种情况,在N种情况种找到KK需要的云使他们的颜色种类数最大(MAX),就相当于啊最后存了一堆数找其中的一个最大值。如果有多种选择随意输出一种就可以,注意多种选择的时候要给靴靴留下一个颜色种类最多的。
这道题说了尽量使用scanf/printf 这个比cin/cout

运行结果:
在这里插入图片描述
第二题
在这里插入图片描述
在这里插入图片描述
输入

3
5 5
0 0 1 0 0
0 0 1 0 0
2 2 0 2 2
0 0 1 0 0
0 0 1 0 0
5 5
0 0 1 0 0
0 0 1 0 0
2 2 0 0 2
0 0 1 0 2
0 0 1 0 0
5 5
1 2 1 2 2
1 2 1 2 2
0 0 0 0 2
1 0 1 2 0
1 0 1 0 0

输出

Win
3 3
Defense
3 3
Defeated

思路:感觉这道题 应该很典型吧,但是具体用什么算法不知道。应该要考虑很多种情况,我想的是在横竖或者斜对角线在5个子范围内如果有空格填上就可以判断赢,有四个字连在一起也可以直接判断为赢,先把位置填上再判断能否赢。具体实现没想到…

第三题
在这里插入图片描述
这道题运行超时了 还没想到原因 是结构体的问题?还是循环?

代码实现:

#include<bits/stdc++.h>
using namespace std;
struct per{
	int num;
	int soc;
	int pow;
}pera[10];
bool cmp(const per &x,const per &y){
	if(x.soc!=y.soc){
		return x.soc>y.soc;
	}else if(x.soc==y.soc){
	   return x.num<y.num;
	}	
}

int main(){
	int n,m,r;
	cin>>n>>m>>r;
	for(int i=0;i<2*n;i++){
		cin>>pera[i].soc; 
		pera[i].num=i+1;
	}
	for(int i=0;i<2*n;i++){
		cin>>pera[i].pow; 
	}
	
	while(--m){
		sort(pera,pera+2*n,cmp);
		for(int i=0;i<2*n;i=i+2){
			if(pera[i].pow<pera[i+1].pow){
				pera[i+1].soc++;
			}
			//cout<<pera[i].soc<<" ";
		}
	}
	for(int i=0;i<2*n;i++){
		if(i==r-1){
			cout<<pera[i].num;
			break;
		}
	}
	return 0; 
}

思路:定义一个结构体存每个人的编号、成绩、能量值。重新自定义sort()函数按照排序要求,每轮排一次,最后按照比赛规则来加分数,输出第r名。这个题感觉第二简单哈哈。
第四题:
在这里插入图片描述
思路:题读懂了后来,010 079 010 +010 079(十进制) 每三个分开代表一个一个LED数字
010 106 106…先不算了好困…

第五题:
在这里插入图片描述
代码实现:

#include<bits/stdc++.h>
using namespace std;

int main(){
    string str;
    getline(cin,str);
    int pos = 0;
    while((pos = str.find(" is ",pos))!=string::npos){
        str.replace(str.begin()+pos,str.begin()+pos+3," was");
        pos =pos+3;
    }
    cout<<str;
    return 0;
}

思路:找到is替换成was即可 这应该是最简单的一道题吧!

第六题:
在这里插入图片描述
代码实现:


```cpp
#include<bits/stdc++.h>
using namespace std;

int main(){
	int a, b, c;
	while(cin>>a>>b>>c){
 		for(int j=1;j<=(4*(b+c)-3);j++){
 			if(j==1||j==(4*c+2*b-2)||j==(4*(b+c)-3)){
	 			for(int i=0;i<2*c;i++){
					cout<<" ";
				}
				for(int i=0;i<2*a;i++){
					cout<<"*";
				}
			}
			if(((j>(2*c+2*b)&&j<(4*(b+c)-3))||(j>1&&j<2*c))&&(j!=(4*c+2*b-2))){
				for(int i=0;i<(2*b+2*c);i++){
					if(i==2*c||i==(2*c+2*a-1)){
						cout<<"*";
					}else{
						cout<<" ";
					}
				}
			}
			if(j>2*c&&j<2*c+2*b){
				for(int i=0;i<4*c+2*a;i++){
					if(i==0||i==2*c||i==4*c+2*a-1||i==2*c+2*a-1){
						cout<<"*";
					}else cout<<" ";
				}
			}
 			if(j==2*c||j==(2*c+2*b)){
 				for(int i=0;i<(2*a+4*c);i++){
					cout<<"*";
				}
			}
			cout<<endl;
			//cout<<" "<<j<<endl; 
		}	
	}
}
思路:哈哈这道题学到了,一行一行的输出,不要着急一口吃个胖子,找好“*”和“ ”的规律。


希望我以后能遇到题不要害怕!!!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值