LDUOJ-2022.11.27期末测试(1)

发布于 2022-11-28  269 次阅读


前言

这次测试基础题偏多,指针通过代码填空方式考察,难度降低了很多

希望期末的时候也和这个一样简单

题目

1.立方和

描述

现在请你求出给定的区间里所有奇数的立方和

输入

一行,两个整数a、b,表示区间的端点

输出

一行,表示计算结果

输入:1 3

输出:28

代码

#define _CRT_SECURE_NO_WARNINGS 1
using namespace std;
#include<iostream>
#include<cmath>
#include<string>
#include<algorithm>
int main()
{
	int a, b, t;
	int i, j, k = 0;
	cin >> a >> b;
	if (a > b)
	{
		t = a;
		a = b;
		b = t;
	}
	for (i = a; i <= b; i++)
	{
		if (i % 2 != 0)
		{
			k += i * i * i;
		}
	}
	cout << k;
	return 0;
}

提示

保证计算结果在int范围内,注意两个数字的大小关系

解析

2.出现次数

描述

给你n个数,请你计算其中每个数的出现次数。

输入

第一行一个整数n;

第二行n个整数,以空格分隔。

输出

输出若干行,从小到大,每一行输出该数和该数出现的次数,以空格分隔,行末无空格。

输入:

5
1 1 2 2 3

输出:

1 2
2 2
3 1

代码

#define _CRT_SECURE_NO_WARNINGS 1
using namespace std;
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
long long a[114514];
int main()
{
	long n;
	cin >> n;
	for (int i = 0; i < n; i++) 
	{
		cin >> a[i];
	}
	sort(a, a + n);
	long long s = a[0];
	int c = 1;
	for (int i = 1; i < n; i++)
	{
		if (s != a[i])
		{
			cout << s << " " << c << endl;
			s = a[i];
			c = 1;
		}
		else
		{
			c++;
		}
		if (i == n - 1)
		{
			cout << s << " " << c << endl;
		}
	}
	return 0;
}

提示

1≤n≤100,1≤a[i]≤100

解析

3.简单计算器

描述

一个最简单的计算器,支持+,-, *,/ 四种运算。使用时用户输入一个算式,算式共有3个参数,其中第1个参数为操作符(+,-,*,/),第2、3个参数为整数。

计算器将根据用户输入的算式输出结果。

现在请聪明的你编程完成上述功能吧!

输入

共两行。

第一行输入一个运算符op,保证为+,-, *,/ 之一。

第二行输入两个整数a,b。

输出

输出a op b的结果。

输入:

/
5 2

输出:

2

代码

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

提示

保证计算结果在int范围内,保证除数不为0

解析

4.比较大小

描述

相信你会比较两个数的大小,那么类比一下,现在请你比较两个字符串的大小,并按要求输出比较后的结果吧!

输入两个字符串aa和bb,如果字符串aa大于字符串bb,请输出“>”;如果字符串aa小于字符串bb,请输出“<”;

如果字符串aa等于字符串bb,请输出“=”;(你只需要输出引号内的内容即可)。

输入

两行,第一行表示字符串a,第二行表示字符串b。

输出

一行,比较后的结果。

输入:

aabb
bbaa

输出:

<

代码

#define _CRT_SECURE_NO_WARNINGS 1
using namespace std;
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
int main()
{
	string a, b;
	cin >> a >> b;
	if (a > b)
	{
		cout << '>';
	}
	else if (a < b)
	{
		cout << '<';
	}
	else
	{
		cout << '=';
	}
	return 0;
}

提示

1≤字符串长度≤1000

解析

5.矩阵乘法

描述

现输入一个n行m列的矩阵A和一个m行p列的矩阵B,输出 A×B。

输入

第一行给出三个整数n,m,p;

接下来给出n行,每行m个整数,表示矩阵A;

然后是m行,每行p个整数,表示矩阵B;

矩阵中每个元素值的绝对值不超过100;

输出

输出A×B的结果

输入:

2 3 2
1 2 3
3 2 1
1 1
2 2
3 3

输出:

14 14
10 10

代码

#define _CRT_SECURE_NO_WARNINGS 1
using namespace std;
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
int a[110][110], b[110][110], c[110][110];
int main()
{
	int n, m, p, i, j, k;
	cin >> n >> m >> p;
	for (i = 0; i < n; i++)
	{
		for (j = 0; j < m; j++)
		{
			cin >> a[i][j];
		}
	}
	for (i = 0; i < m; i++)
	{
		for (j = 0; j < p; j++)
		{
			cin >> b[i][j];
		}
	}
	for (i = 0; i < n; i++)
	{
		for (j = 0; j < p; j++)
		{
			for (k = 0; k < m; k++)
			{
				c[i][j] += a[i][k] * b[k][j];
			}
		}
	}
	for (i = 0; i < n; i++)
	{
		for (j = 0; j < p; j++)
		{
			if (j == p - 1)
			{
				cout << c[i][j] << endl;
			}
			else
			{
				cout << c[i][j] << " ";
			}
		}
	}
	return 0;
}

