高级班期末考试

root 站长 2020-06-28 9:49:55 0

1、 找零钱 考点:枚举算法

#include <iostream>
#include <cstdio>

using namespace std;

int main() {
    freopen("change.in", "r", stdin);
    freopen("change.out", "w", stdout);
    int n, sum = 0;
    cin >> n;

    for (int a = 0; a <= 100; a++)
        for (int b = 0; b <= 20; b++)
            for (int c = 0; c <= 10; c++)
                for (int d = 0; d <= 5; d++)
                    for (int e = 0; e <= 2; e++)
                        if (a + b * 5 + c * 10 + d * 20 + e * 50 == n)
                            sum++;

    cout << sum;
    return 0;
}

2、成绩排序,考点:结构体,排序

#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

struct stu {
    int id;  //学号
    string name;
    int sum;  //总成绩
} a[30];

bool cmp(stu x, stu y) { return x.sum > y.sum; }
int main() {
    freopen("result.in", "r", stdin);
    freopen("result.out", "w", stdout);
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> a[i].id >> a[i].name >> a[i].sum;

    sort(a + 1, a + 1 + n, cmp);

    for (int i = 1; i <= n; i++) cout << a[i].id << ' ' << a[i].name << ' ' << a[i].sum << endl;
    return 0;
}

3、车辆调度 考点:栈,模拟

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <stack>

using namespace std;

int main() {
    freopen("train.in", "r", stdin);
    freopen("train.out", "w", stdout);

    stack<int> c;
    int b[1010], n;
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> b[i];

    for (int i = 1, j = 1; i <= n; i++) {
        c.push(i);
        while (!c.empty() && c.top() == b[j]) c.pop(), j++;
    }

    if (c.empty())
        cout << "YES";
    else
        cout << "NO";
    return 0;
}

4、经典约瑟夫问题 考点:队列,模拟

#include <iostream>
#include <cstdio> 
#include <algorithm>
#include <queue>

using namespace std;


int main()
{
	freopen("joseph.in", "r", stdin);
	freopen("joseph.out", "w", stdout);
	
	queue<int> q;
	int n, m;
	cin >> n >> m;
	for (int i = 1; i <= n; i ++ ) q.push(i);
	
	while (!q.empty())
	{
		//数到第m-1个数字
		for (int i = 1; i < m; i ++ )
		{
			q.push(q.front());//队头插到队尾
			q.pop(); 
		} 
		//第m个人出队
		cout << q.front() << ' ';
		q.pop(); 
	}
	return 0;
}

5、建立邮局 考点: 中位数

#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

int a[30010], n;
int main() {
    freopen("A.in", "r", stdin);
    freopen("A.out", "w", stdout);

    cin >> n;
    for (int i = 1; i <= n; i++) scanf("%d", &a[i]);

    sort(a + 1, a + 1 + n);

    int mid = (1 + n) / 2;

    long long int sum = 0;
    for (int i = 1; i <= n; i++) sum += abs(a[i] - a[mid]);

    cout << sum;
    return 0;
}
{{ vote && vote.total.up }}