2018大华软件大赛模拟赛第4题 (某股票操盘手账户里有N支股票,股价互不等)

这道题是个秤砣砝码的问题(动态规划) 

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 
  4 int fun(int n, int money[], int num[])
  5 {
  6     int allmoney = 0 , i , j;
  7     for(i = 0; i < n; i++)
  8     {
  9         allmoney = allmoney + money[i] * num[i]; //求所有的资金量
 10     }
 11 
 12     int flag[10000] = {0}; //判断存在的资金量
 13     
 14     flag[allmoney] = 1;
 15     //先计算第0种股票能够得到的资金里量,并且计算0种股票最大的资金量
 16     int tempmoney = 0;
 17     for(i = 0; i <= num[0]; i++)
 18     {
 19         flag[money[0] * i] = 1;
 20     }
 21     tempmoney = money[0] * num[0];
 22 
 23     i = 1; //第1种股票
 24     int currentmoney;
 25     int newmoney;
 26 
 27     while(i < n)
 28     {
 29         int f[10000] ={0};
 30         for(j = 1; j <= num[i]; j++)
 31         {
 32             //采用试探的方法 逐次加1
 33             for(currentmoney = 0; currentmoney <= tempmoney; currentmoney++)
 34             {
 35                 newmoney = currentmoney + j * money[i];
 36                 if(newmoney > allmoney)
 37                 {
 38                     break;
 39                 }
 40                 if(flag[currentmoney]  && flag[newmoney] == 0
 41                 &&    currentmoney != money[i])
 42                 {
 43                     f[newmoney] =1;
 44                 }
 45             }
 46         }
 47         //更新已经存在的资金量
 48         tempmoney = tempmoney + num[i] * money[i];
 49         for(int p = 0; p <= tempmoney; p++)
 50         {
 51             if(f[p])
 52             {
 53                 flag[p] = f[p];
 54             }
 55         }
 56         i++;
 57     }
 58 
 59     //统计数量
 60     int count = 0;
 61     for(i = 0; i < 10000; i++)
 62     {
 63         if(flag[i] == 1)
 64         {
 65             count++;
 66         }
 67     }
 68         return count;
 69 }
 70 
 71 int main()
 72 {
 73     int n1, n2;
 74     //int a[20] = {0};
 75     //int b[20] = {0};
 76 
 77     scanf("%d",&n1);
 78     for(int k =0; k < n1; k++ )
 79     {
 80         int count = 0;
 81         scanf("%d",&n2);
 82         int *a = (int *) calloc(n2,sizeof(int));
 83         int *b = (int *) calloc(n2,sizeof(int));
 84 
 85         for(int i = 0; i < n2; i++)
 86         {
 87             scanf("%d",&a[i]);
 88         }
 89 
 90         for(int i = 0; i < n2; i++)
 91         {
 92             scanf("%d",&b[i]);
 93         }
 94         for(int i =0; i < n2 -1; i++)
 95         {
 96             for(int j = 0; j < n2-i -1; j++)
 97             {
 98                 if(a[j] > a[j+1])
 99                 {
100                     int t = a[j];
101                     a[j] = a[j+1];
102                     a[j+1] = t;
103                     
104                     t = b[j];
105                     b[j] = b[j+1];
106                     b[j+1] =b[j];
107                 }
108             }
109         }
110         count = fun(n2,a,b);
111         printf("%d\n",count);
112     }
113 
114     return 0;
115 }

猜你喜欢

转载自www.cnblogs.com/leezheng/p/8971991.html