公元五世纪,我国古代数学家张丘建在《算经》一书中提出了“百鸡问题”:鸡翁一值钱五,鸡母一值钱三,鸡雏三值钱一。百钱买百鸡,问鸡翁、鸡母、鸡雏各几何?
现要求你打印出所有花一百元买一百只鸡的方式。
输入任何一个整数,即可运行程序。
输出有数行,每行三个整数,分别代表鸡翁,母鸡,鸡雏的数量
1
0 25 75 4 18 78 8 11 81 12 4 84
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { int num = sc.nextInt(); int n1 = 0; // 鸡翁个数 int n2 = 0; // 鸡母个数 int n3 = 3; // 鸡雏个数 for(n1 = 0; n1 < 20; n1++){ for(n2 = 0; n2 < 33; n2++){ for(n3 = 0; n3 < 100; n3 = n3 + 3){ if(n1 + n2 + n3 == 100 && 5*n1 + 3*n2 + n3/3 == 100){ System.out.println(n1 + " " + n2 + " " + n3); } } } } } sc.close(); } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws IOException { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); String s = ""; while ((s = bf.readLine()) != null){ int num = Integer.parseInt(s); int x,y; for (int z = 0 ; z<=100; z +=3){ x = 4*z/3-100; y = 200-7*z/3; if ( (x>=0)&&(x<=100) &&(y>=0)&&(y<=100)){ System.out.println(x + " " + y + " " + z ); } } } } }
for i in range(0,25): y = (100 - 7*i)//4 if(((7*i + 4*y) == 100) and (y > 0)): print(str(i)+' '+str(y)+' '+str(100 - y - i))
import java.io.*; public class Main{ public static void main(String[] args)throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str; while((str=br.readLine())!=null){ StringBuilder sb = new StringBuilder(); // 设公鸡X 母鸡Y 小鸡(100-X-Y) 则 5X+3Y+1/3(100-X-Y)=100 化简得 7X+4Y=100; for(int i=0;i<=13;i++){ for(int j=0;j<=25;j++){ if(7*i+4*j==100){ sb.append(i).append(" ").append(j).append(" ").append(100-i-j).append("\n"); break; } } } // 设公鸡X 母鸡Y 小鸡(100-X-Y) 则 5X+3Y+1/3(100-X-Y)=100 化简得 7X+4Y=100; Y=(100-7X)/4 // for(int i=0;i<=14;i++){ // int y = (100-7*i)/4; // if(7*i+4*y==100){ // sb.append(i).append(" ").append(y).append(" ").append(100-i-y).append("\n"); // } // } System.out.print(sb.toString()); } } }
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNext()){ int num = sc.nextInt(); // System.out.println("0 25 75"); // System.out.println("4 18 78"); // System.out.println("8 11 81"); // System.out.println("12 4 84"); // for(int x = 0;x <= 3;x++){ System.out.println((4*x) + " " + (25-7*x) + " " + (3*x+75)); } } sc.close(); } }
#include<iostream> #include<stdio.h> #include<math.h> #include<algorithm> using namespace std; int main() { int n; cin>>n; for(int i=0;i<=100/5+1;++i) { for(int j=0;j<=(100-5*i)/3+1;++j) { for(int k=0;k<=(100-5*i-3*j)*3;k+=3) { if(5*i+3*j+k/3==100 && i+j+k==100) { cout<<i<<" "<<j<<" "<<k<<endl; } } } } return 0; }
#include <iostream> using namespace std; //计算 //5x+3y+1/3z = 100 //x+y+z = 100 //得出 7x+4y = 100 0<=x<=14 0<=y<=25 int main() { int n; while(cin>>n) { for(int i = 0;i<=14;i++) { for(int j = 0;j<=25;j++) { if(7*i+4*j == 100) { int tmp = 100 -i - j; cout<<i<<" "<<j<<" "<<tmp<<endl; } } } } return 0; }
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { int n = sc.nextInt(); List<String> s = new ArrayList<String>(); for(int i=0; i<=100; i++) { for(int j=0; j<=100; j++) { int k = 100-i-j; if(k%3==0 && 5*i+3*j+k/3==100) { s.add(i+" "+j+" "+k); } } } for(String ss : s) { System.out.println(ss); } } } }
三元不定方程组,消元后用一个参数计算另外另个,并且通过解应为正整数,判断结果是否符合要求。
#include<iostream> #include<vector> #include<tuple> using namespace std; int main() { int num = 0; while (cin >> num) { vector<tuple<int,int,int>> res; for (int i = 0; i <= 20; i++) for (int j = 0; j <= (100 - 5 * i) / 3; j++) for (int k = 0; k <= 3 * (100 - 5 * i - 3 * j); k += 3) if (i + j + k == 100 && 5 * i + 3 * j + k / 3 == 100) res.push_back(make_tuple(i, j, k)); for (auto t : res) cout << get<0>(t) << " " << get<1>(t) << " " << get<2>(t) << endl; } }
import java.util.Scanner; public class Demo32 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while(sc.hasNext()){ int num = sc.nextInt(); calculate(); } sc.close(); } public static void calculate(){ for(int i=0;i<=20;i++){ for(int j=0;j<=33;j++){ for(int k=0;k<=300;k++){ if(15*i+9*j+k==300&&i+j+k==100){ System.out.println(i+" "+j+" "+k); } } } } } }
老铁们,我做的对吗
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
while(sc.hasNext()){
int num=sc.nextInt();
System.out.println(0+" "+25+" "+75);
System.out.println(4+" "+18+" "+78);
System.out.println(8+" "+11+" "+81);
System.out.println(12+" "+4+" "+84);
} }