zoukankan      html  css  js  c++  java
  • 1000元买物品分配

     1 #include<stdio.h>
     2 #include<malloc.h>
     3 #include <stdlib.h>
     4 
     5 
     6 //测试 2种商品 30 40
     7 
     8 int main()
     9 {
    10     int m,sum;
    11     printf("请输入您得商品数
    ");
    12     scanf("%d",&m);//m种商品
    13     int *p=(int*)malloc(sizeof(int)*m);//存储价格
    14     int *i=(int*)malloc(sizeof(int)*m);//存放方案
    15     int *max=(int*)malloc(sizeof(int)*m);
    16     int count=0;//记录方案总数
    17     for(int j=0;j<m;j++)
    18     {
    19         i[j]=0;//初始化方案数
    20         printf("请输入第%d个商品价格
    ",j+1);
    21         scanf("%d",p+j);//输入存如价格
    22         //    scanf("%d",p+j);
    23         max[j]=1000/p[j];
    24         printf("max[%d]=%d
    ",j,max[j]);
    25     }
    26     while(i[0]<=max[0]) 
    27     {
    28         for(int j=m-1;j>=0;j--)
    29         {
    30             i[j]++;
    31             if(i[j]>max[j]&&j) i[j]=0;
    32             else break;
    33         }
    34         sum=0;
    35         for(j=0;j<m;j++) sum+=p[j]*i[j];//计算 第一轮第一种商品价格为0 另外为40需要25个才能满足 正好就用i[j]自增来实现
    36         if(sum==1000)
    37         {
    38             count++;
    39             for(int j=0;j<m;j++) printf("%-4d",i[j]);//40*1 40*2
    40             putchar('
    ');
    41         }
    42     } 
    43     printf("总共有 %d 方案
    ",count);
    44     system("pause");
    45     return 0;
    46 }

  • 相关阅读:
    扫雷游戏
    打地鼠Demo
    Game2048
    蛇形矩阵
    约瑟夫环
    二分法查找
    动态规划之防卫导弹
    动态规划之0-1背包问题
    回溯算法之火力网
    回溯算法之8皇后问题
  • 原文地址:https://www.cnblogs.com/lanjianhappy/p/6486913.html
Copyright © 2011-2022 走看看