LDUOJ-期末测试(6)

发布于 2022-12-10  493 次阅读


前言

扣不出来字了,拉胯了

题目

1.判断某一天是今年的第几天

描述

输入年月日,判断这一天是今年的第几天?

输入

输入8位数字,前4位表示年份,中间2位表示月份,后两位表示日期。

输出

一个整数,表示该天是今年的第几天。如果输入数据非法(月份不在1至12之间,或每个月的天数小于等于0或大于最大天数),输出-1。

输入:20200101

输出:1

输入:20200230

输出:-1

代码

#define _CRT_SECURE_NO_WARNINGS 1
using namespace std;
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
int main()
{
    int year = 0, month = 0, day = 0, any;
    int flag = 0, ans = 0;
    cin >> any;
    year = any / 10000;
    month = any % 10000 / 100;
    day = any % 100;
    if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
    {
        flag = 1;
    }
    ans += day;
    switch (month)
    {
    case 12:
        ans += 30;
    case 11:
        ans += 31;
    case 10:
        ans += 30;
    case 9:
        ans += 31;
    case 8:
        ans += 31;
    case 7:
        ans += 30;
    case 6:
        ans += 31;
    case 5:
        ans += 30;
    case 4:
        ans += 31;
    case 3:
        if (flag == 1)
        {
            ans += 29; 
        }
        else
        {
            ans += 28;
        }
    case 2:
        ans += 31;
    case 1:
        ans += 0;
    }
    if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
    {
        if (day <= 31 && day >= 1)
        {
            cout << ans;
        }
        else
        {
            cout << "-1";
        }
    }
    else if(month==2)
    {
        if (flag==0&&day>=1&&day<=28)
        {
            cout << ans;
        }
        else if (day >= 1 && day <= 29 && flag == 1)
        {
            cout << ans;
        }
        else
        {
            cout << "-1";
        }
    }
    else if (month == 4 || month == 6 || month == 9 || month == 11)
    {
        if (day >= 1 && day <= 30)
        {
            cout << ans;
        }
        else
        {
            cout << "-1";
        }
    }
    else
    {
        cout << "-1";
    }
    return 0;
}

提示

1月、3月、5月、7月、8月、10月、12月有31天,4月、6月、9月、11月有30天,平年2月有28天,闰年2月有29天

解析

2.最大公约数与最小公倍数

描述

计算两个整数的最大公约数与最小公倍数

输入

两个整数,保证在int类型的表示范围

输出

两行,分别表示两个整数的最小公倍数与最大公约数。

输入:

6 8

输出:

gcd(6,8)=2
lcm(6,8)=24

代码

#define _CRT_SECURE_NO_WARNINGS 1
using namespace std;
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
int jh(int a,int b)
{
	int t=a;
	a=b;
	b=t;
	return 0;
}
int main()
{
	int a, b, a1, b1;
	int t, f;
	cin >> a >> b;
	a1 = a, b1 = b;
	if (b > a)
	{
		jh(a, b);
	}
	f = a * b;
	while (a % b != 0)
	{
		t = a % b;
		a = b;
		b = t;
	}
	printf("gcd(%d,%d)=%d\n", a1, b1, b);
	printf("lcm(%d,%d)=%d", a1, b1, f / b);
	return 0;
}

提示

解析

3.判断一个数字能否被6整除

描述

输入一个整数(最大长度超过50位,但是不超过100位),判断它能否被6整除

输入

一个整数,数据范围已经超出了int、long、long long的表示范围。

输出

如果数字是6的倍数,输出Yes,否则输出No

输入:

12

输出:

Yes

输入:

5

输出:

No

代码

#define _CRT_SECURE_NO_WARNINGS 1
using namespace std;
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
char b[110]; 
int main()
{
	int j = 0;
	cin.getline(b,110);
	int a = strlen(b);
	int t = 0;
	for (; j <= a; j++)
	{
		t += b[j];
	}
	if (t % 3 == 0 && b[a-1] % 2 == 0)
	{
		cout << "Yes";
	}
	else
	{
		cout << "No";
	}
	return 0;
}

提示

由于数据比较大,可以考虑字符数组存储

一个数字是6的倍数,它应该既是3的倍数,又是2的倍数

解析

4.递归函数

描述

运用递归函数计算1+2+...+n的和

输入

正整数n

输出

输出1+2+...+n的和

输入:

4

输出:

10

代码

#define _CRT_SECURE_NO_WARNINGS 1
using namespace std;
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
int f(int n);
int main()
{
	int n;
	scanf("%d", &n);
	printf("%d", f(n));
	return 0;
}
int f(int n)
{
	if (n==1)
		return  (n);
	else
		return (n+f(n-1));
}

提示

解析

5.最大元素与最小元素的差

描述

计算一个数组中最大元素与最小元素的差

输入

第一行输出n,代表数组中元素的个数,n<100

接下来一行输入n个数

输出

数组中最大值与最小值的差

输入:

5
1 2 3 4 5

输出:

4

代码

