杭電ACM 1012 1013 1014

1012 . u Calculate e

Problem Description
A simple mathematical formula for e is

where n is allowed to go to infinity. This can actually yield very accurate approximations of e using relatively small values of n.

Output
Output the approximations of e generated by the above formula for the values of n from 0 to 9. The beginning of your output should appear similar to that shown below.

http://acm.hdu.edu.cn/showproblem.php?pid=1012

題目大意:用題目中給的級數估計e的值,對n從0到9,輸出e的近似值。

import java.math.BigDecimal;
import java.math.RoundingMode;

public class Main {

    public static void main(String args[]){
        System.out.println("n e");
        System.out.println("- -----------");
        System.out.println("0 1");
        System.out.println("1 2");
        System.out.println("2 2.5");
        BigDecimal r[]=new BigDecimal[9];
        BigDecimal e[]=new BigDecimal[7];
        for(int i=1;i<10;i++){
            r[i-1]=new BigDecimal(i);
        }
        for(int i=1;i<9;i++){
            r[i]=r[i].multiply(r[i-1]);
        }
        for(int i=1;i<9;i++){
            r[i]=r[0].divide(r[i],11,RoundingMode.HALF_DOWN);
        }
        e[0]=r[2].add(new BigDecimal("2.5"));
        System.out.printf("%d %.9f\n",3,e[0].setScale(9, RoundingMode.HALF_DOWN).doubleValue());
        for(int i=1;i<7;i++){
            e[i]=e[i-1].add(r[i+2]);
            System.out.printf("%d %.9f\n",(i+3),e[i].setScale(9, RoundingMode.HALF_DOWN).doubleValue());

        }
    }
}

System.out.printf(“%d %.9f\n”,3,e[0].setScale(9,RoundingMode.HALF_DOWN).doubleValue());注意這裏的%.9f使得小數點後一定會輸出九位,setScale函數是BigDecimal設置精度的函數,這裏9代表精確到小數點後九位,RoundingMode.HALF_DOWN代表四捨五入的進位模式。

1013 .Digital Roots

Problem Description
The digital root of a positive integer is found by summing the digits of the integer. If the resulting value is a single digit then that digit is the digital root. If the resulting value contains two or more digits, those digits are summed and the process is repeated. This is continued as long as necessary to obtain a single digit.

For example, consider the positive integer 24. Adding the 2 and the 4 yields a value of 6. Since 6 is a single digit, 6 is the digital root of 24. Now consider the positive integer 39. Adding the 3 and the 9 yields 12. Since 12 is not a single digit, the process must be repeated. Adding the 1 and the 2 yeilds 3, a single digit and also the digital root of 39.

Input
The input file will contain a list of positive integers, one per line. The end of the input will be indicated by an integer value of zero.

Output
For each integer in the input, output its digital root on a separate line of the output.

http://acm.hdu.edu.cn/showproblem.php?pid=1013

題目大意:就是對一個數字,將它的每一位數字加起來得到一個新的數字,再對新的數字做同樣的事情,最終會得到一個一位數。求這個一位數。

import java.math.BigInteger;
import java.util.Scanner;

public class Main {

    public static void main(String args[]){
        Scanner s=new Scanner(System.in);
        String str;
        while(s.hasNext()){
            str=s.nextLine();
            if(str.charAt(0)=='0')
                break;
            BigInteger n=new BigInteger(str);
            BigInteger ten=new BigInteger("10");
            BigInteger r=n.mod(ten);
            while(true){
                while(!(n.multiply(ten)).equals(new BigInteger("0"))){
                    n=n.divide(ten);
                    r=(n.mod(ten)).add(r);
                }
                if(r.compareTo(ten)<0)
                    break;
                else{
                    n=r;
                    r=n.mod(ten);
                }
            }
            System.out.println(r.toString());
        }
    }
}

要注意這裏的數字可能很長,別用int,可以用String來讀取數字,我這裏選取直接用大整數的方法解決問題。

1014 .Uniform Generator

Problem Description
Computer simulations often require random numbers. One way to generate pseudo-random numbers is via a function of the form

seed(x+1) = [seed(x) + STEP] % MOD

where ‘%’ is the modulus operator.

Such a function will generate pseudo-random numbers (seed) between 0 and MOD-1. One problem with functions of this form is that they will always generate the same pattern over and over. In order to minimize this effect, selecting the STEP and MOD values carefully can result in a uniform distribution of all values between (and including) 0 and MOD-1.

For example, if STEP = 3 and MOD = 5, the function will generate the series of pseudo-random numbers 0, 3, 1, 4, 2 in a repeating cycle. In this example, all of the numbers between and including 0 and MOD-1 will be generated every MOD iterations of the function. Note that by the nature of the function to generate the same seed(x+1) every time seed(x) occurs means that if a function will generate all the numbers between 0 and MOD-1, it will generate pseudo-random numbers uniformly with every MOD iterations.

If STEP = 15 and MOD = 20, the function generates the series 0, 15, 10, 5 (or any other repeating series if the initial seed is other than 0). This is a poor selection of STEP and MOD because no initial seed will generate all of the numbers from 0 and MOD-1.

Your program will determine if choices of STEP and MOD will generate a uniform distribution of pseudo-random numbers.

Input
Each line of input will contain a pair of integers for STEP and MOD in that order (1 <= STEP, MOD <= 100000).

Output
For each line of input, your program should print the STEP value right- justified in columns 1 through 10, the MOD value right-justified in columns 11 through 20 and either “Good Choice” or “Bad Choice” left-justified starting in column 25. The “Good Choice” message should be printed when the selection of STEP and MOD will generate all the numbers between and including 0 and MOD-1 when MOD numbers are generated. Otherwise, your program should print the message “Bad Choice”. After each output test set, your program should print exactly one blank line.

http://acm.hdu.edu.cn/showproblem.php?pid=1014

題目大意:就是求兩個數字的最大公約數,如果爲1就是Good Choice。

import java.util.Scanner;

public class Main {
    public static void main(String args[]){
        Scanner s=new Scanner(System.in);
        while(s.hasNext()){
            int a=s.nextInt();
            int b=s.nextInt();
            if(gcd(a,b)==1){
                System.out.printf("%10d%10d    Good Choice",a,b);
                System.out.println();
            }
            else{
                System.out.printf("%10d%10d    Bad Choice",a,b);
                System.out.println();
            }
            System.out.println();
        }
    }

    private static int gcd(int a, int b) {
        // TODO Auto-generated method stub
        if(a<b){
            a=a^b;
            b=a^b;
            a=a^b;
        }
        if(b==0)
            return a;
        return gcd(b,a-b);
    }
}

注意輸出格式System.out.printf(“%10d%10d Good Choice”,a,b);

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章