寒假训练——vj题解

  • B - B
    M 算日期
    M 是一位数学高手,今天他迎来了 Kita 的挑战。Kita 想让 BM 算出这几年内有多少个闰年。

BM 觉得这问题实在太简单了,于是 Kita 加大了难度。

他先给出第一个年份,再给出一个整数。Kita 要 BM 进行加法运算后得到第二个年份,然后算出这两个年份之间有多少个闰年。

然而,BM 对于大于 9999
的年份十分恐惧,于是他耗尽力气,不仅将年份大于 9999
的部分删除了,并且利用多余的部分使这个年份更小了。

例如,如果 Kita 给出的第一个年份为 9997
,整数为 3
,进行加法运算后得到的年份为 10000
,大于 9999
的部分为 1
年,所以得到的第二个年份为 9999−1=9998
,BM 最终得到的年份区间就是 [9997,9998]

在进行了如上操作后,BM 筋疲力尽,甚至没有力气确定自己最终的年份区间是什么。为了守护他的数学高手的称号,请您帮助他算出最终的答案。
简单的模拟

点击查看代码
#include <bits/stdc++.h>
using namespace std;
bool isLeapYear(int year) {
	if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
		return true;
	} else {
		return false;
	}
}
int main ()
{
	int n,a,b;
    int ans=0;
	int res=0;
	int sum=0;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a>>b;
	     if(b<0)
		sum=a-abs(b);
		else
		 sum=a+b;
		if(sum>=10000)
		{
			res=sum-9999;
			sum=9999-res;
			if(sum>=a)
			{
				for(int i=a;i<=sum;i++){
					if(isLeapYear(i))
						ans++;
				}
				cout<<ans<<endl;
					ans=0;
			}else
			{
			for(int i=a;i>=sum;i--){
				if(isLeapYear(i))
					ans++;	
		     	}
				cout<<ans<<endl;
					ans=0;
			}
		}else
		{
			if(sum>=a)
			{
				for(int i=a;i<=sum;i++){
					if(isLeapYear(i))
						ans++;
				}
				cout<<ans<<endl;
				ans=0;
			}
			else if(sum<a)
			{
				for(int i=a;i>=sum;i--){
					if(isLeapYear(i))
						ans++;
				}
				cout<<ans<<endl;
					ans=0;
			}
		}
		
	
	}

	return 0;
}

* E - E BM 充饥 到了年末,BM 的积蓄所剩无几,但他又很想吃 KFC,请您帮助他画 "KFC" 充饥吧! 签到题,注意对齐和\这个符号需要两次
点击查看代码
#include <bits/stdc++.h>
using namespace std;
int main ()
{
	string a;
	
	cin>>a;
	cout<<" __      _____"<<endl;
    cout<<"|  | ___/ ____\\____"<<endl;
	cout<<"|  |/ /\\   __\\/ ___\\"<<endl;
	cout<<"|    <  |  | \\  \\___"<<endl;
	cout<<"|__|_ \\ |__|  \\___  >"<<endl;
	cout<<"     \\/           \\/"<<endl;
}
* H - H sueh- Draw Progress According to scientific research, in the waiting process, if the progress bar can be used to show the progress, the waiting person will be more patient.

Therefore, Hsueh- needs you to help him draw a progress bar in command line.

The specific progress bar style can be obtained by observing the sample.
后面那个数除前面那个数,一共是前面那个数的总数“-”为第一个数-第二个数,先输出第二个数相同的#

点击查看代码
#include <bits/stdc++.h>
using namespace std;
int main ()
{
	int n;
	double sum;
	cin>>n;
	int a,b;
	for(int i=1;i<=n;i++){
		cin>>a>>b;
		sum=1.0*b/a;
		cout<<"[";
		for(int i=1;i<=b;i++){
			cout<<"#";
		}
		for(int i=1;i<=a-b;i++)
			cout<<"-";
		cout<<"]";
		cout<<" "<<(int)(sum*100)<<"%"<<endl;
	}
}
* I - I 众所周知 bm 学姐喜欢旅游。这一天他来到了著名的杭师大景点勤园 13-406。该景点中有四个著名的观景台分为 A,B,C,D。

如果只去过上述一个观景台,输出 "Oh dear!!"(没有引号)。
如果去过两个,输出 "BaoBao is good!!"(没有引号)。
如果去过三个,输出 "Bao Bao is a SupEr man///!"(没有引号)。
如果全都去过,输出 "Oh my God!!!!!!!!!!!!!!!!!!!!!"(没有引号)。
如果一个都没去过,输出 "Bao Bao is so Zhai......"(没有引号)。
依旧是模拟,考虑全部情况

点击查看代码
#include <bits/stdc++.h>
using namespace std;
int solve (int x)
{
	int sum =0 ;
	while(x!=0)
	{
		sum+=x%10;
        x=x/10;
	}
	if(sum==6||sum>=16)
		return 1;
	else 
		return 0;
}
int main ()
{
	int sum=0;
	int a,b,c,d;
    cin>> a >> b >>c >>d;
      sum+=solve(a)+solve(b)+
	solve(c)+solve(d);
	if(sum==0)
	{
		cout<<"Bao Bao is so Zhai......";
	}else if(sum==1)
	{
		cout<<"Oh dear!!";
	}else if(sum==2)
	{
		cout<<"BaoBao is good!!";
	}else if(sum==3)
	{
		cout<<"Bao Bao is a SupEr man///!";
	}else if(sum==4)
	{
		cout<<"Oh my God!!!!!!!!!!!!!!!!!!!!!";
	}
	return 0;
}
* J -J 大扫除 你是一个住在大豪宅里的富二代「聘请的老青结清洁工」。

