田径赛跑中,有8条跑道,从键盘输入一个整数n(n>8)作为参加比赛的选手人数,安排分组要求人数差距最少,依次打印输出每组安排的选手人数。
------解决方案--------------------
- Java code
package com;import java.util.Scanner;public class Test2 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("请输入人数..."); int a = in.nextInt(); int n = a/8; int t = a%8; System.out.println(t+"--------"+n); if(t==0){ for(int i= 1;i<=8;i++){ System.out.println("第"+i+"条跑道有"+n+"人"); } } if(t>0){ for(int i= 1;i<=t;i++){ System.out.println("第"+i+"条跑道有"+((n*8)+1)+"人"); } for(int i= t+1;i<=8;i++){ System.out.println("第"+i+"条跑道有"+((n*8))+"人"); } } }}
------解决方案--------------------
下面是我的代码。 由于着急。 还有几个数字没法分组 例如 22、26、33、 你理解下代码然后自己添加下情况就ok了
import java.util.Scanner;
public class a {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//22、26
Scanner in = new Scanner(System.in);
System.out.println("请输入人数...");
int num = in.nextInt();
boolean m =false;
for (int j = 8; j >= 4; j--) {
Sel(num,j,m);
}
}
public static void Sel(int num,int j, boolean m){
int group = num/j;
int bl= num%j;
if((bl==0 || bl==j-1)&& m==false){
System.out.println("分为"+group+"组");
for (int i = 0; i <= group; i++) {
if (i<group) {
System.out.println("第" + (i + 1) + "组,有"+j+"人");
}else if(bl!=0){
System.out.println("第" + (i + 1) + "组,有"+(j-1)+"人");
}
}
if(bl==0){
System.exit(0);
}
}
}
}
------解决方案--------------------
我的:
- Java code
public static void main(String[] args) throws DocumentException { Scanner in = new Scanner(System.in); System.out.println("请输入人数..."); int num = in.nextInt(); boolean m = false; int groups = num % 8 == 0 ? num / 8 : num / 8 + 1; System.out.println("共分为" + groups + "组"); while (groups >= 1) { System.out.println("第"+groups+"组人数:" + num / groups); num = num - num / groups; groups--; } }
------解决方案--------------------
- Java code
Scanner in = new Scanner(System.in); System.out.println("请输入人数..."); int x = in.nextInt(); int i = 8; int m = x/8; int mod = x%8; int count = 0; int group = 0; if (mod == 0) { group = m; count = i; } else { group = m+1; count = x/(group); } if (x%(group) != 0) { int a = 0; for (int j = 0; j < x%(group); j++) { System.out.println("第" + (++a) + "组共" + (count + 1) + "人"); } for (int j = 0; j < (group)-(x%(group)); j++) { System.out.println("第" + (++a) + "组共" + count + "人"); } } else { for (int j = 0; j < m+1 ; j++) { System.out.println("第" + j + "组共" + count + "人"); } }