不知为毛,过不了

我的代码:

#include<stdio.h>

int main()
{
 int n,a[30],b[30],temp,i,j,s1,s2;
 double c[30];
 while(scanf("%d",&n)!=EOF)
 {
  s1=0;
  s2=0;
  for(i=0;i<n;i++)
  {
   scanf("%d%d",&a[i],&b[i]);
   s1+=a[i];
   c[i]=a[i]/b[i];
  }
     for(i=0;i<n;i++)
  {
   for(j=i+1;j<n;j++)
   {
    if(c[i]<c[j])
    {
     temp=c[i];
     c[i]=c[j];
     c[j]=temp;
     temp=b[i];
     b[i]=b[j];
     b[j]=temp;
     temp=a[i];
     a[i]=a[j];
     a[j]=temp;
    }
   }
  }
  for(i=0;i<n;i++)
  {
      s2+=s1*b[i];
   s1-=a[i];
  }
  printf("%d\n",s2);
 }
}

Ac的代码:

  1. #include <stdio.h>
  2. #include <algorithm>
  3. using namespace std;
  4. struct Node
  5. {
  6. int dps;
  7. int hp;
  8. }a[30];
  9. int cmp(Node x,Node y)
  10. {
  11. return x.hp*y.dps<y.hp*x.dps;//按比率排,避免小数,所以讲式子进行转换
  12. }
  13. int main()
  14. {
  15. int t,i;
  16. int sum,ans;
  17. while(~scanf("%d",&t))
  18. {
  19. sum = ans = 0;
  20. for(i = 0;i<t;i++)
  21. {
  22. scanf("%d%d",&a[i].dps,&a[i].hp);
  23. sum+=a[i].dps;
  24. }
  25. sort(a,a+t,cmp);
  26. for(i = 0;i<t;i++)
  27. {
  28. ans+=sum*a[i].hp;
  29. sum-=a[i].dps;
  30. }
  31. printf("%d\n",ans);
  32. }
  33. return 0;
  34. }
  35. 有毛区别

hdoj 4310 贪心的更多相关文章

  1. HDU 4310 贪心

    题意 在游戏中你的dps为1但是hp无限 给出n个敌人的dps与hp 你一秒能打掉一个敌人你的dps的hp 当你输出的时候 所有活着的敌人都会打你 求杀死所有敌人时你掉的最少hp 一开始想错了 排序的 ...

  2. HDOJ 2209 贪心

    翻纸牌游戏 Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  3. HDU4310HERO贪心问题

    问题描述 When playing DotA with god-like rivals and pig-like team members, you have to face an embarrass ...

  4. HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  5. HDU 4310 Hero (贪心算法)

    A - Hero Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Sta ...

  6. 贪心 HDOJ 4726 Kia's Calculation

    题目传送门 /* 这题交给队友做,做了一个多小时,全排列,RE数组越界,赛后发现读题读错了,囧! 贪心:先确定最高位的数字,然后用贪心的方法,越高位数字越大 注意:1. Both A and B wi ...

  7. BFS+贪心 HDOJ 5335 Walk Out

    题目传送门 /* 题意:求从(1, 1)走到(n, m)的二进制路径值最小 BFS+贪心:按照标程的作法,首先BFS搜索所有相邻0的位置,直到1出现.接下去从最靠近终点的1开始, 每一次走一步,不走回 ...

  8. [HDOJ - 5208] Where is Bob 【DFS+按位贪心】

    题目链接:HDOJ - 5208 题目分析 使用按位贪心的思想,即从高位向低位枚举,尽量使这一位的答案为 1 . 我们使用 DFS ,每次就是对于  [l1, r1] [l2, r2] x  进行处理 ...

  9. HDOJ(HDU).1864 最大报销额 (贪心)

    HDOJ(HDU).1864 最大报销额 题意分析 题目有点问题,原题中说的 单项物品的价值不得超过600元 应该是单类物品的价值不能超过600元. 一开始以为是01背包,后来按贪心写过了. 一张一张 ...

随机推荐

  1. Unity 的“Vertex Lit Rendering path“中 shader Pass 的注意事项

    "MADFINGER/Environment/Unlit (Supports Lightmap)"是 ShadowGun 示例中最简单的 shader 了,如下: // Unlit ...

  2. HDOJ1518Square 深搜

    Square Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  3. (经常看看)jdk 设计模式

    在JDK(Java Development Kit)类库中,开发人员使用了大量设计模式,正因为如此,我们可以在不修改JDK源码的前提下开发出自己的应用软件,本文列出了部分JDK中的模式应用实例,有兴趣 ...

  4. lightoj 1018 dp

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1018 #include <cstdio> #include <cst ...

  5. C#三种判断数据库中取出的字段值是否为空(NULL) 的方法

    操作数据库,需要判断返回的字段值是否为空,收集了3种方法供参考 1 通过System.DBNull判断,网上大部分都使用这个方法. DataTable dt;                     ...

  6. wdlinux mysql innodb的安装

    mysql innodb的安装 wget -c http://down.wdlinux.cn/in/mysql_innodb_ins.sh chmod 755 mysql_innodb_ins.sh ...

  7. [Javascript] Using JSHint for Linting with Gulp

    gulpfile.js var gulp = require('gulp'); var jshint = require('gulp-jshint'); var stylish = require(' ...

  8. c++ 文件写样例

    #include <iostream> #include <sstream> #include <fstream>> using namespace std; ...

  9. 将html中的br换行符转换为文本输入中的换行符(转)

    PHP中的有个非常好的函数:nl2br(),将文本框中的换行转换为HTML页面的<br />,但是如何实现将html中的<br />换行符转换为文本框中的换行符呢?下面这几个方 ...

  10. Bit data type

    mysql> create table t(a bit()); Query OK, rows affected (0.04 sec) mysql> insert into t '; Que ...