PAT乙级 1069 微博转发抽奖 (20 分) Java 实现

该Java程序设计了一个彩票抽奖系统,每次循环读取输入的姓名,并根据设定的中奖规则决定是否为中奖者。程序跳过前S个名额,然后在剩余的M-S个人中,每N个人产生一个中奖者。如果在整个过程中没有中奖者,则输出'Keep going...'。程序使用了字符串数组来存储中奖者名单,并通过查找函数避免重复中奖。
摘要由CSDN通过智能技术生成
import java.io.*;

public class pat1069 {
	
	static String[] winners = new String[1001];		//记录中奖者名单

	public static void main(String[] args) throws IOException{
		// TODO Auto-generated method stub
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] arr = br.readLine().trim().split(" ");
		int M = Integer.parseInt(arr[0]);
		int N = Integer.parseInt(arr[1]);
		int S = Integer.parseInt(arr[2]);
		int winnerNum = 0;
		for(int i=1;i<=M;i++) {					//循环9次
			String name = br.readLine();
			if(i<S) continue;					//如果还没到第一个中奖人员,continue
			int loc = find(name);
			if(loc==-1) continue;				//如果已经存在,contune
			winners[loc] = name;				//把名字插入
			winnerNum++;
			System.out.println(name);
			for(int j=1;j<N;j++) {
				i++;
				String nextName = br.readLine();
			}
		}
		
		if(winnerNum==0) System.out.println("Keep going...");
		
	}
	
	private static int find(String name) {
		for(int i=0;i<1001;i++) {
			if(winners[i]==null) return i;
			if(winners[i].equals(name)) return -1;
		}
		return -1;
	}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值