设为首页 加入收藏

TOP

策略模式-----C++实现(二)
2014-02-14 12:52:26 来源: 作者: 【 】 浏览:374
Tags:策略 模式 -----C 实现

 

  Output

  For each set of products, the program prints on the standard output the profit of an optimal selling schedule for the set. Each result is printed from the beginning of a separate line.

  Sample Input

  4 50 2 10 1 20 2 30 1

  7 20 1 2 1 10 3 100 2 8 2

  5 20 50 10

  Sample Output

  80

  185

  Hint

  The sample input contains two product sets. The first set encodes the products from table 1. The second set is for 7 products. The profit of an optimal schedule for these products is 185.

  #include

  #include

  #include

  #include

  #include

  #include

  using namespace std;

  struct node

  {

  int p, d;

  } fei[10001];

  int a[10001];

  bool operator < (const node& a, const node& b)

  {

  return a.p > b.p;

  }

  int main()

  {

  int n, i, j, s, x;

  while(scanf("%d",&n)!=EOF)

  {

  s = 0;

  memset(a,0,sizeof(a));

  for(i=0; i

  scanf("%d%d",&fei[i].p,&fei[i].d);

  sort(fei,fei+n);

  for(i=0; i

  {

  x=fei[i].d;

  if(a[x]==0)

  {

  s = s + fei[i].p;

  a[x]++;

  }

  else

  {

  for(j=x-1; j>=1; j--)

  {

  if(a[j]==0)

  {

  s=s+fei[i].p;

  a[j]++;

  break;

  }

  }

  }

  }

  printf("%d\n",s);

  }

  return 0;

  }

      

首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇vc6做对对碰外挂原理 下一篇c++概率的运用,基于控制台程序

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·HTTPS 详解一:附带 (2025-12-26 02:20:37)
·TCP/IP协议到底在讲 (2025-12-26 02:20:34)
·TCP和UDP在socket编 (2025-12-26 02:20:32)
·有没有适合新手练习 (2025-12-26 01:48:47)
·用清华镜像网怎么下 (2025-12-26 01:48:44)