Hundred yuan and hundred chickens//Structural structure variables

Topic description One
hundred dollars and one hundred chickens. Each hen is 3 yuan, each rooster is 4 yuan, and each chick is 0.5 yuan. If you spend 100 yuan to buy 100 chickens, what are the possibilities? Try programming to output all combinations. [Note: The number of each type of chicken can be zero]
Input
no
output
Output purchase plan
Each plan output occupies one line, and each line is output in the order of hen, rooster, and chick,
and then the line is output according to the number of hens from small Large order output.
For example:
x1 x2 x3
y1 y2 y3
z1 z2 z3
where x1 <y1 <z1

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define ll long long
#define N 150
using namespace std;
typedef struct  //按照母鸡数量由小到大的次序输出,遇到这种适合用结构体变量接着sort排序,这题并不是很典型,ABC+DEF=GHI那题很典型
{
    
    
    int x,y,z;
}Node;
Node s[N];//注意开数组最后排序
int num=0;
bool cmp(Node a,Node b)//存在s数组中的元素就是Node类型,这样写即可
{
    
    
    return a.x<b.x;
}
void Find()
{
    
    
    int num1,num2,num3;
    for(num1=0;3*num1<=100;num1++)
        for(num2=0;4*num2<=100;num2++)
           for(num3=0;num3/2.0+num1*3+num2*4<=100;num3++)
              if(num1+num2+num3==100&&num1*3+num2*4+num3/2.0==100)
              {
    
    
                  s[num].x=num1;
                  s[num].y=num2;
                  s[num].z=num3;
                  num++;
              }
}
int main()
{
    
    
    Find();
    sort(s,s+num,cmp);//快速排序
    for(int i=0;i<num;i++)
        cout<<s[i].x<<" "<<s[i].y<<" "<<s[i].z<<endl;
    return 0;
}

Guess you like

Origin blog.csdn.net/m0_52380556/article/details/115261279