#YGAP7. 阅读题集锦(二)

阅读题集锦(二)

第一题

int main() {
	int x, y, s, p;
	cin >> x >> y;
	s = x + y;
	p = x - y;
	if (x < y) p = y - x;
	s = s - p;
	cout << s;
}
//输入:13 31

1.运行结果:{{ input(1) }}

第二题

int main() {
	for (int i = 1; i <= 7; i++) a[i] = 0;
	for (int i = 1; i <= 4; i++) a[i] = i;
	int temp = a[7];
	for (int i = 7; i >= 2; i--) a[i] = a[i - 1];
	a[1] = temp;
	for (int i = 1; i <= 7; i++) cout << a[i] << ' ';
}

2.运行结果:{{ input(2) }}

第三题

int a[50], c, t, temp;
int main() {
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++) cin >> a[i];
	for (int i = 1; i < n; i++) {
		c = a[i], t = i;
		for (int j = i + 1; j <= n; j++) {
			if (c < a[j]) {
				t = j;
				c = a[j];
			}
		}
		if (t != i) {
			temp = a[i];
			a[i] = a[t];
			a[t] = temp;
		}
	}
	for (int i = 1; i <= n; i++) cout << a[i] << ' ';
}
//输入:
 //18
 //90 12 33 44 77 29 8 3 4 6 2 1 21 24 23 54 53 25

3.运行结果:{{ input(3) }}

第四题

int a[20][20], n, h, v, s;
int main() {
	cin >> n >> h >> v;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			cin >> a[i][j];
			if (i == h || j == v) s += a[i][j];
		}
	}
	if (h <= v) {
		for (int i = 1; i <= n - (v - h); i++) s += a[i][i + v - h];
	} else {
		for (int j = 1; j <= n - (h - v); j++) s += a[j + h - v][j];
	}
	cout << s - 2 * a[h][v];
}
/*
输入:
8 5 3
2 16 18 5 13 13 14 0
3 15 19 14 12 16 5 11
9 1 5 6 1 14 7 5
1 2 6 5 2 12 4 8
3 13 10 1 10 1 12 18
1 5 0 1 4 6 18 0
19 15 7 4 0 2 12 13
8 15 17 0 2 11 16 16
*/

4.运行结果:{{ input(4) }}

第五题

int main() {
	int x, y;
	cin >> x >> y;
	x = x + y;
	y = x - y;
	x = x - y;
	cout << x << ' ' << y;
}
//输入:3 7

5.运行结果:{{ input(5) }}

第六题

long long x = 1;
int main() {
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++) x = x * i;
	cout << x;
}
//输入:8

6.运行结果:{{ input(6) }}

第七题

#include <bits/stdc++.h>
using namespace std;

int func(int n) {
	if (n == 0) return 1;
	if (n < 0) return func(n + 1) + 3;
	return func(n - 1) - 2;
}

int main() {
	cout << func(func(2));
}

7.运行结果:{{ input(7) }}

第八题

long long s[40];
int main() {
	int a, j = 0;
	cin >> a;
	while (a != 0) {
		s[++j] = a % 2;
		a = a / 2;
	}
	if (j == 0) cout << 0;
	else {
		for (int i = j; i >= 1; i--) cout << s[i];
	}
}
//输入:58

8.运行结果:{{ input(8) }}