Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 简单算法。Can';我没弄对_Java_Algorithm - Fatal编程技术网

Java 简单算法。Can';我没弄对

Java 简单算法。Can';我没弄对,java,algorithm,Java,Algorithm,问题 Yraglac最近决定试用Soylent,一种膳食替代饮料,旨在满足普通成年人的所有营养需求。Soylent不仅味道很好,而且价格低廉,这对Yraglac来说很重要,因为他目前正处于预算之中。每瓶可提供400卡路里的热量,因此建议每人每天摄入5瓶,总热量为2000卡路里。然而,Yraglac想知道,如果他每天的卡路里需求量与普通成年人不一样,他应该喝多少瓶。他只能消耗整数瓶,并且至少需要消耗他每天所需的卡路里 Src: 输入 public Soylent() { Scanner

问题

Yraglac最近决定试用Soylent,一种膳食替代饮料,旨在满足普通成年人的所有营养需求。Soylent不仅味道很好,而且价格低廉,这对Yraglac来说很重要,因为他目前正处于预算之中。每瓶可提供400卡路里的热量,因此建议每人每天摄入5瓶,总热量为2000卡路里。然而,Yraglac想知道,如果他每天的卡路里需求量与普通成年人不一样,他应该喝多少瓶。他只能消耗整数瓶,并且至少需要消耗他每天所需的卡路里

Src:

输入

public Soylent() {

    Scanner scan = new Scanner(System.in);
    int iterations = scan.nextInt();

    for (int i = 0; i < iterations; i++) {
        int calories = scan.nextInt();
        System.out.println((int) (Math.ceil(calories / 400)));
    }
}
第一行包含一个整数T≤ 1000给出了测试用例的数量。每个测试用例由一行整数N(0)组成≤ N≤ 100000),一天所需的热量

输出

public Soylent() {

    Scanner scan = new Scanner(System.in);
    int iterations = scan.nextInt();

    for (int i = 0; i < iterations; i++) {
        int calories = scan.nextInt();
        System.out.println((int) (Math.ceil(calories / 400)));
    }
}
对于每个测试用例,输出一行,其中包含一天Yraglac需要消耗的瓶子数量

样本输入

public Soylent() {

    Scanner scan = new Scanner(System.in);
    int iterations = scan.nextInt();

    for (int i = 0; i < iterations; i++) {
        int calories = scan.nextInt();
        System.out.println((int) (Math.ceil(calories / 400)));
    }
}
2
2000
1600

样本输出

public Soylent() {

    Scanner scan = new Scanner(System.in);
    int iterations = scan.nextInt();

    for (int i = 0; i < iterations; i++) {
        int calories = scan.nextInt();
        System.out.println((int) (Math.ceil(calories / 400)));
    }
}
5
4

我的尝试

public Soylent() {

    Scanner scan = new Scanner(System.in);
    int iterations = scan.nextInt();

    for (int i = 0; i < iterations; i++) {
        int calories = scan.nextInt();
        System.out.println((int) (Math.ceil(calories / 400)));
    }
}
public Soylent(){
扫描仪扫描=新扫描仪(System.in);
int迭代次数=scan.nextInt();
对于(int i=0;i

我做错了什么?

这是因为您在
Math.ceil
调用中使用了
int
s

例如,如果运行此代码:

public class Main {

    public static void main(String[] args) {
        System.out.println(Math.ceil(2500/400));
        System.out.println(Math.ceil(2500/400.0));
    }
}
您将获得以下输出:

6.0
7.0
在第二个
Math.ceil
调用中,您实际使用的是双精度,因此2500/400的小数不会丢失

更清楚地说:

int result1 = 2500 / 400; //6.0
int ceiling1 = Math.ceil(result1); //6

double result2 = 2500 / 400; //6.0
int ceiling2 = Math.ceil(result2); //6

double result3 = 2500 / 400.0; //6,25
int ceiling3 = Math.ceil(result3); //7

double result4 = (double) 2500 / 400; //6.25
int ceiling4 = Math.ceil(result4); //7

因此,要修复代码,请将
carries
设置为double,除以
400.0
,或者将
carries/400
的结果转换为
double

,因为在
Math.ceil
调用中使用了
int
s,这会出错

例如,如果运行此代码:

public class Main {

    public static void main(String[] args) {
        System.out.println(Math.ceil(2500/400));
        System.out.println(Math.ceil(2500/400.0));
    }
}
您将获得以下输出:

6.0
7.0
在第二个
Math.ceil
调用中,您实际使用的是双精度,因此2500/400的小数不会丢失

更清楚地说:

int result1 = 2500 / 400; //6.0
int ceiling1 = Math.ceil(result1); //6

double result2 = 2500 / 400; //6.0
int ceiling2 = Math.ceil(result2); //6

double result3 = 2500 / 400.0; //6,25
int ceiling3 = Math.ceil(result3); //7

double result4 = (double) 2500 / 400; //6.25
int ceiling4 = Math.ceil(result4); //7

因此,要修正代码,要么将
卡路里
设为双倍,除以
400.0
,要么将
卡路里/400
的结果转换为
双倍
。正如Michael_T.所说,卡路里是双倍的,而不是整数。谢谢

public class Soylent {

public Soylent() {

    Scanner scan = new Scanner(System.in);
    int iterations = scan.nextInt();

    for (int i = 0; i < iterations; i++) {
        double calories = scan.nextDouble();
        System.out.println((int) (Math.ceil(calories / 400)));
    }
}
公共类{
公共图书馆{
扫描仪扫描=新扫描仪(System.in);
int迭代次数=scan.nextInt();
对于(int i=0;i
正如迈克尔所说,卡路里是双倍的,而不是整数。谢谢

public class Soylent {

public Soylent() {

    Scanner scan = new Scanner(System.in);
    int iterations = scan.nextInt();

    for (int i = 0; i < iterations; i++) {
        double calories = scan.nextDouble();
        System.out.println((int) (Math.ceil(calories / 400)));
    }
}
公共类{
公共图书馆{
扫描仪扫描=新扫描仪(System.in);
int迭代次数=scan.nextInt();
对于(int i=0;i
根据您编写的代码,您的代码似乎打印出了输入数据的正确答案。您认为有什么问题吗?我想问几个问题,需要更多信息:它是否编译?您有测试用例吗?您有输出吗?我想说的是,我们有问题、预期结果和您的代码,但没有您的结果:)您的代码运行正常。或者你是在说你得到的是5.0而不是5?为什么不只打印卡路里/400?卡路里是双倍的吗?根据你编写的代码,你的代码似乎打印出了输入数据的正确答案。你认为错在哪里?我想有几个问题,需要更多信息:它编译吗?你有测试用例吗?你得到输出了吗?我想说的是我们有问题,预期的结果和你的代码,但不是你的结果:)你的代码工作正常。或者你是说你得到的是5.0而不是5?你为什么不只打印卡路里/400?卡路里是双倍的吗?