【编程竞赛】CSDN 编程竞赛—第43期 20230405

【编程竞赛】CSDN 编程竞赛—第43期 20230405

1. 已知两个字符串A,B。 连续进行读入n次。每次读入的字符串都为A|B。输出读入次数最多的字符串。

输入描述:
第一行输入整数n.(1<=n<=100)。
以下n行读入n个字符串。
輸出描述:
输出读入次数最多的宇符串,如果两个字符串读入次数相同输出,“dogfall””。

import java.util.*;

public static void main(String[] args) throws Exception{
    
    
        Scanner scan = new Scanner(System.in);
        String str_0 = scan.nextLine().trim();
        int n = Integer.parseInt(str_0);
        ArrayList<String> vector = new ArrayList<>();
        for (int i = 0; i < n; i++){
    
    
            String str_1 = scan.nextLine().trim();
            vector.add(str_1);
        }
        scan.close();
        String result = solution(n, vector);
        System.out.println(result);
}
    
public static String solution(int n, ArrayList<String> vector){
    
    
        String result = "";
        HashMap<String, Integer> map = new HashMap<>();
        // 循环计数每个字符串出现的次数
        for(String c:vector){
    
    
            if(map.containsKey(c)){
    
    
                map.put(c,map.get(c)+1);
            }else {
    
    
                map.put(c,1);
            }
        }
        List<Map.Entry<String,Integer>> list = new ArrayList(map.entrySet());
        // 根据字符串出现的次数排序
        Collections.sort(list, Comparator.comparingInt(Map.Entry::getValue));
        int size = list.size();
        if(list.get(size-1).getValue() == list.get(size-2).getValue()){
    
    
            result = "dogfall";
        }else {
    
    
            result = list.get(size-1).getKey();
        }
        return result;
}    

2. 小豚鼠排排坐。小艺酱买了一排排格子的小房子n*m,她想让k只小豚鼠,每只小豚鼠,都有自己的房子。但是为了不浪费空间,她想要小房子的最外圈尽量每行每列都有一只小豚鼠居住(小豚鼠也可以住在中间的格子,只需保证房子最外国的行和列至少住一只豚鼠即可,无需每行每列都有豚鼠)。小艺酱想知道自己有多少种方案安排小豚鼠。

输入描述:
输入整数n,m,k。(1<=n,m<=20,0<=k<=n*m)
输出描述:
输出方案数,答案对1e9+7取模。

C++代码实现如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
int n,m,k,ans;
int a[100][100];

bool chk(){
    
    
	bool flag = 0;
	
	for(int i=1;i<=n;i++)
		if(a[i][1] == 1){
    
    
			flag = 1;
			break;
		}
	if(!flag) return false;
	
	flag = 0;
	for(int i=1;i<=n;i++)
		if(a[i][m] == 1){
    
    
			flag = 1;
			break;
		}
	if(!flag) return false;
	
	flag = 0;
	for(int i=1;i<=m;i++)
		if(a[1][i] == 1){
    
    
			flag = 1;
			break;
		}
	if(!flag) return false;
	
	flag = 0;
	for(int i=1;i<=m;i++)
		if(a[n][i] == 1){
    
    
			flag = 1;
			break;
		}
	if(!flag) return false;
	
	return true;
}
void dfs(int x,int y,int z){
    
    
	if(z == k){
    
    
		ans += chk();
		return ;
	}
	if(x == n+1) y++,x=1;
	if(y == m+1) return;
	dfs(x+1,y,z);
	a[x][y] = 1;
	dfs(x+1,y,z+1);
}
int main(){
    
    
	cin >> n >> m >> k;
	dfs(1,1,0);
	
	int MOD = 1000000007;
	
	cout << ans%MOD;
	return 0;
}

