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


    题目:某股票操盘手账户里有N支股票,股价互不等,分别为v1,v2...vn;每支股票的持有股数为m1,m2...mn。

       现在操盘手要回笼资金需要卖出股票,假设卖出价格即为当前股价,请问能回笼多少种不同的资金量。

              比如:两支股票,股价分别为10、11,数量为1、2,则能回笼0、10、11、22、21、32,总共6种资金量

    输入:输入的第一行指定用例数量T;
          用例的第一行输入股票种类n;
       用例的第二行输入股票的价格,以空格隔开;
       用例的第三行输入股票的数量,已空格隔开;

    输出:输出不同资金量的个数

    限制: 1<=n<=10
          1<=v<=20
        1<=m<=6

    Input:
    1
    2
    10 11
    1 2

    Output:
    6

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

      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
    ",count);
    112     }
    113 
    114     return 0;
    115 }
  • 相关阅读:
    MyEclipse 使用快捷键
    修改MyEclipse默认的Servlet和jsp代码模板
    设置MyEclipse开发项目时使用的JDK
    65.广搜练习:细胞数目
    65.广搜练习:细胞数目
    61.新的开始(最小生成树)
    61.新的开始(最小生成树)
    66.广搜练习:最少关卡路
    66.广搜练习:最少关卡路
    64.广搜练习跳马问题
  • 原文地址:https://www.cnblogs.com/leezheng/p/8971991.html
Copyright © 2020-2023  润新知