提示

1≤n,m,p≤100保证输入数据和结果均在int范围内。

解析

6.GCD+LCM

描述

输入两个整数,计算这两个数的gcd(最大公约数)和lcm(最小公倍数)。

输入

两个整数a和b

输出

每行一个,先输出最大公约数,后输出最小公倍数

输入:

2 4

输出:

2

4

代码

#define _CRT_SECURE_NO_WARNINGS 1
using namespace std;
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
int gcd(int a, int b)
{
	if (b == 0)
		return a;
	else
		return gcd(b, a % b);
}
int main()
{
	int a, b;
	int lcm;
	cin >> a >> b;
	lcm = a * b / gcd(a, b);
	cout << gcd(a, b) << endl << lcm;
	return 0;
}

提示

保证输入数据和结果均在int范围内。

解析

7.统计个数

描述

给定一个字符串,求出其中出现的大写字母、小写字母、数字出现的个数

输入

一行,一个字符串s

输出

三行,第一行为出现的大写字母的个数,第二行为出现的小写字母的个数,第三行为出现的数字的个数

输入:

aabb1

输出:

0
4
1

代码

#define _CRT_SECURE_NO_WARNINGS 1
using namespace std;
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
char a[114514];
int main()
{
    int i;
    int b[3] = { 0 };
    cin.getline(a, 114514);
    for (i = 0; a[i] != '\0'; i++)
    {
        if (a[i] >= 'a' && a[i] <= 'z')
        {
            b[0]++;
        }
        else if (a[i] >= '0' && a[i] <= '9')
        {
            b[1]++;
        }
        else if (a[i] >= 'A' && a[i] <= 'Z')
        {
            b[2]++;
        }
    }
    cout << b[2] << endl << b[0] << endl << b[1];
    return 0;
}

提示

1≤字符串长度≤1000

解析

8.数字交换

描述

给定两个整数a,b请你交换a和b的值并输出吧

输入

一行,给出两个整数a,b

输出

输出交换后的值,用空格分开

输入:

3 5

输出:

5 3

代码

#define _CRT_SECURE_NO_WARNINGS 1
using namespace std;
#include<iostream>
#include<cmath>
#include<string>
#include<algorithm>
void cul(long long* a, long long* b)
{
	long long t;
	t = *a;
	*a = *b;
	*b = t;
}
int main()
{
	long long a, b;
	scanf("%lld%lld", &a, &b);
	cul(&a, &b);
	printf("%lld %lld", a, b);
	return 0;
}//代码填空题

提示

1≤a,b≤1018,考虑到数据范围较大,  提示用long long数据类型

解析

9.统计成绩

描述

又到了一学期一度的考试周,老师要开始统计大家的成绩了。

从键盘上按学号输入n个学生的数据(包括姓名,学号,三门课的成绩),按照总成绩从高到低排序,

并输出学生的姓名、学号和总分。

输入

第一行一个整数n,表示n个学生;

接下来n行,每行包括一个字符串,表示学生姓名,三个整数,表示三门课的成绩。

输出

输出n行,表示按照总分从高到低排序后学生的信息,包括姓名、学号和总分,以空格分开。

输入:

3
aa 80 20 15
bb 90 95 100
cc 61 61 59

输出:

bb 2 285
cc 3 181
aa 1 115

代码

#define _CRT_SECURE_NO_WARNINGS 1
using namespace std;
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
struct student
{
    string name;
    int xh;
    int yw, sx, yy;
    int sum;
};
student a[110];
int n;
int main()
{
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        a[i].xh = i + 1;
        cin >> a[i].name;
        cin >> a[i].yw >> a[i].sx >> a[i].yy;
        a[i].sum = a[i].yw + a[i].sx + a[i].yy;
    }
    for (int i = n - 1; i > 0; i--)
    {
        for (int j = 0; j < i; j++)
        {
            if (a[j].sum < a[j + 1].sum)
            {
                swap(a[j], a[j + 1]);
            }
        }
    }
    for (int i = 0; i < n; i++)
    {
        cout << a[i].name << " " << a[i].xh << " " << a[i].sum << endl;
    }
    return 0;
}

提示

从键盘上按照学号顺序输入表示第一个输入的学生的学号就是1,以此类推。

1≤n≤100,成绩在[1,100]内,姓名长度<20。

解析