2022-2023-1高级语言程序设计A卷(通信,人工智能,电气)

发布于 2022-12-19  888 次阅读


前言

期末考试

考前胆战心惊了一段时间

最后发现自己多虑了😋

题目

1.排序

描述

给出N(N≤100)个数,使用沉底法按照从小到大的顺序输出。

输入

输入数据第一行是一个正整数N,第二行有N个整数。

输出

输出一行,从小到大输出这N个数,中间用空格隔开。

输入:

5
1 4 3 2 5

输出:

1 2 3 4 5

代码

#define _CRT_SECURE_NO_WARNINGS 1
using namespace std;
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
int main()
{
	long long a[101], temp;
	int n, i, j;
	cin >> n;
	for (i = 0; i < n; i++)
	{
		cin >> a[i];
	}
	for (i = 0;i<n; i++)
	{
		for (j = 0;j<n; j++)
		{
			if (a[j]>a[i])
			{
				temp = a[j];
				a[j] = a[i];
				a[i]=temp;
			}
		}
	}
	for (i = 0; i < n; i++)
	{
		cout << a[i] << " ";
	}
	return 0;
}

提示

解析

2.查找字符

描述

输入一个字符,判断在给定的字符串中是否存在该字符,若存在,给出该字符在字符串中第一次出现的位置。

输入

分两行输入,第一行输入给定的字符串,可能包含空格;第二行输入待查找的字符。

输出

输出待查找的字符在字符串中第一次出现的相对位置,如果不存在,输出-1。

输入:

Ludong University
u

输出:

2

代码

#define _CRT_SECURE_NO_WARNINGS 1
using namespace std;
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
char* search(char* ps, char ch);
int main()
{
	char str[80], * p, ch;
	cin.getline(str, sizeof(str)); //输入字符串str
	cin >> ch;
	p = search(str, ch);
	if (p)
	{
		cout << p+1-str;
	}
	else
	{
		cout << -1;
	}
	return 0;
}
char* search(char* ps, char ch)
{
	while (*ps != '\0')
	{
		if (*ps == ch)
		{
			return ps;
		}
		else
		{
			ps++;
		}
	}
	return NULL;
}

提示

解析

3.计算一整数各位之和

描述

输入一个正整数,计算该正整数各位数字的和

输入

输入一个正整数。

输出

输出各位数字之和。

输入:

1234

输出:

10

代码

#define _CRT_SECURE_NO_WARNINGS 1
using namespace std;
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
int main()
{
	int n, a = 0;
	cin >> n;
	for (; n != 0; n = n / 10)
	{
		a = a + n % 10;
	}
	cout << a;
	return 0;
}

提示

解析

4.计算两数间所有素数和

描述

输入两个大于1的正整数m和n,计算两个正整数之间的所有素数之和;如果m或n为素数,计算的和中包含m或n。

输入

输入m和n,m与n之间用空格分隔,m与n间的大小不确定。

输出

输出所有素数和。

输入:

2 9

输出:

17

代码

#define _CRT_SECURE_NO_WARNINGS 1
using namespace std;
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
int main()
{
	int n, m, a = 0, t, i, flag, sum = 0;
	cin >> n >> m;
	if (n > m)
	{
		swap(n, m);
	}
	if (n == 1)
	{
		n = 2;
	}
	for (t = n; t <= m; t++)
	{
		flag = 0;
		for (i = 2; i * i <= t; i++)
		{
			if (t % i == 0)
				flag = 1;
		}
		if (flag == 0)
		{
			sum = sum + t;
		}
	}
	cout << sum;
	return 0;
}

提示

解析

5.简单计算器模拟

描述

简单计算器模拟:输入两个整数和一个运算符,输出运算结果。

输入

总计一行输入,输入一个整数,再输入一个运算符,再输入一个整数,用空格隔开。

输出

输出两个整数运算后的结果。当除法运算时,除数为0时,输出“err”。

输入:

