ZISUOJ 2023-2024学年第一学期《高级语言程序设计》期末试题(20240122)

说明:

        早上考完C语言,趁着做题环境还在,赶紧复现写一下题解和思路。对于我个人来说,这次考试发挥中规中矩吧,如果考试的时候冷静一点,可能错的次数会少一点,最后一题知道是用贪心做,但是当时没写出来,挺可惜的。

排名:

根据老师说的,期末成绩只根据做出题目的数量来给赋分,那我也算是年级段并列第一吧。

题目列表:

问题 A: Simple Calculator 

思路:

        当时直接看的输入输出写的答案,压根没看题目,猜到了分别是输出a+b,a-b,a*b,直接写即可。

参考题解:

#include <bits/stdc++.h>
using namespace std;
//using ll = long long;
//using PII = pair<int,int>;
//const int N = 1e4+5;
int main(){
	//问题 A: Simple Calculator
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int a,b;cin >> a >> b;
	cout << a+b << '\n';
	cout << a-b << '\n';
	cout << a*b << '\n';
	return 0;
}

问题 B: Segmented Linear Function

思路:

        之前写过的原题,用if判断一下x即可,然后根据不同情况进行输出。

参考题解:

#include <bits/stdc++.h>
using namespace std;
//using ll = long long;
//using PII = pair<int,int>;
//const int N = 1e4+5;
int main(){
	//问题 B: Segmented Linear Function
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int x;cin >> x;
	if(x<1) cout << x;
	else if(x<10) cout << 2*x-1;
	else cout << 3*x-11;
	return 0;
}

问题 C: Scholarship 

思路:

        此题重点在于读懂题目的(1)、(2)、(3)的判断,注意不要漏掉两个数都小于90输出0的情况。写if判断然后输出即可。

参考题解:

#include <bits/stdc++.h>
using namespace std;
//using ll = long long;
//using PII = pair<int,int>;
//const int N = 1e4+5;
int main(){
	//问题 C: Scholarship
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int a,b;cin >> a >> b;
	int minn = min(a,b);
	int maxn = max(a,b);
	if(minn>=95) cout << 500;
	else if(minn>=90&&maxn>=95) cout << 200;
	else if(minn>=90)cout << 100;
	else cout << 0;
	return 0;
}

问题 D: Acceleration of a Spaceship

 思路:

        根据题意模拟一下即可,注意设置初始值为8。

参考题解:

#include <bits/stdc++.h>
using namespace std;
//using ll = long long;
//using PII = pair<int,int>;
//const int N = 1e4+5;
int main(){
	//问题 D: Acceleration of a Spaceship
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int n;cin >> n;
	int ans = 0;
	int beginnum = 8;
	for(int i = 1;i<=n;i++){
		if(i==1) ans = beginnum*(i+1);
		else ans*=(i+1);
	}
	cout << ans;
	return 0;
}

问题 E: Mean Score and Maximum Score Difference

 思路:

        读题知道题目要求我们输出n个数的平均值和n个数中最大的差值。平均值的计算通过一个double类型的变量来累加所有数,最后输出的时候再除以n即可。而n个数的最大差值通过找到这些数中的最大值和最小值然后作差输出即可,当然,找最大最小值就通过最常用的打擂台的方式找即可。

参考题解:

#include <bits/stdc++.h>
using namespace std;
//using ll = long long;
//using PII = pair<int,int>;
const int N = 1e2+5;
int a[N];
int main(){
	//问题 E: Mean Score and Maximum Score Difference
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int n;cin >> n;
	for(int i = 1;i<=n;i++) cin >> a[i];
	int maxn = INT_MIN;
	int minn = INT_MAX;
    double ans = 0;
	for(int i = 1;i<=n;i++){
		if(a[i]>maxn) maxn = a[i];
		if(a[i]<minn) minn = a[i];
		ans+=a[i];
	}
	cout << fixed << setprecision(2) << ans/n << '\n';
	cout << maxn-minn << '\n';
	return 0;
}

问题 F: Draw a House

思路:

        很经典的题,考察的二重循环,最重要的是确认每个循环的次数和跳出循环的条件。确认好后按输出的格式输出即可。

参考题解:

#include <bits/stdc++.h>
using namespace std;
//using ll = long long;
//using PII = pair<int,int>;
const int N = 1e2+5;
int a[N];
int main(){
	//问题 F: Draw a House
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int n;cin >> n;
	for(int i = 1;i<=n-1;i++){
		for(int j = 1;j<=n-i;j++) cout << ' ';
		for(int j = 1;j<=2*i-1;j++) cout << '*';
		cout << '\n';
	}
	for(int i = 1;i<=n;i++){
		for(int j = 1;j<=2*n-1;j++) cout << '*';
		cout << '\n'; 
	}
	return 0;
}

问题 G: String Abbreviation

 

思路:

        读进char s[]数组中,或者使用string接收,然后模拟遍历,如果是连续的,先输出首个字符,遇到中间的字符则不输出,到了不连续的位置,则输出'-'和截止字符,我这里下面的写法需要考虑最后一个字符的状态,所以加了一个if判断。