#define _CRT_SECURE_NO_WARNINGS 1
using namespace std;
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#define N 100
int compute(int a[], int n, int* pmin, int* pmax)
{
	int i;
	*pmax = *pmin = a[0];
	for (i = 1; i < n; i++)
	{
		if (a[i] > *pmax)
		{
			*pmax = a[i];
		}
		if (*pmin > a[i])
		{
			*pmin = a[i];
		}
	}
	return 0;
}

int main()
{
	int n, i, a[N];
	int minValue, maxValue;
	scanf("%d", &n);
	for (i = 0; i < n; i++)
	{
		scanf("%d", &a[i]);
	}
	compute(a, n, &minValue, &maxValue);
	printf("%d", maxValue - minValue);
	return 0;
}

提示

解析

6.结构体操作

描述

学生的信息由姓名(不超过20个字符)、性别、年龄、C语言成绩、数学成绩、英语成绩组成,输入若干同学的基本信息,按平均分数降序排序,最后输出基本信息(如果平均分数相同,按输入的顺序输出)。

输入

第一行代表学生的人数n(n<100);

接下的n行中,每一行输入姓名、性别(m或f),年龄,C语言成绩、数学成绩、英语成绩三门功课的成绩(整数)

输出

按平均分由高到低排序,依次输出学生的相关信息。

输入:

2
zhang m 21 90 90 90
li f 19 94 95 97

输出:

li f 19 94 95 97
zhang m 21 90 90 90

代码

#define _CRT_SECURE_NO_WARNINGS 1
using namespace std;
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
struct student
{
    string name, xb;
    int age;
    int yw, sx, yy;
    int sum;
};
student a[110];
int n;
int main()
{
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        cin >> a[i].name;
        cin >> a[i].xb;
        cin >> a[i].age;
        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].xb << " " << a[i].age<<" "<<a[i].yw<<" "<<a[i].sx<<" "<<a[i].yy << endl;
    }
    return 0;
}

提示

在构造的结构体类型中,增加平均分数。

解析

7.整除的数

描述

输入两个整数a,b,计算a和b之间的能被4或6整除的数的个数

输入

输入两个整数a和b,注意二者的大小关系

输出

输出一行,表示a和b之间的能被4或6整除的数的个数(包含a和b)

输入:

4 8

输出:

3

代码

#define _CRT_SECURE_NO_WARNINGS 1
using namespace std;
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
int main()
{
	int a, b, t, ans = 0;
	cin >> a >> b;
	if (a > b)
	{
		t = a;
		a = b;
		b = t;
	}
	while (a <= b)
	{
		if (a % 4 == 0 || a % 6 == 0)
		{
			ans++;
		}
		a++;
	}
	cout << ans;
	return 0;
}

提示

解析

8.鞍点计算

找出具有m行n列二维数组Array的“鞍点”,即该位置上的元素在该行上最大,在该列上最小,其中1≤m,n≤10。同一行和同一列没有相同的数。 

输入

输入数据有多行,第一行有两个数m和n,下面有m行,每行有n个数。 

输出

按下列格式输出鞍点:
Array[i][j]=x
其中,x代表鞍点,i和j为鞍点所在的数组行和列下标,我们规定数组下标从0开始。
一个二维数组并不一定存在鞍点,此时请输出None。 
我们保证不会出现两个鞍点的情况,比如: 
3 3
1 2 3
1 2 3
3 6 8 

输入:

3 3
1 2 3
4 5 6
7 8 9

输出:

Array[0][2]=3

输入:

4 5
65 85 18 85 91
98 23 93 18 80
70 79 2 12 77
22 12 96 22 41

输出:

None

代码

#define _CRT_SECURE_NO_WARNINGS 1
using namespace std;
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#define N 100
int a[N][N];
int Min(int a[][N], int m, int n, int r)
{
	int i, p = 0;
	for (i = 0; i < m; i++)
	{
		if (a[p][r] > a[i][r])
			p = i;
	}
	return p;
}
int main()
{
	int m, n, i, j;
	cin >> m >> n;
	for (i = 0; i < m; i++)
		for (j = 0; j < n; j++)
		{
			cin >> a[i][j];
		}

	int max, r = 0, c, num = 0;
	for (i = 0; i < m; i++)
	{
		for (j = 0; j < n; j++)
		{
			if (a[i][r] < a[i][j])
				r = j;
		}
		max = a[i][r];
		c = Min(a, m, n, r);
		if (c == i)
		{
			printf("Array[%d][%d]=%d", i, r, a[i][r]);
			num++;
		}
	}
	if (num == 0)
	{
		printf("None");
	}
	return 0;
}

提示

解析

9.最长单词

描述

输入1行句子(不多于200个单词,每个单词长度不超过100),只包含字母和空格。单词由至少一个连续的字母构成,空格是单词间的间隔。
试输出第1个最长的单词。注意:如果所有单词长度相同,那么第一个单词就是最长单词。

输入

一行句子。

输出

1行,第一个最长的单词。

输入:

I am studying Programming language C in Peking University

输出:

Programming

代码

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

提示

解析