java 如何编写一个将整数序列读入数组并计算数组中所有元素的交替总和的程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10052568/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to write a program that reads a sequence of integers into an array and that computes the alternating sum of all elements in the array?
提问by smiley
Write a program that reads a sequence of integers into an array and that computes the alternating sum of all elements in the array. For example, if the program is executed with the input data
编写一个程序,将整数序列读入数组,并计算数组中所有元素的交替和。例如,如果使用输入数据执行程序
1 4 9 16 9 7 4 9 11 then it computes
1 4 9 16 9 7 4 9 11 然后计算
1 - 4 + 9 - 16 + 9 - 7 + 4 - 9 + 11 = - 2
1 - 4 + 9 - 16 + 9 - 7 + 4 - 9 + 11 = - 2
I have below code so far:
到目前为止,我有以下代码:
import java.util.Arrays;
/**
This class computes the alternating sum
of a set of data values.
*/
public class DataSet
{
private double[] data;
private int dataSize;
/**
Constructs an empty data set.
*/
public DataSet()
{
final int DATA_LENGTH = 100;
data = new double[DATA_LENGTH];
dataSize = 0;
}
/**
Adds a data value to the data set.
@param x a data value
*/
public void add(double x)
{
if (dataSize == data.length)
data = Arrays.copyOf(data, 2 * data.length);
data[dataSize] = x;
dataSize++;
}
/**
Gets the alternating sum of the added data.
@return sum the sum of the alternating data or 0 if no data has been added
*/
public double alternatingSum()
{
. . .
}
}
I have to use the following class as the tester class:
我必须使用以下类作为测试类:
/**
This program calculates an alternating sum.
*/
public class AlternatingSumTester
{
public static void main(String[] args)
{
DataSet data = new DataSet();
data.add(1);
data.add(4);
data.add(9);
data.add(16);
data.add(9);
data.add(7);
data.add(4);
data.add(9);
data.add(11);
double sum = data.alternatingSum();
System.out.println("Alternating Sum: " + sum);
System.out.println("Expected: -2.0");
}
}
回答by Tom
I implemented the method alternatingSum for you:
我为你实现了alternateSum方法:
public double alternatingSum() {
double alternatingSum = 0;
if(data != null || dataSize > 0) {
for(int i = 0; i < dataSize; i = i + 2) {
alternatingSum += data[i];
}
for(int i = 1; i < dataSize; i = i + 2) {
alternatingSum -= data[i];
}
}
return alternatingSum;
}
回答by RAJU GORAI
int[] a = {50, 60, 60, 45, 70};
int sum = IntStream.range(0, a.length).filter(i -> i % 2 == 0).map(i -> a[i]).sum()
- IntStream.range(0, a.length).filter(i -> i % 2 == 1).map(i -> a[i]).sum();
System.out.println("Sum= " + sum);
I did this using stream, first I used Intstream, then filter out the even indexes and get the mapped the value and added them, and did the same for odd indexes as well, then subtracted it.
我使用流来做到这一点,首先我使用 Intstream,然后过滤掉偶数索引并获取映射的值并添加它们,并对奇数索引也做同样的事情,然后减去它。
回答by Ved
I would use this simple logic to achieve the goal. First add all the odd numbers in the array. Then add all the even numbers from the same. Now subtract the both values n you will get your answer. Hope this helps.
我会用这个简单的逻辑来实现目标。首先将数组中的所有奇数相加。然后添加相同的所有偶数。现在减去这两个值 n 你会得到你的答案。希望这可以帮助。
回答by barsju
I would solve this using a for loop and a boolean flag:
我会使用 for 循环和布尔标志来解决这个问题:
set flag to false
set sum to zero
for alle elements in array
if flag is set
add to sum
else
subtract from sum
When loop is done you have your sum.
回答by infiltr
If You have 4 numbers, for example a[]={1, 3, 5, 6}
, there are several cases:
例如a[]={1, 3, 5, 6}
,如果您有 4 个号码,则有几种情况:
operation:
+ + +
+ + -
+ - +
+ - -
- + +
- + -
- - +
- - -
In your case "operation" will be only + - +
在您的情况下,“操作”将只是 + - +
use array with this symbols and calculate your result.
使用带有此符号的数组并计算结果。
int k=a[0];
for(int i = 1; i<= 3; i++){
if(operation[i-1]=="+".charAt(0))
{k=k+a[i];}
etc...
}
It's not hard :) good luck.
这并不难:) 祝你好运。