当前位置: 首页 > news >正文

每天一道算法_4_Hangover

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

今天的题目是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 makencards 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.



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(转载请说明出处)

相关文章:

  • 一初探js特效魅力之鼠标悬浮事件01
  • linux系统的组成——The composition of the Linux system
  • snprintf与printf
  • 一初探js特效魅力之div显示隐藏变色02
  • 帮豆包刷“天天爱消除”,“天天连萌”
  • 『开源』一个简单的 字符串计算 算法开源
  • 一初探js特效魅力之函数传参03
  • 每天一道算法_5_Financial Management
  • Android生命周期
  • 九度oj 1545奇怪的连通图
  • android中类似 QQ震动窗口的实现,带声音和振动效果
  • Linux下多任务间通信和同步-概述
  • 一初探js特效魅力之全选不选反选04
  • 360全线产品从小米应用商店下架
  • 阿里IPO弃港赴美?
  • Docker容器管理
  • iOS 系统授权开发
  • js正则,这点儿就够用了
  • Octave 入门
  • open-falcon 开发笔记(一):从零开始搭建虚拟服务器和监测环境
  • React Native移动开发实战-3-实现页面间的数据传递
  • Shadow DOM 内部构造及如何构建独立组件
  • 阿里云Kubernetes容器服务上体验Knative
  • 大数据与云计算学习:数据分析(二)
  • 等保2.0 | 几维安全发布等保检测、等保加固专版 加速企业等保合规
  • 分布式任务队列Celery
  • 关键词挖掘技术哪家强(一)基于node.js技术开发一个关键字查询工具
  • 蓝海存储开关机注意事项总结
  • 前嗅ForeSpider教程:创建模板
  • 算法-图和图算法
  • 我从编程教室毕业
  • 仓管云——企业云erp功能有哪些?
  • 如何用纯 CSS 创作一个货车 loader
  • ​MPV,汽车产品里一个特殊品类的进化过程
  • # Python csv、xlsx、json、二进制(MP3) 文件读写基本使用
  • #我与Java虚拟机的故事#连载13:有这本书就够了
  • (27)4.8 习题课
  • (Python第六天)文件处理
  • (十三)Java springcloud B2B2C o2o多用户商城 springcloud架构 - SSO单点登录之OAuth2.0 根据token获取用户信息(4)...
  • (五)c52学习之旅-静态数码管
  • (译)2019年前端性能优化清单 — 下篇
  • (转)scrum常见工具列表
  • * 论文笔记 【Wide Deep Learning for Recommender Systems】
  • ****Linux下Mysql的安装和配置
  • .NET 3.0 Framework已经被添加到WindowUpdate
  • .NET 4.0中的泛型协变和反变
  • .NET 8.0 发布到 IIS
  • .Net CF下精确的计时器
  • .Net Core/.Net6/.Net8 ,启动配置/Program.cs 配置
  • .NET delegate 委托 、 Event 事件,接口回调
  • .Net Winform开发笔记(一)
  • .NET处理HTTP请求
  • .Net多线程总结
  • []我的函数库
  • [AI]文心一言出圈的同时,NLP处理下的ChatGPT-4.5最新资讯