每天一道算法_4_Hangover

此系列刚开始两天就被十一假期打断了,去山西玩了几天,今天刚回来,为了弥补一下心里的貌似隐隐作痛的愧疚感,补上一刀。今天的题目是 Hangover,如下: DescriptionHow far can you make a stack of cards overhang a table? If you have one card, you can create a max

大家好,又见面了,我是全栈君。

此系列刚开始两天就被十一假期打断了,去山西玩了几天,今天刚回来,为了弥补一下心里的貌似隐隐作痛的愧疚感,挑了个简单的练练手,权当给自己补上一刀。

今天的题目是 Hangover,如下:

 

Description

How far can you make a stack of cards overhang a table? If you have one card, you can create a maximum overhang of half a card length. (We’re assuming that the cards must be perpendicular to the table.) With two cards you can make the top card overhang the bottom one by half a card length, and the bottom one overhang the table by a third of a card length, for a total maximum overhang of 1/2 + 1/3 = 5/6 card lengths. In general you can make n cards overhang by 1/2 + 1/3 + 1/4 +… + 1/(n + 1) card lengths, where the top card overhangs the second by 1/2, the second overhangs tha third by 1/3, the third overhangs the fourth by 1/4, etc., and the bottom card overhangs the table by 1/(n + 1). This is illustrated in the figure below.






每天一道算法_4_Hangover

Input

The input consists of one or more test cases, followed by a line containing the number 0.00 that signals the end of the input. Each test case is a single line containing a positive floating-point number c whose value is at least 0.01 and at most 5.20; c will contain exactly three digits.

Output

For each test case, output the minimum number of cards necessary to achieve an overhang of at least c card lengths. Use the exact output format shown in the examples.

Sample Input

1.00
3.71
0.04
5.19
0.00

Sample Output

3 card(s)
61 card(s)
1 card(s)
273 card(s)

 

代码如下:

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;


public class Hangover {
	
	public static void main(String args[]){
		List<Float> list = new ArrayList<Float>();
		Scanner scanner = new Scanner(System.in);
		int all = scanner.nextInt();
		while(all > 0){
			list.add(scanner.nextFloat());
			all --;
		}
		for(int i = 0; i < list.size(); i ++){
			float f = list.get(i);
			int count = 1;
			float j=2,sum=0;
			for(; sum + (1/j) < f; j ++){
				count ++;
				sum = sum+ (1/j);
			}
			System.out.println(count + " card(s)");
		}
	}
}

 

输入输出如下:

4 1.00 3.71 0.04 5.19
3 card(s)
61 card(s)
1 card(s)
273 card(s)

由于输入的时候弄了半天没想出合适的用0.00结尾的方法,就用了开头先输入一个整数,表示后面要输入的整数个数,然后依次输入的方法,

相信你能看懂。

 

作者:jason0539

微博:http://weibo.com/2553717707

博客:http://blog.csdn.net/jason0539(转载请说明出处)

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/121386.html原文链接:https://javaforall.net

(0)
上一篇 2022年3月10日 下午7:00
下一篇 2022年3月10日 下午8:00


相关推荐

  • tcpdf的方法_tcp fin

    tcpdf的方法_tcp fin$pdf=newTCPDF(PDF_PAGE_ORIENTATION,PDF_UNIT,PDF_PAGE_FORMAT,true,’UTF-8′,false);页面记得也设为utf-8AddPage();新建一个pdf文档页面。Image($file,$x,$y,$w,$h,$type,$link,$align,$resize,$dpi,$p

    2025年9月1日
    10
  • java中string转换为int(int char)

    //Stringchangeintpublicstaticvoidmain(String[]args){Stringstr=”123″;intn;//firstmethod//n=Integer.parseInt(str);n=0;n=Integer.parseInt(str);…

    2022年4月12日
    40
  • Java HotSpot VM Options 参数设置

    Java HotSpot VM Options 参数设置

    2021年6月18日
    110
  • jdbc的增删改查_使用jdbc完成数据的增删改查

    jdbc的增删改查_使用jdbc完成数据的增删改查JBDC数据的持久化:把数据保存到磁盘上。JDBC是java访问数据库的基石,JDO,Hibernate,Mybatis等都是基于JDBCJDBC是一个独立于特定数据库的管理系统,通用的SQL数据库存取和操作的公共接口配置文件:jdbc.propertiesuser=rootpassword=abc123url=jdbc:mysql://localhost:3306/testdriverClass=com.mysql.jdbc.Driver获取Connectionpublic s

    2022年8月8日
    4
  • nginx支持的负载均衡算法_nginx算法

    nginx支持的负载均衡算法_nginx算法1:Nginx负载均衡算法(1):轮询(默认)每个请求按时间顺序逐一分配到不同的后端服务,如果后端某台服务器死机,自动剔除故障系统,使用户访问不受影响。upstreamtomcat{server192.168.200.113:8080weight=1;server192.168.200.114:8080weight=1;}(2):Weight(轮询权值)Weight的值越大分配到的访问概率越高,主要用于后端每台服务器性能不均衡…

    2022年10月10日
    5
  • ubuntu anaconda换源_ubuntu如何换用国内源

    ubuntu anaconda换源_ubuntu如何换用国内源1.先生成配置文件.condarccondaconfig–setshow_channel_urlsyes2.配置文件的目录是:~/.condarc,使用vim打开,并添加我们需要的源:vim~/.condarc3.添加代码更换清华源channels:-https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/-https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/

    2022年10月1日
    4

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号