参考题解:

#include <bits/stdc++.h>
using namespace std;
//using ll = long long;
//using PII = pair<int,int>;
const int N = 1e2+5;
//int a[N];
//string s;
char s[N];
int main(){
	//问题 G: String Abbreviation
//	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin >> s;
	int len = strlen(s);
	int type = 0;
	char beginc;
	int i;
	for(i = 0;i<len-1;i++){
		if(s[i+1]==s[i]+1&&type == 0){
			beginc = s[i];
			type = 1;
		}else if(s[i+1]!=s[i]+1&&type==1){
			printf("%c-%c",beginc,s[i]);
			type = 0;
		}else if(type==0) printf("%c",s[i]);
	}
	if(type == 1) printf("%c-%c",beginc,s[i]);
	else printf("%c",s[len-1]);
	return 0;
}

问题 H: Substring Statistics

思路:

        读代码知道考察的是指针。知道ptr是出现子串的第一个字符的相对下标,ans是子串出现的次数。因为src是char类型的指针,所以第一个选项选A,用于动态分配其内存。其他都是基本题。

参考题解:

#include <bits/stdc++.h>
using namespace std;
//using ll = long long;
//using PII = pair<int,int>;
int main(){
	//问题 H: Substring Statistics
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cout << "A\n";
	cout << "B\n";
	cout << "A\n";
	cout << "A\n";
	cout << "D\n";
	cout << "D\n";
	return 0;
}

 问题 I: Joe's Toy Store

思路:

        之前有写过类似的题。写一个结构体,然后通过两个字符串的字典序排序,排序好然后统计好数字并按照要求输出即可。这个题用map也可以更快地做出来。

参考题解:

#include <bits/stdc++.h>
using namespace std;
//using ll = long long;
//using PII = pair<int,int>;
const int N = 1e2+5;
struct node{
	string s1;
	string s2;
	int num;
}a[N];
bool cmp(node a1,node a2){
	if(a1.s2!=a2.s2) return a1.s2<a2.s2;
	else return a1.s1<a2.s1;
}
int main(){
	//问题 I: Joe's Toy Store
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int n;cin >> n;
	while(n--){
		int m;cin >> m;
		for(int i = 1;i<=m;i++) cin >> a[i].s1 >> a[i].s2 >> a[i].num;
		sort(a+1,a+1+m,cmp);
		int ans;
		int i = 1,j;
		int type = 0;
		while(i<=m){
			if(type==0){
				cout << a[i].s2 << '\n';
				type = 1;
			}
			ans = a[i].num;
			for(j = i+1;a[j].s1==a[j-1].s1&&a[j].s2==a[j-1].s2;j++) ans+=a[j].num;
			cout << "   |----" << a[i].s1 << '(' << ans << ")\n";
			i = j;
			if(a[i].s2!=a[i-1].s2) type = 0;
		}
		cout << '\n';
	}
	return 0;
}

问题 J: Win a Prize

思路:

        主要思想还是贪心,这个题还是后来向老师借鉴来的。后续等博主我理解再写吧。

参考题解:

#include <bits/stdc++.h>
using namespace std;
//using ll = long long;
//using PII = pair<int,int>;
const int N = 5e2+5;
int n = 0;
struct tinygame{
	int T,R;
}games[N];
bool arrange[N];
bool cmp(tinygame games1,tinygame games2){
	return games1.R>games2.R;
}
int main(){
	//问题 J: Win a Prize
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin >> n;
	for(int i = 0;i<n;i++) arrange[i] = false;
	for(int i = 0;i<n;i++) cin >> games[i].T;
	for(int i = 0;i<n;i++) cin >> games[i].R;
	sort(games,games+n,cmp);
	int ans = 0;
	for(int i = 0;i<n;i++){
		for(int t = games[i].T-1;t>=0;t--){
			if(!arrange[t]){
				arrange[t] = true;
				ans+=games[i].R;
				break;
			}
		}
	}
	cout << ans << '\n';
	return 0;
}

相关推荐

  1. 2023-2024山东大学软件学院web数据管理期末

    2024-01-27 23:52:01       30 阅读
  2. 广州大学2023-2024学年第一学期《计算机网络》A卷

    2024-01-27 23:52:01       38 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-27 23:52:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-27 23:52:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-01-27 23:52:01       82 阅读
  4. Python语言-面向对象

    2024-01-27 23:52:01       91 阅读

热门阅读

  1. 14. Spring Boot的自动配置

    2024-01-27 23:52:01       55 阅读
  2. linux动态库,静态库

    2024-01-27 23:52:01       52 阅读
  3. 用Spring Validation优雅实现参数校验

    2024-01-27 23:52:01       55 阅读
  4. springboot核心有几层架构

    2024-01-27 23:52:01       55 阅读
  5. 第十四届蓝桥杯C组题目 三国游戏

    2024-01-27 23:52:01       60 阅读
  6. python入门Flask框架学习(二)

    2024-01-27 23:52:01       59 阅读
  7. RabbitMq学习

    2024-01-27 23:52:01       45 阅读