每天一道算法_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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • ICMP详解「建议收藏」

    ICMP详解「建议收藏」ICMP(InternetControlMessageProtocol)网际控制报文协议。它是TCP/IP协议簇的一个子协议,用于在IP主机、路由器之间传递控制消息。控制消息是指网络通不通、主机是否可达、路由是否可用等网络本身的消息。这些控制消息虽然并不传输用户数据,但是对于用户数据的传递起着重要的作用。ICMP使用IP的基本支持,就像它是一个更高级别的协议,但是,ICMP实际上是IP的…

    2022年7月12日
    20
  • java面向接口编程的例子_大二java期末考试试题

    java面向接口编程的例子_大二java期末考试试题转载:https://blog.csdn.net/l1028386804/article/details/43761615 我想,对于各位使用面向对象编程语言的程序员来说,“接口”这个名词一定不陌生,但是不知各位有没有这样的疑惑:接口有什么用途?它和抽象类有什么区别?能不能用抽象类代替接口呢?而且,作为程序员,一定经常听到“面向接口编程”这个短语,那么它是什么意思?有什么思想内涵?和面向对…

    2025年6月2日
    3
  • 检索学位论文的检索式_怎么用谷歌学术查文献

    检索学位论文的检索式_怎么用谷歌学术查文献学位论文(简称大论文)文献检索格式和平时发表的期刊论文(简称小论文)文献格式往往不一样。小论文归拢大论文的时候,格式调起来很麻烦,很繁琐。而且很多同学并没有使用文献管理软件的习惯。这时候怎么办?答案:谷歌学术搜索。1、把你的文献名粘贴到谷歌学术搜索(注意是http://scholar.google.hk/)搜索,如下图所示2、注意所查文献下方“引用”的链接,点击

    2022年10月11日
    3
  • python 对selenium框架进行二次封装,原来如此简单

    python 对selenium框架进行二次封装,原来如此简单

    2021年11月10日
    54
  • app软件开发的基本流程_app项目开发流程

    app软件开发的基本流程_app项目开发流程 本文转载自互联网,如有侵权,请联系我及时删除。谢谢。(一)项目启动前  从事产品的工作一年多,但自己一直苦于这样或者那样的困惑,很多人想要从事产品,或者老板自己创业要亲自承担产品一职,但他们对产品这个岗位的认识却不明晰,有的以为是纯粹的画原型,有的是以为做项目管理跟踪项目进度,有的是做竞品分析给老板看。实际上,这些都不是产品经理的核心和重点。在较为成熟的企业,因为产品的壮大和人员的增多,…

    2025年6月26日
    3
  • DP算法分类总结_算法总结

    DP算法分类总结_算法总结转载请注明出处,谢谢。&nbsp;&nbsp;http://blog.csdn.net/cc_again?viewmode=list&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;———-&nbsp;Accagain&nbsp;…

    2025年8月24日
    2

发表回复

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

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