HDU4310_acm_Hero

来源:互联网 发布:矩阵秩的性质 编辑:程序博客网 时间:2024/06/06 01:11

Problem Description
When playing DotA with god-like rivals and pig-like team members, you have to face an embarrassing situation: All your teammates are killed, and you have to fight 1vN.

There are two key attributes for the heroes in the game, health point (HP) and damage per shot (DPS). Your hero has almost infinite HP, but only 1 DPS.

To simplify the problem, we assume the game is turn-based, but not real-time. In each round, you can choose one enemy hero to attack, and his HP will decrease by 1. While at the same time, all the lived enemy heroes will attack you, and your HP will decrease by the sum of their DPS. If one hero's HP fall equal to (or below) zero, he will die after this round, and cannot attack you in the following rounds.

Although your hero is undefeated, you want to choose best strategy to kill all the enemy heroes with minimum HP loss.
 

Input
The first line of each test case contains the number of enemy heroes N (1 <= N <= 20). Then N lines followed, each contains two integers DPSi and HPi, which are the DPS and HP for each hero. (1 <= DPSi, HPi <= 1000)
 

Output
Output one line for each test, indicates the minimum HP loss.
 

Sample Input
110 22100 11 100
 

Sample Output
20201

本题提供两种语言的解决方案,首先参考网上的C代码写出了注释版的C代码,再把它转化为了Java代码。

在转化中发现C与Java的区别还是挺大的,但是既然为一个算法题,能用C解决的就一定也能用Java,只不过是实现的方法不尽相同罢了,原理还是共通的。

C中使用结构体的方法定义了Hero的数据类型,而Java中是使用类Class的方法定义Hero;

C中排序的方法是用类库中的sort()函数,而Java则需要实现Comparator接口了,通过Arrays中的sort()方法对对象排序;

在排序函数/方法的编写中,两者的区别在于C中使用指针来传参数,Java通过中间变量Object 传递,返回的类型与计算方法一样。


本题的方法: 先对Hero对象数组进行排序,按照优先攻击的顺序排序。 排序方法是 DPSi / HPi 的大小,越大的攻击优先级越高。

对结构体或者Java中的对象进行排序,可以使用类库中带的排序方法。对于C是sort ,对于Java则是调用Arrays中的sort。

其中排序的规则,C是自己写一个函数作为sort的参数即可,Java需要实现comparator接口,再将其作为参数传递到sort即可。

排序完成三个for循环,第一个循环  从0-N 把所有存活的Hero循环一遍

第二个循环  把所有HP>0的Hero.HPi 减 1 循环

第三个循环  每次把剩下的Hero(即第一个循环的 i 变量)

最后把每次造成的伤害累加到sum中

//快速排序 四个参数//1.待排序数组首地址//2.数组中待排序元素数量//3.各元素占用空间大小//4.指向函数的指针,用于确定排序的顺序//对结构体进行排序#include<stdio.h>#include<stdlib.h>struct In{double bi;int dps;int hp;}hero[100];int cmp(const void *a,const void *b){//比较函数struct In *c = (In*)a;struct In *d = (In*)b;/*(a->dps / a->hp) - (b->dps / b->hp) a->dps * b->hp - b->dps * a->hp-------------------------------- a->hp * b->hp计算机除法会出现数据不准确,不可预知的错误所以改成乘法可以正确运行*/return (d->dps * c->hp - c->dps * d->hp);}int main(){int n,i,j,k;int sum;while (scanf("%d",&n) != EOF ){ //要输入的数据条数for(i=0; i<n; i++){scanf("%d %d",&hero[i].dps,&hero[i].hp);//转换成浮点数//hero[i].bi = hero[i].dps * 1.000 / hero[i].hp * 1.000;}qsort(hero, n, sizeof(hero[0]),cmp); sum = 0;for(i=0; i<n; i++){//输入的n个数据for(j=hero[i].hp; j>0; j--){//每个数据的 生命值减一枚举for(k=i; k<n; k++){//i--n 所有存活的伤害累加sum = sum + hero[k].dps;}//for3}//for2}//for1//输出伤害printf("%d\n",sum);}//end whilereturn 0;}//end main
JAVA代码
import java.util.Scanner;import java.util.ArrayList;import java.util.Arrays;import java.util.Comparator;public class Main {        public static void main(String[] args) {    //输入    Scanner sc = new Scanner(System.in);   int N = sc.nextInt();    Hero[] hero = new Hero[N];    ArrayList arrayList = new ArrayList();    for(int i=0; i<N; i++){    hero[i] = new Hero();    hero[i].DPSi = sc.nextInt();     hero[i].HPi  = sc.nextInt();    arrayList.add(hero[i]);    }        //排序    Arrays.sort(hero,0,N-1,new Com_hero());        //计算输出    int DSP_Count = 0;    for(int i=0; i<N; i++){    //    for( ; hero[i].HPi>0; hero[i].HPi --){    //    for(int k=i; k<N; k++){    DSP_Count += hero[k].DPSi;    }    }    }    System.out.println(DSP_Count);        }}//对象声明class Hero{int HPi;int DPSi;Hero(){}}class Com_hero implements Comparator{public int compare(Object com1,Object com2){Hero h1 = (Hero)com1;Hero h2 = (Hero)com2;if(h1.DPSi * h2.HPi < h2.DPSi * h1.HPi )return 1;else return -1;}}




#include"stdio.h"#include"string.h"#include"stdlib.h"struct A{int t,v;}E[30];int cmp(const void *a,const void *b){A *c,*d;c=(A *)a;d=(A *)b;return (c->t*d->v)-(d->t*c->v);}int main(){int n;int i,l;int base,ans;while(scanf("%d",&n)!=-1){base=0;for(i=0;i<n;i++){scanf("%d%d",&E[i].v,&E[i].t);base+=E[i].v;}qsort(E,n,sizeof(E[0]),cmp);ans=0;for(i=0;i<n;i++){ans+=base*E[i].t;base-=E[i].v;}printf("%d\n",ans);}return 0;}


1 0
原创粉丝点击