各位大佬救救孩子吧
  • 板块学术版
  • 楼主月下纯春
  • 当前回复0
  • 已保存回复0
  • 发布时间2023/10/15 19:48
  • 上次更新2023/11/2 13:34:39
查看原帖
各位大佬救救孩子吧
52153
月下纯春楼主2023/10/15 19:48

某程序猿有100元、50元、20元、10元、5元、1元纸币各a、b、c、d、e、f张,欲购买价值为x元的商品。请给出支付方案,使得在满足找零钱币张数最少的情况下,支付钱币张数也尽可能少。 数据范围 int 输入格式 a b c d e f total 输出格式 denomination1:amount1 denomination2:amount2 (意思是每行给出一个面额和它的张数,面额从大到小排列)

#include <stdio.h> 
#include<math.h> 
int s=0; int main()
{ 
   int a, b, c, d, e, f, total,m=0; 
   scanf("%d%d%d%d%d%d%d", &a, &b, &c, &d, &e, &f, &total); 
   m=total;
   int value[6] = {100, 50, 20, 10, 5, 1};
   int count[6] = {a, b, c, d, e, f};
   int pay[6] = {0};

   for (int i = 0; i < 6; i++) 
   {
   		while (total >= value[i] && count[i] > 0) 
   	{
       		total -= value[i];
       		count[i]--;
       		pay[i]++;
   	 	 }
     }
   for(int i=0;i<6;i++)
   s+=value[i]*pay[i];



   if(s<m) 
   {
   	for(int i=6;i>=0;i--)
   	{
   		if((m-s)>value[i]&&value[i-1]>(m-s)&&count[i-1]>0) 
   		{
   		
   			while(s<m)
   			{
   			pay[i-1]++; 
   			s+=value[i-1];
   		 	}
   		  }
   	  }
}


if(s>m)
{
   
   for(int i=6;i>0;i--)
   {
   	while(s>m&&(s-value[i])>m&&pay[i]>=0)
   	{
   		//printf("%d\n",s);
   		pay[i]--;
   		s-=value[i];
   	}
   	
   } 
}



for (int i = 0; i < 6; i++) 
{
   if (pay[i] > 0) 
   {
       printf("%d:%d\n", value[i], pay[i]);
  	}
}

return 0;
} 

有一个点过不去,请问是哪里有问题啊

2023/10/15 19:48
加载中...