首页 > 专题系列 > Java解POJ > POJ 1965 Cube Root [解题报告] Java
2013
11-10

POJ 1965 Cube Root [解题报告] Java

Cube Root

问题描述 :

Your task is to calculate the cube root of a given positive integer. We can not remember why exactly we need this, but it has something in common with a princess, a young peasant, kissing and half of a kingdom (a huge one, we can assure you).

Write a program to solve this crucial task.

输入:

The input consists of large positive integers of up to 150 decimal digits.Each number is on its own separate line of the input file. The input file may contain empty lines. Numbers can be preceded or followed by whitespaces but no line exceeds 255 characters.

输出:

For each number in the input file your program should output a line consisting of two values separated by single space. The second value is the cube root of the given number, truncated (not rounded!) after the 10th decimal place. First value is a checksum of all printed digits of the cube root, calculated as the sum of the printed digits modulo 10.

样例输入:

1
   	8

1000

     2
33076161

样例输出:

1 1.0000000000
2 2.0000000000
1 10.0000000000
0 1.2599210498
6 321.0000000000

解题代码:

//* @author  [email protected]
import java.math.BigDecimal;
import java.util.Scanner;
public class Main {
 public static void main(String[] args) {		
 Scanner scan=new Scanner(System.in);
  BigDecimal k;
  BigDecimal root=new BigDecimal("1");		
  BigDecimal aux=new BigDecimal("0.0000000001");
  BigDecimal tr=new BigDecimal("3");
   String a;
   int ind=0,sum;
   while(scan.hasNext()){
    k=scan.nextBigDecimal();
    root=k.divide(new BigDecimal("2"));
    while(k.subtract(root.pow(3)).abs().compareTo(aux)>0)
     root=root.subtract(root.pow(3).subtract(k).divide(root.pow(2).multiply(tr),120,BigDecimal.ROUND_DOWN));
	a=root.toPlainString();
	ind=a.indexOf('.');
	a=a.substring(0,ind+11);
	sum=0;
	for(int j=0;j< a.length();j++) if(j!=ind)sum+=a.charAt(j)-48;
	System.out.println(sum%10+" "+a);
     }
  }
}

  1. #include <stdio.h>
    int main(void)
    {
    int arr[] = {10,20,30,40,50,60};
    int *p=arr;
    printf("%d,%d,",*p++,*++p);
    printf("%d,%d,%d",*p,*p++,*++p);
    return 0;
    }

    为什么是 20,20,50,40,50. 我觉得的应该是 20,20,40,40,50 . 谁能解释下?

  2. a是根先忽略掉,递归子树。剩下前缀bejkcfghid和后缀jkebfghicd,分拆的原则的是每个子树前缀和后缀的节点个数是一样的,根节点出现在前缀的第一个,后缀的最后一个。根节点b出现后缀的第四个位置,则第一部分为四个节点,前缀bejk,后缀jkeb,剩下的c出现在后缀的倒数第2个,就划分为cfghi和 fghic,第3部分就为c、c