杭电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);

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值