每天加点油(16)

  1. 链接:https://www.nowcoder.com/questionTerminal/5a0a2c7e431e4fbbbb1ff32ac6e8dfa0
    来源:牛客网

洗牌在生活中十分常见,现在需要写一个程序模拟洗牌的过程。 现在需要洗2n张牌,从上到下依次是第1张,第2张,第3张一直到第2n张。首先,我们把这2n张牌分成两堆,左手拿着第1张到第n张(上半堆),右手拿着第n+1张到第2n张(下半堆)。接着就开始洗牌的过程,先放下右手的最后一张牌,再放下左手的最后一张牌,接着放下右手的倒数第二张牌,再放下左手的倒数第二张牌,直到最后放下左手的第一张牌。接着把牌合并起来就可以了。 例如有6张牌,最开始牌的序列是1,2,3,4,5,6。首先分成两组,左手拿着1,2,3;右手拿着4,5,6。在洗牌过程中按顺序放下了6,3,5,2,4,1。把这六张牌再次合成一组牌之后,我们按照从上往下的顺序看这组牌,就变成了序列1,4,2,5,3,6。 现在给出一个原始牌组,请输出这副牌洗牌k次之后从上往下的序列。

输入描述:
第一行一个数T(T ≤ 100),表示数据组数。对于每组数据,第一行两个数n,k(1 ≤ n,k ≤ 100),接下来一行有2n个数a1,a2,…,a2n(1 ≤ ai ≤ 1000000000)。表示原始牌组从上到下的序列。

输出描述:
对于每组数据,输出一行,最终的序列。数字之间用空格隔开,不要在行末输出多余的空格。
示例1
输入
3
3 1
1
2
3
4
5
6
3 2
1
2
3
4
5
6
2 2
1
1
1
1
输出
1 4 2 5 3 6
1 5 4 3 2 6
1 1 1 1

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
 
public class Main {
 
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);// 使用Scanner来获取键盘输入
 
        // 第一次输入的是T
        // T:要洗牌的牌组数量
        int t = scanner.nextInt();
 
        for (int i = 0; i < t; i++) {
            // 第二次输入的是n和k
            // n:2n即牌的数量
            // k:洗牌的次数
            int n = scanner.nextInt();
            int k = scanner.nextInt();
 
            // 获得牌组cards,并分成两堆cards1和cards2
            ArrayList<Integer> cards = new ArrayList<>();
            ArrayList<Integer> cards1 = new ArrayList<>();
            ArrayList<Integer> cards2 = new ArrayList<>();
 
            // 获得牌组cards所有数
            for (int j = 0; j < 2 * n; j++) {
                cards.add(scanner.nextInt());
            }
            Collections.reverse(cards);// 将牌组顺序翻转,便于洗牌
 
            for (int x = 0; x < k; x++) {
                cards1.clear();
                cards2.clear();
 
                //将下半堆放入cards1
                for (int j = 0; j < n; j++) {
                    cards1.add(cards.get(j));
                }
 
                // 将上半堆放入cards2
                for (int j = n; j < 2 * n; j++) {
                    cards2.add(cards.get(j));
                }
 
                // 洗牌过程
                cards.clear();
                for (int j = 0; j < n; j++) {
                    cards.add(cards1.get(j));
                    cards.add(cards2.get(j));
                }
            }
            
            Collections.reverse(cards);// 再把牌组顺序反转为正常顺序
            String str = cards.toString().replace("[", "").replace("]", "").replace(",", "");
            System.out.println(str);
        }
        scanner.close();
    }
}
  1. 题目描述:统计同成绩学生人数
    读入N名学生的成绩,将获得某一给定分数的学生人数输出。

输入描述:
测试输入包含若干测试用例,每个测试用例的格式为
第1行: N
第2行: N名学生的成绩,相邻两数字用一个空格间隔。
第3行:给定分数
当读到N=0时输入结束。其中N不超过1000,成绩分数为(包含) 0到100之间的一个整数。

输出描述:
对每个测试用例,将获得给定分数的学生人数输出。

示例

输入:
3
80 60 90
60
2
85 66
0
5
60 75 90 55 75
75
0

输出:
1
0
2


import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while(scanner.hasNext()) {
            int n = scanner.nextInt();
            ArrayList<Integer> score = new ArrayList<Integer>();
            for(int i = 0; i < n; i++) {
                score.add(scanner.nextInt());
            }
            int temp = scanner.nextInt();
            int count = 0;
            for(int i = 0; i < n; i++) {
                if(score.get(i) == temp) {
                    count++;
                }
            }
            System.out.println(count);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值