团体程序设计天梯赛-练习集——L1-029 是不是太胖了、L1-051 打折(精确到某位小数)

L1-029 是不是太胖了

据说一个人的标准体重应该是其身高(单位:厘米)减去100、再乘以0.9所得到的公斤数。已知市斤的数值是公斤数值的两倍。现给定某人身高,请你计算其标准体重应该是多少?(顺便也悄悄给自己算一下吧……)

输入格式:
输入第一行给出一个正整数H(100 < H ≤ 300),为某人身高。

输出格式:
在一行中输出对应的标准体重,单位为市斤,保留小数点后1位。

输入样例:

169

输出样例:

124.2

最开始的想法是直接输出,但是忘记保留小数点后一位怎么写了

//cpp
#include<iostream>

using namespace std;

int main(){
	int H = 0;
	double W = 0.0;
	cin>>H;
	W = (H - 100) * 0.9 * 2;
	cout<<W<<endl;
	return 0;
}

这样的话有一个测试案例没有通过。

又查到了这个兄弟的做法
https://blog.csdn.net/createprogram/article/details/86750241

//C 
//但是这个我会
#include<stdio.h>
int main()
{
    double h,w;
    scanf("%lf",&h);
    w=(h-100)*1.8;
    printf("%.1f",w);
    return 0;
}

以及 setprecision()用法
https://www.cnblogs.com/LengYun/p/13138365.html
https://blog.csdn.net/mbxc816/article/details/7193615

//cpp
#include<iostream>
#include <iomanip>

using namespace std;

int main(){
	int H = 0;
	double W = 0.0;
	cin>>H;
	W = (H - 100) * 0.9 * 2;
	cout<<fixed<<setprecision(1)<<W<<endl;
	return 0;
}

然后java
刚开始是这么写的

//java
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int H = sc.nextInt();
		double W = (H - 100) * 0.9 * 2;
		System.out.println("%.1f",W);
	}
}

直接报错,应该是println用的不对
Main.java:8: error: no suitable method found for println(String,double)
System.out.println("%.1f",W);
^
method PrintStream.println() is not applicable
(actual and formal argument lists differ in length)
method PrintStream.println(boolean) is not applicable
(actual and formal argument lists differ in length)
method PrintStream.println(char) is not applicable
(actual and formal argument lists differ in length)
method PrintStream.println(int) is not applicable
(actual and formal argument lists differ in length)
method PrintStream.println(long) is not applicable
(actual and formal argument lists differ in length)
method PrintStream.println(float) is not applicable
(actual and formal argument lists differ in length)
method PrintStream.println(double) is not applicable
(actual and formal argument lists differ in length)
method PrintStream.println(char[]) is not applicable
(actual and formal argument lists differ in length)
method PrintStream.println(String) is not applicable
(actual and formal argument lists differ in length)
method PrintStream.println(Object) is not applicable
(actual and formal argument lists differ in length)

搜了搜其他人写的

//java
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int H = sc.nextInt();
		double G = (H - 100) * 0.9 * 2;
		System.out.printf("%.1f",G);
	}
}

https://www.cnblogs.com/huangZ-H/p/10079315.html
https://www.jianshu.com/p/fa31f7ae8664
//这两条还没仔细看,周日有功夫看下

同样的,L1-051 打折,也是类似的做法

L1-051 打折

去商场淘打折商品时,计算打折以后的价钱是件颇费脑子的事情。例如原价 ¥988,标明打 7 折,则折扣价应该是 ¥988 x 70% = ¥691.60。本题就请你写个程序替客户计算折扣价。

输入格式:
输入在一行中给出商品的原价(不超过1万元的正整数)和折扣(为[1, 9]区间内的整数),其间以空格分隔。

输出格式:
在一行中输出商品的折扣价,保留小数点后 2 位。

输入样例:

988 7

输出样例:

691.60
//cpp
#include<iostream>
#include <iomanip>

using namespace std;

int main(){
	int yuanJia = 0, zheKou = 0;
	double Z = 0.0;
	cin>>yuanJia>>zheKou;
	Z = yuanJia * zheKou * 0.1;
	cout<<fixed<<setprecision(2)<<Z<<endl;
	return 0;
}
//java
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int yuanJia = sc.nextInt();
		int zheKou = sc.nextInt();
		double Z = zheKou * yuanJia * 0.1;
		System.out.printf("%.2f",Z);
        System.out.println();
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值