CSP入门模拟

划拳是古老中国酒文化的一个有趣的组成部分。酒桌上两人划拳的方法为:每人口中喊出一个数字,同时用手比划出一个数字。如果谁比划出的数字正好等于两人喊出的数字之和,谁就赢了,输家罚一杯酒。两人同赢或两人同输则继续下一轮,直到唯一的赢家出现。

下面给出甲、乙两人的划拳记录,请你统计他们最后分别喝了多少杯酒。

输入格式:

输入第一行先给出一个正整数 N(≤100),随后 N 行,每行给出一轮划拳的记录,格式为:

甲喊 甲划 乙喊 乙划

其中是喊出的数字,是划出的数字,均为不超过 100 的正整数(两只手一起划)。

输出格式:

在一行中先后输出甲、乙两人喝酒的杯数,其间以一个空格分隔。

N = int(input())

data = [[i for i in map(int, input().split())] for j in range(N)]
# print(data)
A = B = 0
for ele in data:
    total = ele[0] + ele[2]
    if ele[1] == total and ele[3] != total:
        B += 1
    elif ele[1] != total and ele[3] == total:
        A += 1
print(A,B)

一个数组A中存有N(>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(≥0)个位置,即将A中的数据由(A0​A1​⋯AN−1​)变换为(AN−M​⋯AN−1​A0​A1​⋯AN−M−1​)(最后M个数循环移至最前面的M个位置)。如果需要考虑程序移动数据的次数尽量少,要如何设计移动的方法?

输入格式:

每个输入包含一个测试用例,第1行输入N(1≤N≤100)和M(≥0);第2行输入N个整数,之间用空格分隔。

输出格式:

在一行中输出循环右移M位以后的整数序列,之间用空格分隔,序列结尾不能有多余空格。

n, m = map(int, input().split())
nums = [i for i in map(int, input().split())]

a = []
b = []
move = n - (m % n)
for i in range(n):
    if i < move:
        a.append(nums[i])
    else:
        b.append(nums[i])
result = b + a
print(" ".join(str(i) for i in result))

给定一系列正整数,请按要求对数字进行分类,并输出以下 5 个数字:

  • A1​ = 能被 5 整除的数字中所有偶数的和;
  • A2​ = 将被 5 除后余 1 的数字按给出顺序进行交错求和,即计算 n1​−n2​+n3​−n4​⋯;
  • A3​ = 被 5 除后余 2 的数字的个数;
  • A4​ = 被 5 除后余 3 的数字的平均数,精确到小数点后 1 位;
  • A5​ = 被 5 除后余 4 的数字中最大数字。

输入格式:

每个输入包含 1 个测试用例。每个测试用例先给出一个不超过 1000 的正整数 N,随后给出 N 个不超过 1000 的待分类的正整数。数字间以空格分隔。

输出格式:

对给定的 N 个正整数,按题目要求计算 A1​~A5​ 并在一行中顺序输出。数字间以空格分隔,但行末不得有多余空格。

若分类之后某一类不存在数字,则在相应位置输出 N

nums = [i for i in map(int, input().split())]
n = len(nums)
A = [0] * 6
signal = count = 0
signal_A = [0] * 6
nums.remove(nums[0])
for ele in nums:
    if ele % 10 == 0:
        A[1] += ele
        signal_A[1] = 1
    if ele % 5 == 1:
        A[2] = A[2] + (-1) ** signal * ele
        signal += 1
        signal_A[2] = 1
    if ele % 5 == 2:
        A[3] += 1
        signal_A[3] = 1
    if ele % 5 == 3:
        A[4] += ele
        signal_A[4] = 1
        count += 1
    if ele % 5 == 4:
        A[5] = max(A[5],ele)
        signal_A[5] = 1
for i in range(1,6):
    if i == 5:
        if signal_A[i] == 1:
            if i == 4:
                print(format(A[4]/count,'.1f') )
            else:print(A[i] )
        else:print("N" )
    else:
        if signal_A[i] == 1:
            if i == 4:
                print(format(A[4]/count,'.1f'),end=' ')
            else:print(A[i],end=' ')
        else:print("N",end=' ')

输入格式:

输入第 1 行给出正整数 N(≤105),即双方交锋的次数。随后 N 行,每行给出一次交锋的信息,即甲、乙双方同时给出的的手势。C 代表“锤子”、J 代表“剪刀”、B 代表“布”,第 1 个字母代表甲方,第 2 个代表乙方,中间有 1 个空格。

输出格式:

输出第 1、2 行分别给出甲、乙的胜、平、负次数,数字间以 1 个空格分隔。第 3 行给出两个字母,分别代表甲、乙获胜次数最多的手势,中间有 1 个空格。如果解不唯一,则输出按字母序最小的解。

#include <iostream>
using namespace std;

int main()
{
    int N;
    cin >> N;
    char tmp1, tmp2;
    int win=0, equ=0, lose=0;
    int C1=0, C2=0, B1=0, B2=0, J1=0, J2=0;
    while(N--){
        cin >> tmp1 >> tmp2;
        if(tmp1=='C' && tmp2=='C') {equ++;}
        else if(tmp1=='C' && tmp2=='J') {win++;C1++;}
        else if(tmp1=='C' && tmp2=='B') {lose++;B2++;}
        else if(tmp1=='J' && tmp2=='C') {lose++;C2++;}
        else if(tmp1=='J' && tmp2=='J') {equ++;}
        else if(tmp1=='J' && tmp2=='B') {win++;J1++;}
        else if(tmp1=='B' && tmp2=='C') {win++;B1++;}
        else if(tmp1=='B' && tmp2=='J') {lose++;J2++;}
        else if(tmp1=='B' && tmp2=='B') {equ++;}
    }
    cout << win << ' ' << equ << ' ' << lose << endl;
    cout << lose << ' ' << equ << ' ' << win << endl;
    if(B1>=C1 && B1>=J1) cout << "B ";
    else if(C1>=J1) cout << "C ";
    else cout << "J ";
    if(B2>=C2 && B2>=J2) cout << 'B';
    else if(C2>=J2) cout << 'C';
    else cout << 'J';
    return 0;
}

1042 Shuffling Machine

分数 20

全屏浏览

切换布局

作者 CHEN, Yue

单位 浙江大学

Shuffling is a procedure used to randomize a deck of playing cards. Because standard shuffling techniques are seen as weak, and in order to avoid "inside jobs" where employees collaborate with gamblers by performing inadequate shuffles, many casinos employ automatic shuffling machines. Your task is to simulate a shuffling machine.

The machine shuffles a deck of 54 cards according to a given random order and repeats for a given number of times. It is assumed that the initial status of a card deck is in the following order:

S1, S2, ..., S13, 
H1, H2, ..., H13, 
C1, C2, ..., C13, 
D1, D2, ..., D13, 
J1, J2

where "S" stands for "Spade", "H" for "Heart", "C" for "Club", "D" for "Diamond", and "J" for "Joker". A given order is a permutation of distinct integers in [1, 54]. If the number at the i-th position is j, it means to move the card from position i to position j. For example, suppose we only have 5 cards: S3, H5, C1, D13 and J2. Given a shuffling order {4, 2, 5, 3, 1}, the result will be: J2, H5, D13, S3, C1. If we are to repeat the shuffling again, the result will be: C1, H5, S3, J2, D13.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer K (≤20) which is the number of repeat times. Then the next line contains the given order. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print the shuffling results in one line. All the cards are separated by a space, and there must be no extra space at the end of the line.

#include <iostream>
#include <stdio.h>

using namespace std;

#define N 54

int main() {
#ifdef ONLINE_JUDGE
#else
    freopen("input.txt", "r", stdin);
#endif
    string cards[N] = {
        "S1","S2","S3","S4","S5","S6","S7","S8","S9","S10","S11","S12","S13",
        "H1","H2","H3","H4","H5","H6","H7","H8","H9","H10","H11","H12","H13",
        "C1","C2","C3","C4","C5","C6","C7","C8","C9","C10","C11","C12","C13",
        "D1","D2","D3","D4","D5","D6","D7","D8","D9","D10","D11","D12","D13",
        "J1","J2"
    };
    int K, order[N], rst[N];
    scanf("%d\n", &K);
    for(int i = 0; i < N; i++) {
        scanf("%d", &order[i]);
        rst[i] = i;
    }
    // 后面的i都代表cards中的某一个card i
    // rst储存着card i的洗牌后序列位
    while(K--) {
        for(int i = 0; i < N; i++) {
            rst[i] = order[rst[i]] - 1; // 从0开始
            rst[i] = rst[i] % N;
        }
    }
    for(int j = 0; j < N; j++) {
        if(j != 0) {
            cout << " ";
        }
        for(int i = 0; i < N; i++) { // 找出j号位卡牌
            if(rst[i] == j) {
                cout << cards[i];
                break;
            }
        }
    }
    return 0;
}

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值