每天一道算法_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(转载请说明出处)

 

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

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

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)
blank

相关推荐

  • awk从放弃到入门(1):awk基础 (通俗易懂,快进来看)「建议收藏」

    awk从放弃到入门(1):awk基础 (通俗易懂,快进来看)「建议收藏」我们先来用专业的术语描述一下awk是什么,如果你看不懂,没关系,我们会再用"大白话"解释一遍。 awk是一个报告生成器,它拥有强大的文本格式化的能力,这就是专业的说法。你可能不理解所谓的报告生成器中的"报告"是什么,你可以把"报告"理解为"报表"或者"表格",也就是说,我们可以利用awk命令,将一些文本整理成我们想要的样子,比如把一些文本整理成"表"的样子,然后再展示出来,刚才概念中提到的

  • HTTP.SYS远程代码执行漏洞测试

    HTTP.SYS远程代码执行漏洞测试今天中午收到命令让下午两点出一个网站的测试报告 这么急也没详细的测试就用awvs跑了一下 没想到跑出来一个高危HTTP.SYS远程代码执行漏洞测试开始跑出来的时候很兴奋 一个高危也不错了   但是验证的时候可给我愁坏了这里我非常推荐这篇博客 太详细了http://www.cnblogs.com/peterpan0707007/p/8529261.html然后我就想着用…

  • Anaconda环境下Tensorflow的安装与卸载

    Anaconda环境下Tensorflow的安装与卸载Anaconda环境下Tensorflow的安装与卸载文章目录Anaconda环境下Tensorflow的安装与卸载一、环境的创建与删除1.查看自己配置的环境2.配置一个新的环境3.进入和退出环境4.删除环境二、包(第三方库)的安装与卸载1.查看安装的包2.安装包3.删除包4.更新包三、tensorflow的安装与卸载1.创建一个tensorflow环境2.激活tensorflow环境3.安装tensorflow4.查看是否安装成功5.查看tensorflow的版本号6.退出tensorflow环境一

  • 七牛云大数据平台建设实践

    七牛云大数据平台建设实践七牛云CEO许式伟首次披露七牛云在大数据方向的产品思路。

  • Olympian Math Problem

    Olympian Math Problem

  • Qt学习笔记#4:QTimer和QTime

    QTimerClassQTimer是一个计时器类它的使用分三步,创建对象,连接signal和slot函数,start()QTimer*timer=newQTimer(this);connect(timer,SIGNAL(timeout()),this,SLOT(update()));timer->start(1000);其中,SIGNAL(timeou

发表回复

您的电子邮箱地址不会被公开。

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