首页 > 专题系列 > Java解POJ > POJ 1320 Street Numbers [解题报告] Java
2013
11-09

POJ 1320 Street Numbers [解题报告] Java

Street Numbers

问题描述 :

A computer programmer lives in a street with houses numbered consecutively (from 1) down one side of the street. Every evening she walks her dog by leaving her house and randomly turning left or right and walking to the end of the street and back. One night she adds up the street numbers of the houses she passes (excluding her own). The next time she walks the other way she repeats this and finds, to her astonishment, that the two sums are the same. Although this is determined in part by her house number and in part by the number of houses in the street, she nevertheless feels that this is a desirable property for her house to have and decides that all her subsequent houses should exhibit it.

Write a program to find pairs of numbers that satisfy this condition. To start your list the first two pairs are: (house number, last number):
         6         8

35 49

输入:

There is no input for this program.

输出:

Output will consist of 10 lines each containing a pair of numbers, in increasing order with the last number, each printed right justified in a field of width 10 (as shown above).

样例输入:


样例输出:

         6         8
        35        49

解题代码:

/* @author:zeropinzuo */
import java.io.*;
import java.util.*;

public class Main{
 public static void main(String args[]){
   int[] array = new int[10];
   array[0]=6;
   array[1]=35;
		
   for(int i=2;i< 10;i++)
    array[i] = 6*array[i-1]-array[i-2];
			
   for(int i=0;i< 10;i++){
	//System.out.println("  "+array[i]+"   "+(Math.sqrt(8*array[i]*array[i]+1)-1)/2);
   }
		
   System.out.println("         "+6+"         "+8);
   System.out.println("        "+35+"        "+49);
   System.out.println("       "+204+"       "+288);
   System.out.println("      "+1189+"      "+1681);
   System.out.println("      "+6930+"      "+9800);
   System.out.println("     "+40391+"     "+57121);
   System.out.println("    "+235416+"    "+332928);
   System.out.println("   "+1372105+"   "+1940449);
   System.out.println("   "+7997214+"  "+11309768);
   System.out.println("  "+46611179+"  "+65918161);
  }
}

  1. 第23行:
    hash = -1是否应该改成hash[s ] = -1

    因为是要把从字符串s的start位到当前位在hash中重置

    修改提交后能accept,但是不修改居然也能accept

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

  3. 思路二可以用一个长度为k的队列来实现,入队后判断下队尾元素的next指针是否为空,若为空,则出队指针即为所求。

  4. Thanks for taking the time to examine this, I really feel strongly about it and love studying a lot more on this topic. If possible, as you acquire experience