你需要给这座高 n
层楼的豪宅清理垃圾。

已知字符 "." 表示没有垃圾,其他任何字符都表示垃圾,每种字符表示一种垃圾。

要求计算整个豪宅每层楼垃圾种类的总和。
这段代码首先读取输入的测试用例数量 T。然后,对于每个测试用例,它会读取楼层数 n,并依次读取每层楼的垃圾情况。对于每层楼,它会将垃圾字符加入到一个无序集合 trashSet 中,确保只统计每层楼的垃圾种类数。然后,将 trashSet 的大小加到 totalTrashTypes 中,并清空 trashSet。最后,输出 totalTrashTypes。

点击查看代码
#include &lt;iostream&gt;
#include &lt;unordered_set&gt;
using namespace std;

int countTrashTypes(int n) {
    int totalTrashTypes = 0;
    unordered_set&lt;char&gt; trashSet;
    for (int i = 0; i &lt; n; i++) {
        string floor;
        cin &gt;&gt; floor;
        for (char c : floor) {
            if (c != '.') {
                trashSet.insert(c);
            }
        }
        totalTrashTypes += trashSet.size();
        trashSet.clear();
    }
    return totalTrashTypes;
}

int main() {
    int T;
    cin &gt;&gt; T;
    for (int i = 0; i &lt; T; i++) {
        int n;
        cin &gt;&gt; n;
        int totalTrashTypes = countTrashTypes(n);
        cout &lt;&lt; totalTrashTypes &lt;&lt; endl;
    }
    return 0;
}

* D -D Palindrome Hard Problem 个问题可以使用动态规划来解决。我们可以定义一个二维数组dp,其中dp[i][j]表示从第i个字符串到第j个字符串能够形成的回文串的最大数量。

首先,我们可以初始化dp[i][i]为1,因为单个字符串本身就是一个回文串。

然后,我们可以遍历字符串的长度l,从2开始到字符串的长度。对于每个长度l,我们遍历字符串的起始位置i,计算dp[i][i+l-1]的值。

对于dp[i][i+l-1],我们可以考虑两种情况:

如果第i个字符和第i+l-1个字符相等,那么dp[i][i+l-1]的值可以为dp[i+1][i+l-2] + 2。这是因为如果第i+1个字符到第i+l-2个字符能够形成回文串,并且第i个字符和第i+l-1个字符相等,那么就可以将这两个字符加入到回文串中,所以回文串的长度加2。

如果第i个字符和第i+l-1个字符不相等,那么dp[i][i+l-1]的值可以为max(dp[i+1][i+l-1], dp[i][i+l-2])。这是因为我们可以选择舍弃第i个字符或者舍弃第i+l-1个字符,来使得回文串的数量最大化。

最后,dp[0][n-1]就是我们所求的终极串能够分割出的回文串的最大数量。

点击查看代码
#include <iostream>
#include <vector>
#include <string>
using namespace std;

int countPalindromes(int n, vector<string>& strings) {
    int m = strings[0].length();
    vector<vector<int>> dp(n, vector<int>(n, 0));

    for (int i = 0; i < n; i++) {
        dp[i][i] = 1;
    }

    for (int l = 2; l <= m; l++) {
        for (int i = 0; i < n - l + 1; i++) {
            int j = i + l - 1;
            if (strings[i] == strings[j]) {
                dp[i][j] = dp[i + 1][j - 1] + 2;
            } else {
                dp[i][j] = max(dp[i + 1][j], dp[i][j - 1]);
            }
        }
    }

    return dp[0][n - 1];
}

int main() {
    int n;
    cin >> n;
    vector<string> strings(n);
    for (int i = 0; i < n; i++) {
        cin >> strings[i];
    }

    int result = countPalindromes(n, strings);
    cout << result << endl;

    return 0;
}

* K - K words Find Kth Element Hello, guys. I am Kwords. I prepared a problem for the contest invited by Hsueh-. Due to I was very boring, I generate n arrays, labeled from 1 to n . And then I will give q queries.

In each queries:

First, I will give a interger m
, and then m
different intergers.
You need to merge the arrays pointed to by these m
labels which I given.
Then, I will give a interger k
.
You need to answer me, what is the k
-th
smallest number in the combined array.
It is attention that, each query is independent. It means that the array merging operation of a single query will not affect other queries.

点击查看代码
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
    int n;
    cin >> n;

    vector<vector<int>> arrays(n);
    for (int i = 0; i < n; i++) {
        int m;
        cin >> m;
        arrays[i].resize(m);
        for (int j = 0; j < m; j++) {
            cin >> arrays[i][j];
        }
    }

    int q;
    cin >> q;

    for (int i = 0; i < q; i++) {
        int querySize;
        cin >> querySize;

        vector<int> indices(querySize);
        for (int j = 0; j < querySize; j++) {
            cin >> indices[j];
        }

        int k;
        cin >> k;

        vector<int> mergedArray;
        for (int j = 0; j < querySize; j++) {
            mergedArray.insert(mergedArray.end(), arrays[indices[j] - 1].begin(), arrays[indices[j] - 1].end());
        }

        sort(mergedArray.begin(), mergedArray.end());

        cout << mergedArray[k - 1] << endl;
    }

    return 0;
}

posted @ 2024-02-18 10:59  冬天的睡袋  阅读(34)  评论(0)    收藏  举报