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


相关推荐

  • Ubuntu(VMWare)与Windows共享文件的简单方法

    Ubuntu(VMWare)与Windows共享文件的简单方法

    2021年7月30日
    68
  • python+opencv图像模板匹配—多模板匹配

    python+opencv图像模板匹配—多模板匹配

    2021年10月6日
    40
  • 几款国产开源的Windows界面库

    几款国产开源的Windows界面库vchelp2013-3-212:10:47阅读(3972)评论(0)上次介绍的几款图形界面库http://blog.okbase.net/vchelp/archive/23.html都是国外的开源项目,今天介绍的几款都是国人的开源项目,大部分是采用DirectUI设计思想。 1. 炫彩界面库XCGUI炫彩界面库不仅是界面库,它是软件界面开发框架,让用户开发

    2022年7月26日
    6
  • qq邮箱收到钓鱼邮件_emc邮件门事件该怎么回答

    qq邮箱收到钓鱼邮件_emc邮件门事件该怎么回答0x0背景福无双至、祸不单行。本来是风和日丽的天气,白帽子在工地认真搬砖然后被一些所谓的负(dou)责(bi)人怼了,心里感觉到很委屈。准备下班之际莫名其妙收到了一封QQ邮箱弹窗点开一看直觉就是"这货有毒"。0x1过程新学期课程表安排通知,感觉还是比较有针对性的,想一想自己前段时间加入一些考研群的,目测就是这些群里面的人吧。话不多说,就开启burpsuit看看通信过程。一访…

    2022年8月24日
    8
  • acwing-393. 雇佣收银员(差分约束)

    acwing-393. 雇佣收银员(差分约束)一家超市要每天 24 小时营业,为了满足营业需求,需要雇佣一大批收银员。已知不同时间段需要的收银员数量不同,为了能够雇佣尽可能少的人员,从而减少成本,这家超市的经理请你来帮忙出谋划策。经理为你提供了一个各个时间段收银员最小需求数量的清单 R(0),R(1),R(2),…,R(23)。R(0) 表示午夜 00:00 到凌晨 01:00 的最小需求数量,R(1) 表示凌晨 01:00 到凌晨 02:00 的最小需求数量,以此类推。一共有 N 个合格的申请人申请岗位,第 i 个申请人可以从 ti 时刻开始

    2022年8月9日
    11
  • Android Hook技术的简单实现

    Android Hook技术的简单实现一、什么是Hook技术Android程序有一套特有的事件分发机制,都是按既定程序从前往后执行的。Hook技术就是利用反射和代理,在既定程序中插入我们自己写的程序。比如,我们想在App所有的点击事件中添加播放音乐的效果。所有控件的点击事件,分发流程都是系统已经写好了,这时我们怎么做到在其中插入我们的播放音乐的效果呢?让我们拭目以待!二、如何寻找Hook点1.尽量选择静态变量和单例对象,因为一旦创建对象,他们不容易变化,非常容易定位。2.尽量Hookpublic的对象和方法三、Hook过程选

    2022年5月11日
    48

发表回复

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

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