首页 > 专题系列 > Java解POJ > POJ 2209 The King [解题报告] Java
2013
11-10

POJ 2209 The King [解题报告] Java

The King

问题描述 :

Once upon a time in a country far away lived a king and he had a big kingdom. He was a very clever king but he had one weakness — he could count only up to three.

Nevertheless, he did not consider this to be a really great drawback, since he had a lot of wizards who could count up to one hundred (and some of them, people said, even up to one thousand), so it was all right. But one day the grief came to the kingdom as the outnumbering barbarians started to approach from all sides. And the king then had to make the most important decision in his life. He had to choose which of his sons to make generals that he would send to the borders of the country to lead the army.

However, the king knew that though some of his sons were clever, just like he was, some of them were quite stupid and could only lower army spirits with their wrong decisions. More precisely, he knew about each of his sons his mental potential — an integer number ranging from minus three to three (remember, that the king could count only up to three). He also knew that the chance of his army defeating barbarians was proportional to the sum of some powers of mental potentials of those of his sons that he would make generals (the power exponent was a positive integer number, the same for all his sons and not exceeding three either). Thus he had to choose such a combination of his sons to lead

the army, that this sum would be maximal possible.

However, the king himself could not make all apropriate calculations since, for example, the second power of the number not exceeding three (which is its square) could be greater than three, and therefore he asked you, his most intellegent wizard, to solve this problem.

输入:

The first line of the input file contains the number of the sons of the king (integer number less or equal to one hundred). The second line contains the positive integer number not exceeding three, the exponent in the formula used to calculate the chance of defeating barbarians. The third line contains the list of mental potentials of king’s sons — all integer numbers, not greater than three by their absolute value.

输出:

Output the only number — the maximal possible chance of defeating barbarians measured as the sum described.

样例输入:

3
3
2 -1 1

样例输出:

9

温馨提示:

In the example above the king should choose his first and third sons to be the generals. In this case the chance to defeat barbarians, which is the sum of cubes of mental potentials of these sons, is eight plus one, that is nine.

解题代码:

//* @author 洪晓鹏<[email protected]>
import java.util.Scanner;


public class Main {
 public static void main(String[] args)
 {
	Scanner in = new Scanner(System.in);
	int numOfSon = in.nextInt();
	int power = in.nextInt();
	int[] potential = new int[numOfSon];
	int sum = 0;
	if(power%2==1)
	{
         for(int i = 0; i< numOfSon; i++)	
	  {
		potential[i] = in.nextInt();
		if(potential[i]>0)
			sum = (int) (sum + Math.pow(potential[i],power));		
	  }
	}
	else
	{
	  for(int i = 0; i< numOfSon; i++)	
	{
	potential[i] = in.nextInt();
	sum = (int) (sum + Math.pow(potential[i],power));		
       }
    }
   System.out.println(sum);
  }
}

  1. 第二个方法挺不错。NewHead代表新的头节点,通过递归找到最后一个节点之后,就把这个节点赋给NewHead,然后一直返回返回,中途这个值是没有变化的,一边返回一边把相应的指针方向颠倒,最后结束时返回新的头节点到主函数。

  2. 站长好。我是一个准备创业的互联网小白,我们打算做一个有关国*际*游*学的平台。手上也有了一些境外资源。现阶段的团队现在没有cto.原意出让一些管理股寻找一个靠谱的技术专家做合伙人, 不知道是不是能得到您的帮助。发个帖子或者其他方式。期待您的回应。可以加我微信tianxielemon聊聊。