实习笔试;360;20200416

1 输入输出模板

import java.io.*;
import java.util.*;

public class Main {
	
    public static void main(String args[]) {
    	Scanner cin = new Scanner(System.in);
        int N, M;
        // 读取输入,直到没有整型数据可读
        while(cin.hasNextInt()){
        	// 读取N 和 M
        	N = cin.nextInt();
        	M = cin.nextInt();
        	System.out.println(N + " " + M);
        	// 读取接下来M行
        	for (int i=0; i<M; i++){
        		// 读取每行的a b c
        		int a = cin.nextInt(),
        			b = cin.nextInt(),
        			c = cin.nextInt();
        		System.out.println(String.format("%d %d %d", a, b, c));
        	}
        }
    }
    
}

2 题目一:守擂战

每个人都有战斗力值,当被打败,扔到队尾,胜者一直在队头,连胜场次达到一定次数,比赛结束
下面的输入代表4个人,当连赢2场,比赛结束
四个人的战斗力是 1 3 2 4
4 2
1 3 2 4

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {
    
    public static void main(String args[])
    {
        Scanner scanner = new Scanner(System.in);
        int N, M;
        while(scanner.hasNextInt()){
        	List<Integer> list = new ArrayList<>();
        	N = scanner.nextInt();
        	M = scanner.nextInt();
        	for(int i=0; i<N; i++){
        		list.add(scanner.nextInt());
        	}
        	
        	System.out.println(function(list, M));
        }
    }
	
    public static int function(List<Integer> list, int M){
    	int m = M;		// M为需要连胜的场次
    	int count = 0;	// 已进行场次数
    	while(!list.isEmpty()){
    		int i = list.get(0), j = list.get(1);
    		// 若队头输了,m初始化,仍需连赢M - 1场
    		if(i < j){
    			list.remove(0);
    			list.add(i);
    			m = M - 1;
    		}
    		// 若队头赢了,换下一个,更新m
    		else if(i > j){
    			list.remove(1);
    			m--;
    		}
    		
    		count++;
    		if(m == 0)	break;
    	}
    	return count;
    }
    
}
发布了131 篇原创文章 · 获赞 0 · 访问量 2257

猜你喜欢

转载自blog.csdn.net/weixin_43969686/article/details/105565095