【Java】赛道公平分组问题
程序员文章站
2022-10-03 15:03:40
import java.util.Arrays;import java.util.Scanner;public class TrackGroup { public static void main(String[] args) { System.out.print("请输入参赛人数:"); Scanner sc = new Scanner(System.in); //参赛人数 int X = sc.nextInt(); ....
import java.util.Arrays;
import java.util.Scanner;
public class TrackGroup {
public static void main(String[] args) {
System.out.print("请输入参赛人数:");
Scanner sc = new Scanner(System.in);
//参赛人数
int X = sc.nextInt();
if(X<1||X>100){
System.out.println("输入有误!参赛人数:1-100人");
return;
}
//赛道
double TRACK = 8;
int group;
//可分的组
if(X<=8)group=1;
else{
if(X%8==0)group = X/8;
else group = X/8+1;
}
//每组的平均人数
int people = X/group;
int[] group_people = new int[group];
//平均分组后多余的人数
int left = X-group*people;
for (int i = 0; i < group; i++) {
//将多余的人数依次分配给平均分好的组
if(i<left)group_people[i] = people+1;
else group_people[i] = people;
}
System.out.println(Arrays.toString(group_people));
for (int i = 0; i < group_people.length; i++) {
System.out.println("第"+(i+1)+"组:"+group_people[i]+"人");
}
}
}
本文地址:https://blog.csdn.net/qq_44647926/article/details/109272676