ZZULIOJ1075: 聚餐人数统计
题目描述
马克思手稿中有这样一道趣味数学题:男人、女人和小孩总计n个人,在一家饭店里吃饭,共花了cost先令,每个男人各花3先令,每个女人各花2先令,每个小孩各花1先令,请用穷举法编程计算男人、女人和小孩各有几个。
输入
输入两个正整数,表示人数n和花费cost。
输出
若问题有解,则输出所有解,每行输出三个数据,代表男人、女人和小孩的人数,用空格分隔;若问题无解,则输出“No answer"。
样例输入
30 50
样例输出
0 20 10 1 18 11 2 16 12 3 14 13 4 12 14 5 10 15 6 8 16 7 6 17 8 4 18 9 2 19 10 0 20
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int z=0;
boolean flag=true;
Scanner input = new Scanner(System.in);
int n=input.nextInt();
int cost=input.nextInt();
for (int x = 0; x <=cost/3; x++) {
for (int y = 0; y <=cost/2; y++) {
z=n-x-y;
if(cost==3*x+2*y+z&&n==x+y+z){
System.out.println(x+" "+ y+" "+z);
flag=false;
}
}
}
if(flag==true){
System.out.println("No Answer");
}
}
}

查看9道真题和解析