【备战2014笔面试】华为机考-X人共花费Y元的方案

这个题目比较简单,主要考察的是循环的嵌套与控制

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>


/*
问题说明:

X人共花费Y元的方案,其中男人3元/人,女人2元/人,小孩1元/人。男人、女人、小孩至少各一人,
计算一共有多少种方案组合。

输入:
3 6
输出:
1
*/


int main(void)
{
	int TotalCost,NumPeo;
	printf("input NumPeo, TotalCost:");
	scanf("%d %d",&NumPeo,&TotalCost);
	if (NumPeo<3||TotalCost<6)
	{
		printf("%d",0);
		//return 1;
	}
	else if (NumPeo==3&&TotalCost==6)
	{
		printf("%d",1);
		//return 1;
	}
	else
	{
		int mal = 1;
		int fem = 1;
		int chi = 1;
		
		int rest = TotalCost;
		
		int numPlan = 0;
		
		for(mal = 1;rest>=0;mal++)
		{
                rest = rest-3;
                                
                for(fem = 1;rest>=0;fem++)
                {
                        rest = rest - 2;
                        
                        for(chi = 1;rest>=0;chi++)
                        {
                                rest = rest - 1;
                                
                                if(((mal+fem+chi)==NumPeo)&&(rest>=0))
                                {
                                       numPlan++;
                                }
                        }
                }
          }		
	  
          printf("%d\n",numPlan);
        
	}
	
	getch();
	return 0;
	
	
}


你可能感兴趣的:(C++)