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


相关推荐

  • 图存储之十字链表

    图存储之十字链表一概述十字链表是有向图的一种链式存储结构,在十字链表中,对应于有向图中的每条弧有一个结点,对应于每个顶点也有一个结点。二十字链表十字链表的结构分为弧结点和顶点结点,其中弧结点中有5个域:尾域和头域分别指示弧尾和弧头这两个顶点在图中的位置;链域hlink指向弧头相同的下一条弧;链域tlink指向弧尾相同的下一个条弧;info域指向该弧的相关信息。…

    2022年6月18日
    25
  • Java实现远程开关机(网络唤醒)

    Java实现远程开关机(网络唤醒)Java实现远程开关机是做的一个在局域网下实现一台机器对多台机器的开关机控制远程唤醒实现是通过电源的一种协议进行控制的,需要知道被唤醒机器的mac地址首先要须知:网络唤醒条件网络唤醒需要终端的主板和网卡支持,需要先在BIOS设置支持网络唤醒网络唤醒要接通电源保证网卡能通电要接网线不能是wifi如果强制关机可能不能通过网络唤醒来开机跨交换机或者跨路由的话就有可能不支持唤醒跨多…

    2022年6月2日
    42
  • 红队评估实战靶场(1)

    0x00前言[滑稽][滑稽]又是我,我又来发水文了,这几天打靶机打上瘾了,再来更新篇靶机的文章0x01靶机渗透配置好靶机后,这里需要打开win7,来到c盘目录下启动phpstudy启动完成后

    2021年12月11日
    37
  • 一款你不容错过的Laravel后台管理扩展包 —— Voyager

    一款你不容错过的Laravel后台管理扩展包 —— Voyager

    2021年10月21日
    44
  • 多级时间轮定时器_时间轮与哈希表定时

    多级时间轮定时器_时间轮与哈希表定时时间轮简述顾名思义,时间轮就像一个轮子,在转动的时候外界会指向轮子不同的区域,该区域就可以被使用。因此只要将不同时间的定时器按照一定的方法散列到时间轮的不同槽(即时间轮划分的区域)之中,就可以实现在运转到某个槽时,进行判断该定时器是否已经到达运行时间(需要判断是由于有的定时器并非在这一圈就需要运行,可能需要后面几圈才会运行。从图中也可以看出,每个槽中的定时器是以(双向)链表…

    2022年9月29日
    2
  • JVM性能调优监控工具jps、jstack、jmap、jhat、jstat、hprof使用详解

    JVM性能调优监控工具jps、jstack、jmap、jhat、jstat、hprof使用详解摘要:JDK本身提供了很多方便的JVM性能调优监控工具,除了集成式的VisualVM和jConsole外,还有jps、jstack、jmap、jhat、jstat、hprof等小巧的工具,本博客希望能起抛砖引玉之用,让大家能开始对JVM性能调优的常用工具有所了解。现实企业级Java开发中,有时候我们会碰到下面这些问题: OutOfMemoryError,内存不足 内存…

    2022年5月2日
    42

发表回复

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

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