用小于等于n元去买100只鸡,大鸡5元/只,小鸡3元/只,还有1/3元每只的一种小鸡,分别记为x只,y只,z只。编程求解x,y,z所有可能解。
(本题没有测试数据,
int main(){} 就能通过 真·本地过了就是过了)
int main(){} 就能通过 测试数据有多组,输入n。
对于每组输入,请输出x,y,z所有可行解,按照x,y,z依次增大的顺序输出。
40
x=0,y=0,z=100 x=0,y=1,z=99 x=0,y=2,z=98 x=1,y=0,z=99
#include<iostream>
#include<cstring>
using namespace std;
int main() {
int n;
cin >> n;
for (int x = 0; x <=100; x++) {
for (int y = 0; y <=100 - x; y++) {
for (int z = 0; z <=100 - x - y; z++) {
if ((x * 5 + y * 3 + z *0.333 <= n)&&(x+y+z==100))
cout << "x=" << x << ",y=" << y << ",z=" << z << endl;
}
}
}
return 0;
} //点赞最多的解决double的问题真的是不错的方式,但其实用这个方法也不用存double
#include<iostream>
using namespace std;
int main(){
int n;
while(cin>>n){
int x,y;
for(int x=0;x<100;x++)
for(int y=0;y<100;y++){
if(x*5+y*3+(100-x-y)*((double)1/3)<=n)
cout<<"x="<<x<<",y="<<y<<",z="<<100-x-y<<endl;
else
break;
}
}
return 0;
} #include <stdio.h>
int main()
{
int i, j,n,left,a;
while(scanf("%d",&n) == 1)
{
//价格都假定为原来的三倍,n也乘3,如果全买小鸡的钱都不够就进行下次循环
if(3 * n < 100)
continue;
//否则打印信息
printf("x=0,y=0,z=100\n");
//left是假设全买小鸡剩下的钱,每再多8元,就可以换一只9元的小鸡
left = 3 * n - 100;
if(left < 8)
continue;
for(i = 1; i <= left / 8 && i <= 100;i++)
printf("x=0,y=%d,z=%d\n",i,100 - i);
//每多14元就可以换一只15元的小鸡
if(left < 14)
continue;
for(i = 1;i <= left / 14 && i <= 100;i++)
{
a = left - i*14;
for(j = 0;j <= a/8 && i + j <= 100;j++)
printf("x=%d,y=%d,z=%d\n",i,j,100-i-j);
}
}
} #include<stdio.h>
int main (){//the shorter,the better.
int n,i,j;
for(;~scanf("%d",&n);)
for (i = 0; i <= 100; i++)
for (j = 0; j <= 100-i;i*15+9*j+100-i-j > 3*n?:printf("x=%d,y=%d,z=%d\n",i,j,100-i-j),j++);
}
枚举法暴力求解,在for循环时取100和n元钱能买到的每一类鸡的最小值来限制循环次数
#include<iostream>
using namespace std;
const int BigChickenPrice = 5;
const int SmallChickenPrice = 3;
const double AnotherSmallChickenPrice = 1.0 / 3.0;
int Min(double a, double b) {
return(a < b) ? a : b;
}
int main() {
int n = 0;
cin >> n;
for (int i = 0; i <= Min(n / BigChickenPrice,100.0); ++i) {
for (int j = 0; j <= Min(n / SmallChickenPrice,100.0); ++j) {
for (int k = 0; k <= Min(n / AnotherSmallChickenPrice,100.0); ++k) {
if (5.0 * i + 3.0 * j + (1.0 / 3.0) * k <= n && i + j + k == 100) {
cout <<"x=" << i <<"," << "y=" << j << "," << "z=" << k << endl;
}
}
}
}
return 0;
} import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
for (int x = 0; x < 101; x++) {
for (int y = 0; y < 101; y++) {
for (int z = 0; z < 101; z++) {
if (x+y+z==100&&x*5+y*3+z/3<=n)
System.out.println("x="+x+",y="+y+",z="+z);
}
}
}
}
} import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
for (int x = 0; x <= 100; x++) {
for (int y = 0; y <= 100-x; y++) {
int z=100 -y-x;
if (x*5+y*3+z/3<=n)
System.out.println("x="+x+",y="+y+",z="+z);
}
}
}
}
#include <iostream>
(720)#include <cstdio>
using namespace std;
int main() {
int n;
int x, y, z;
while (scanf("%d", &n) != EOF) {
for ( x = 0; x <= n/5; x++) {
for ( y = 0; y <= n/3; y++) {
z = 100 - x - y;
if (x * 5 * 3 + y * 3 * 3 + z <= n * 3) {
printf("x=%d,y=%d,z=%d\n", x, y, z);
}
}
}
}
return 0;
} 并不是无脑循环,循环次数较少,这循环次数差不多是最少了,还能再优化吗?
def findHundredChicken(x,y,z,price): #递归增加小鸡,减少小小鸡,输出
if 5*x+3*y+1/3*z <= price: #价格超出后不再递归
print("x=%d,y=%d,z=%d"%(x,y,z))
findHundredChicken(x,y+1,z-1,price)
try:
while True:
num = int(input())
for i in range(101): #在这里循环到大鸡最多能买的个数
if (num-i*5)*3<100-i:
break
findHundredChicken(i,0,100-i,num) #始终保持百鸡
except Exception:
pass
#include <iostream>
using namespace std;
int main() {
float n;
while (cin >> n) {
for (int x = 0; x <= 100; x++) {
for (int y = 0; y <= 100 - x; y++) {
int z = 100 - x - y;
if (x * 5 + y * 3 + z * 1.0 / 3 <= n) {
cout << "x=" << x << ",y=" << y << ",z=" << z << endl;
}
}
}
}
return 0;
}