蓝桥杯 算法训练 调和级数问题


蓝桥杯 算法训练 调和级数问题

Description

输入一个实数x,求最小的n使得,1/2+1/3+1/4+…+1/(n+1)>=x。

  输入的实数x保证大于等于0.01,小于等于5.20,并且恰好有两位小数。你的程序要能够处理多组数据,即不停地读入x,如果x不等于0.00,则计算答案,否则退出程序。

  输出格式为对于一个x,输出一行n card(s)。其中n表示要计算的答案。

Input

输入描述:

  分行输入x的具体数值

输入样例:

Output

输出描述:

  分行输出n的数值,格式为n card(s)

输出样例:

Sample Input

0.36 1.45 2.78 4.12 0.00

Sample Output

1 card(s) 5 card(s) 24 card(s) 93 card(s)

Hint

HINT:时间限制:1.0s 内存限制:512.0MB

Source

蓝桥杯练习系统 ID: 107 原题链接: http://lx.lanqiao.cn/problem.page?gpid=T107

代码


import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner scanner = new Scanner(System.in);

        while(scanner.hasNext()) {
            double input = scanner.nextDouble();
            double sum = 0.00;
            int n = 0;
            if (input == 0) {
                break;
            }
            for(int i = 2;;i++) {
                sum += 1.00/i;
                if (sum>=input) {
                    n = i-1;
                    break;
                }
            }
            System.out.println(n+" card(s)");         
        }       

    }
}

Author: qwq小小舒
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint policy. If reproduced, please indicate source qwq小小舒 !
  TOC