30 + 5

输出:

35

代码

#define _CRT_SECURE_NO_WARNINGS 1
using namespace std;
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
int jsq(int a, char b, int c)
{
	int ans;
	if (b == '+')
	{
		ans = a + c;
		cout << ans;
	}
	else if (b == '-')
	{
		ans = a - c;
		cout << ans;
	}
	else if (b == '*')
	{
		ans = a * c;
		cout << ans;
	}
	else if (b == '/')
	{
		if (c == 0)
		{
			cout << "err";
		}
		else
		{
			ans = a / c;
			cout << ans;
		}
	}
	return 0;
}
int main()
{
	char b;
	int a, c, ans;
	cin >> a >> b >> c;
	jsq(a, b, c);
	return 0;
}

提示

解析

6.单词统计

描述

输入一行字符(长度小于1000),统计其中单词的个数,以及首字母为‘a’的单词的个数,各单词以空格隔开,且空格数可以是多个。

输入

输入只有一行句子,仅由空格和英文字母构成。

输出

输出分两行,第一行为单词个数,第二行为首字母为‘a’的单词的个数。

输入:

The examiner will now ask the child their name

输出:

9

1

代码

#define _CRT_SECURE_NO_WARNINGS 1
using namespace std;
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
int main()
{
	char str[1005];
	int i, a, b = 0, a1 = 0;
	cin.getline(str, 1005);
	for (i = 0; str[i] != '\0'; i++)
	{
		if (str[i] != ' ' && str[i + 1] == ' ')
		{
			b++;
		}
		if (str[i] == ' ' && str[i + 1] == 'a')
		{
			a1++;
		}
	}
	a = strlen(str);
	if (str[a - 1] != ' ')
	{
		b++;
	}
	if (str[0] == 'a')
	{
		a1++;
	}
	cout << b << endl << a1;
	return 0;
}

提示

解析

7.矩阵操作

描述

输入两个3行3列的矩阵A和B,计算A+B的结果,以及A中的最大元素和最小元素。

输入

输入3行,每行输入3个整数,表示矩阵A的元素。

接下来3行,每行输入3个整数,表示矩阵B的元素。

输出

输出3行3列矩阵,表示A+B的结果,注意每一行最后一个数后无空格,

第四行输出A中最大的元素,第五行输出A中最小的元素。

输入:

1 2 3
1 2 3
1 2 3
4 5 6
4 5 6
4 5 6

输出:

5 7 9
5 7 9
5 7 9
3
1

代码

#define _CRT_SECURE_NO_WARNINGS 1
using namespace std;
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
int main()
{
	int a[3][3], b[3][3], c[3][3],min=0,max=0;
	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			cin >> a[i][j];
			if (i == 0 && j == 0)
			{
				min = a[i][j];
				max = a[i][j];
			}
			if (a[i][j] > max)
			{
				max = a[i][j];
			}
			if (a[i][j] < min)
			{
				min = a[i][j];
			}
		}
	}
	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			cin >> b[i][j];
		}
	}
	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			c[i][j] = a[i][j] + b[i][j];
			cout << c[i][j];
			if (j != 2)
			{
				cout << " ";
			}
			else
			{
					cout << endl;
			}
		}
	}
	cout << max << endl << min;
	return 0;
}

提示

解析

8. 数列求和

输入n的值,计算公式1!+3!+5!+7!+…+n!的结果。

输入

输入n的值,n为整型,且n为奇数

输出

输出公式的计算结果。

输入:

3

输出:

7

代码

#define _CRT_SECURE_NO_WARNINGS 1
using namespace std;
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
long long jc(int x)
{
	if (x == 1)
		return 1;
	return x * jc(x - 1);
}
int main()
{
	long long i, n;
	unsigned long long sum = 0;
	cin >> n;
	for (i = 1; i <= n; i = i + 2)
	{
		sum = sum + jc(i);
	}
	cout << sum;
	return 0;
}

提示

解析