请用java语言设计一个循环结构,计算一百块钱买一百只鸡问题。公鸡5元1只, 母鸡3元1只,小鸡1元3只,
发布网友
发布时间:2022-05-29 17:41
我来回答
共5个回答
热心网友
时间:2023-10-29 20:14
class Program
{
public static ArrayList func()
{
ArrayList resultList=new ArrayList();
for (int i = 0; i < 21; i++)
{
for (int j = 0; j < 34; j++)
{
for (int k = 0; k < 100; k++)
{
if (i + j + k == 100 && 5 * i + 3 * j + (double)k / 3 == 100)
{
int[] result = new int[3];
result[0] = i;
result[1] = j;
result[2] = k;
resultList.Add(result);
}
}
}
}
return resultList;
}
static void Main(string[] args)
{
ArrayList resultList = func();
foreach (int[] result in resultList)
{
Console.WriteLine("******************************************************");
Console.WriteLine("公鸡:" + result[0] + "只");
Console.WriteLine("母鸡:" + result[1] + "只");
Console.WriteLine("小鸡:" + result[2] + "只");
}
Console.ReadLine();
}
}
上面是c#写的代码和运行结果,思路就在这里,resultList是一个结果数组,包含了所有的可能结果,上图显示的是4个。每一个结果是一个int[3],即长度为3的int数组0位表示公鸡,1位表示母鸡,2位表示小鸡。
授人以鱼不如授人以渔,如果你确实是想学习java的话就自己把他翻译成java的吧。c#的语法逻辑和java完全一致,仅仅有些细微的差别。比如没有foreach函数,得自己写for循环来遍历结果数组。输出到console的语法也不一样,Console.WriteLine()要变成System.out.println()。其他几乎一模一样。
热心网友
时间:2023-10-29 20:14
/*
* 假设全买小鸡,最多能买300只 全买公鸡,最多能买20只 全买母鸡,最多能买33只 三个变量:小鸡x 公鸡g 母鸡m
*/
for (int x = 3; x < 100; x = x + 3) { // 小鸡的个数肯定是3的倍数,所以x的变化是已3的倍数递增
for (int g = 1; g < 20; g++) {
for (int m = 1; m < 33; m++) {
//System.out.println((x + g + m) + " " + (1 * x + 5 * g + 3 * m ));
if (x + g + m == 100 && x / 3 + 5 * g + 3 * m == 100) { // 个数和总价都为100
System.out.println("公鸡数量:" + g + ",母鸡数量:" + m + ",小鸡数量:" + x);
}
}
}
}
结果:三种组合结果(只考虑三种全有的情况)
公鸡数量:4,母鸡数量:18,小鸡数量:78
公鸡数量:8,母鸡数量:11,小鸡数量:81
公鸡数量:12,母鸡数量:4,小鸡数量:84
热心网友
时间:2023-10-29 20:15
package selfAddMinus;
public class XiaoJi {
public static void main(String[] args) {
// TODO Auto-generated method stub
int count=0;
for(int five=0;five<=20;five++) {
for(int three=0;three<=34;three++) {
for(int one=0;one<=100;one++) {
if(5*five+3*three+1*one==100) {
count++;
System.out.println(count+"种"+"公鸡:"+five+"只"+" "+"母鸡:"+three+"只"+" "+"小鸡:"+(3*one)+"只");
}
}
}
}
}
}
这是你要的代码,总共364种
热心网友
时间:2023-10-29 20:15
public static void main(String[] args) {
//总的鸡数量
int count = 100;
//总的钱
int money = 100;
//分别代表公鸡、母鸡、小鸡
int boy = 0, girl = 0, child = 0;
//分别代表公鸡、母鸡、小鸡的价格
double boyPrice = 5, girlPrice = 3, childPrice = 0.333333;
for (boy = 0; boy <= 100; boy++) {
for (girl = 0; girl <= 100; girl++) {
for (child = 0; child <= 100; child++) {
//当三种鸡的数量之和是100,且价格不超过100就符合
if ((boy + girl + child) == count
&& (boy * boyPrice + girl * girlPrice + child
* childPrice) < money) {
System.out.println(" 公鸡:" + boy + " 母鸡:" + girl
+ " 小鸡:" + child);
}
}
}
}
}
热心网友
时间:2023-10-29 20:16
package testProject;
import java.util.LinkedList;
import java.util.List;
public class Test {
public static final int CHICKEN_NUM = 100;
public static final int FUND_TOTAL = 100;
public static final int COCK_PRICE = 5;
public static final int HEN_PRICE = 3;
public static final int NUM_CHICK_PER_DOLLAR = 3;
public static void main(String args[]){
List<ResultSet> result = new LinkedList<ResultSet>();
int maxCockNum = FUND_TOTAL/COCK_PRICE;
int maxHenNum = FUND_TOTAL/HEN_PRICE;
for(int cockNum=0; cockNum <= maxCockNum; cockNum++){
for(int henNum=0; henNum <= maxHenNum; henNum++){
int remainingFund = FUND_TOTAL - cockNum*COCK_PRICE - henNum*HEN_PRICE;
int chickNum = remainingFund * NUM_CHICK_PER_DOLLAR;
int totalNumOfChicken = cockNum + henNum + chickNum;
if(totalNumOfChicken == CHICKEN_NUM){
ResultSet newSet = new ResultSet(cockNum, henNum, chickNum);
result.add(newSet);
}
}
}
System.out.println("All solutions:");
for(int i=0; i< result.size(); i++){
System.out.println(result.get(i).toString());
}
}
}
class ResultSet{
public ResultSet(int cockNum, int henNum, int chickNum){
this.cockNum = cockNum;
this.henNum = henNum;
this.chickNum = chickNum;
}
@Override
public String toString(){
return "公鸡: "+ cockNum + "只, 母鸡: "+ henNum + "只, 小鸡: " + chickNum + "只.";
}
public int cockNum;
public int henNum;
public int chickNum;
}