3. 某监狱有一个由n个牢房组成的大厅,每个牢房紧挨着。每个牢房里都有一个囚犯,每个牢房都是锁着的。一天晚上,狱卒感到无聊,决定玩一个游戏。在第一轮,他喝了一杯威士忌,然后跑下大厅,打开每个牢房的锁。在第二轮比赛中,他喝了一杯威士忌,然后跑下大厅,锁上每隔一个的牢房的锁(牢房2、4、6…)。在第三轮比赛中,他喝了一杯威士忌,然后跑下大厅。他每隔三个牢房(第3、6、9号牢房)就去一次。如果牢房被锁上了,他就把它打开;如果牢房门打开了,他就锁上牢房。他重复n轮,喝最后一杯,然后昏倒。一些囚犯(可能为零号) 意识到他们的牢房被解锁旦狱卒丧失了行动能力。他们就可以立即逃跑。现在根据牢房数量,确定有多少囚犯越狱。

输入描述:
第一行输入包含一个正整数t。表示有t行数据,下面每一行都包含一个介干5和100之回(含5和100)的整数,即轮数n
输出描述:
对于每一行,必须打印出监狱有n个牢房时越狱的囚犯人数。
示例:
输入
2
5
100
输出
2
10

#include<iostream>
#include<map>
using namespace std;
int main() {
    
    
	int t;
	cin >> t;
	while (t--) {
    
    
		map<int, int> m;
		int n;
		cin >> n;
		for (int i = 0; i < n; i++) {
    
    
			for (int j = 1; ; j++) {
    
    
				if ((i+1)*j > n) break;
				m[(i+1)*j]++;
			}
		}
		int ans = 0;
		for (int i = 1; i <= n; i++) {
    
    
			if (m[i] % 2 == 1) ans++;
		}
		cout << ans << endl;
	}
	return 0;
}

4. 开会了!作为一个集体,开会的时候桌子当然是需要和老大相邻的!(老大可能坐在桌子上边)小艺被分配到排桌椅的活,可是小艺的力气都用在吃上了,怎么可能搬动这些桌椅呢。她决定用现有的布局当作是会议座位安排。每个桌子分配一个人。

相邻桌子不同的桌子颜色不同。小艺想知道选择某个桌子之后老大身边能围多少人?

输入描述:
第一行输入2个整数n,m,一个字符c。(1<=n,m<=100)
分别表示空间大小和老大指定的桌椅颜色号。
以下n行每行m个字符。'.'代表空地,其他字符表示桌椅。连接在一起,相同的字符表示一个桌子。
输出描述:
输出围着老大的人数。

示例
输入
3 4 R
G.B.
BRRA
TTT.
输出
4

class Main {
    
    
    public static int xy[][] = {
    
    {
    
    -1,0},{
    
    0,-1},{
    
    1,0},{
    
    0,1}};
    public static boolean is[][];
    public static char mp[][];
    public static int n,m;
    public static void finish(int x,int y){
    
    
        is[x][y] = true;
        for(int i = 0;i<4;i++){
    
    
            int nx = x + xy[i][0];
            int ny = y + xy[i][1];
            if(nx >= 0 && ny >= 0 && nx < n && ny < m && !is[nx][ny] && mp[x][y] == mp[nx][ny]){
    
    
                finish(nx,ny);
            }
        }
    }
    public static void main(String[] args) {
    
    
        Scanner cin = new Scanner(System.in);
        String p = cin.nextLine();
        String a[] = p.split(" ");
        n = Integer.valueOf(a[0]);
        m = Integer.valueOf(a[1]);
        char boss = a[2].charAt(0);
        mp = new char[n][m];
        is = new boolean[n][m];
        for(int i = 0;i<n;i++){
    
    
            p = cin.nextLine();
            for(int j = 0;j<m;j++){
    
    
                mp[i][j] = p.charAt(j);
            }
        }
        int ans = 0;
        for(int i = 0;i<n;i++){
    
    
            for(int j = 0;j<m;j++){
    
    
                if(mp[i][j] == boss){
    
    
                    for(int k = 0;k<4;k++){
    
    
                        int nx = i+xy[k][0];
                        int ny = j+xy[k][1];
                        if(nx>=0 && ny >= 0 && nx < n && ny < m && !is[nx][ny] && mp[nx][ny] != '.' && mp[nx]
                                [ny] != boss){
    
    
                            ans++;
                            finish(nx,ny);
                        }
                    }
                }
            }
        }
        System.out.println(ans);
    }
}

猜你喜欢

转载自blog.csdn.net/universsky2015/article/details/129979082