首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >理解这个“总结”任务

理解这个“总结”任务
EN

Stack Overflow用户
提问于 2013-11-09 19:33:00
回答 1查看 69关注 0票数 0

我很难理解这个任务要求我做什么。我是一个完全的初学者,几个星期的学习Java/编程的一般,这对我来说非常困难。所以很抱歉标题中没有很具体的内容。

如果有人能向我解释一下,我会非常感激的,也许我可以为一个例子提供一些示例代码(我不是要求人们为我完成任务,我只是觉得这样更容易理解),这样我就可以知道该做什么了。

谢谢。

总结规范:

这个程序从命令行获取输入。

代码语言:javascript
复制
The first input is K, an integer, followed by an arbitrary number a_1, ..., a_N of floating-point numbers.

If K=0, then N is output.

If K > 0, then the a_i are grouped in groups of size K, inside the groups the numbers are multiplied, and all the products are added, yielding the output.
If K < 0, then the summation sums up the reciprocal values of the products.
In the special case K=1 the output thus is a_1 + ... + a_N.
In the case K=-1 the output is 1/a_1 + ... + 1/a_N.

There are two error cases:
If no command-line argument is given, then error-code 1 is to be returned.
If K < 0, and one of a_i = 0, then error-code 2 is to be returned.
In both cases there must be no output.
The return-code of a program is set via System.exit(code);

请注意,程序的返回代码是通过echo $?在命令行上获得的。绝不能有任何其他产出。此外,也不允许额外的空格或行中断。

下面是没有错误的例子:http://pastebin.com/F2uz262v

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-11-09 20:36:49

您将在这里找到命令行参数:

代码语言:javascript
复制
public static void main(String args[]) {
                            // ^ these are the command line arguments

所以要得到K

代码语言:javascript
复制
int K = Integer.parseInt(args[0]);

但实际上,您不应该这样写它,因为这是Java,Java变量应该以小写字母开头。

代码语言:javascript
复制
int k = Integer.parseInt(args[0]);

首先,让我们讨论一下k=0的情况。我们必须测试k是否为0:

代码语言:javascript
复制
if(k == 0) {

如果是,则N是附加参数的数目,即

代码语言:javascript
复制
  int numberOfAdditionalArguments = args.length - 1;

然后我们打印它(之后没有断线,就像上面写的!)

代码语言:javascript
复制
  System.out.print(numberOfAdditionalArguments);

然后关闭}块

代码语言:javascript
复制
}

我想这足以让你开始工作。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19882153

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档