7-1 Simplified version of Tian Ji’s horse racing score 10

Author usx programming course group

Unit Shaoxing University of Arts and Sciences

This is a simplified version of Tian Ji’s horse racing problem. The details are as follows:
Tian Ji and the King of Qi are horse racing. Both sides have n horses participating. The bet for each game is 200 taels of gold. Now It is known that the speed of each horse of King Qi and Tian Ji is known, and King Qi must play according to the speed of the horse from fast to slow. Please write a program to help Tian Ji calculate how many taels of gold he can win at most (if he loses, it will be expressed as a negative number) .
For simplicity, ensure that the speeds of the 2n horses are different.

Input format:

First enter a positive integer T, indicating the number of groups of test data, and then T groups of test data.
Each set of test data inputs 3 lines. The first line is n (1≤n≤100), indicating the number of participating horses from both sides. The second line is n positive integers, indicating the number of Tian Ji’s horses. Speed, n positive integers in line 3, represents the speed of King Qi’s horse.

Output format:

For each set of test data, output a line containing an integer, indicating how many taels of gold Tian Ji can win at most.

Input example:

4
3
92 83 71
95 87 74
2
20 25
21 12
10
1 2 3 24 5 6 7 8 9 12
11 13 15 19 22 34 14 21 44 99
4
10 15 16 37
14 20 30 40

Output sample:

200
400
-1200
0

The c language code is analyzed as follows

#include <stdio.h>
int main() {
    int t;
    scanf("%d", &t);

    for (int i = 0; i < t; i++) {
        int n;
        scanf("%d", &n);//接收个数

        int tian[n], qi[n];
        for (int j = 0; j < n; j++) {
            scanf("%d", &tian[j]);//接收tianji马的速度
        }

        for (int j = 0; j < n; j++) {
            scanf("%d", &qi[j]);//接收qi马的速度
        }

        //对这两组数据同时进行排序
        for (int j = 0; j < n - 1; j++) {
            for (int k = 0; k < n - j - 1; k++) {
                if (tian[k] > tian[k + 1]) {
                    // 交换tian[k]和tian[k+1]
                    int temp = tian[k];
                    tian[k] = tian[k + 1];
                    tian[k + 1] = temp;
                }

                if (qi[k] > qi[k + 1]) {
                    // 交换qi[k]和qi[k+1]
                    int temp = qi[k];
                    qi[k] = qi[k + 1];
                    qi[k + 1] = temp;
                }
            }
        }

        //开始遍历
        int x = 0;
        int y = 0;
        int count = 0;

        for (x = 0; x < n; x++) {
            for (y = 0; y < n;) {
                if (tian[y] > qi[x]) {
                    y += 1;
                    count += 1;
                    break;
                } else {
                    y += 1;
                }
            }
            if (y == n) {
                break;  // 田忌所有的马都不能赢
            }
        }
        printf("%d\n", count * 200 - (n - count) * 200);
    }
    return 0;
}

The analysis of python code is as follows

t = int(input())
for i in range(t):
    n = int(input())
    tian = list(map(int, input().split()))  # 田忌马的速度
    qi = list(map(int, input().split()))  # 齐王马的速度
    tian.sort()
    qi.sort()
    x = 0
    y = 0
    count = 0
    for x in range(n):
        for y in range(n):  # 找到田忌比齐王最弱的马强的下标,齐马增加,田的马从y开始
            if tian[y] > qi[x]:
                y += 1
                count += 1
                break
            else:
                y += 1
        if y == n:
            break  # 田忌所有的马都不能赢
    print(count*200 - (n-count)*200)

Guess you like

Origin blog.csdn.net/Androidandios/article/details/134431082