gpt4 book ai didi

java - java中如何将for循环每次迭代的结果存储到数组中

转载 作者:行者123 更新时间:2023-11-30 02:18:15 25 4
gpt4 key购买 nike

分数查找器(100 分)

Praveen 正在学校找到一份计算机科学教师的工作。他曾多次被不同学校拒绝,但这一次他决心得到这份工作。他去找圣玛丽学校的校长。

校长说他的学校有评分和学分制度。有 N 个科目,每个科目都有一个学分 Ci (1 <= i <= N)。每个学生在每个科目中都会获得特定的成绩,每个成绩都有一个分值,分别是:

A = 10, 
A(minus) = 9,
B = 8,
B(minus) = 7,
C = 6,
C(minus) = 5

现在如果有 3 个科目分别为 4、3 和 2 学分,并且某个学生的成绩为 A(负),这 3 个科目分别为 B 和 C,那么他的分数将计算如下:总分=各科目绩点与相应学分的乘积之和。

= ( (9*4) + (3*8) + (2*6) ) = 72.

他希望 Praveen 说出可能的总分通过为每个科目分配不同的成绩来授予获得 N 个科目学分的学生。

输入格式您的函数包含一个参数 - 一个由 N 个元素组成的一维整数数组 A,其中每个元素代表该主题的学分。输入的第一行包含一个整数 N 表示数组的大小。接下来的 N 行输入,每行包含一个表示第 i 个科目的学分 Ci 的整数

Constraints
1 <= N <= 100
1 <= Ci <= 5

输出格式您必须返回一个整数,表示可能给出的分数总数。

Sample TestCase 1
Input

2
1
2
Output

16

现在我正在编写这样的代码

package javaapplication1;

import java.util.Scanner;

public class CandidateCode {
private static void possibleCombination(int input) {
int i=0;
int[] a=new int[Grades.length];

a[i]=input;
System.out.println("the a is "+a[i]);
}

private static final int[] Grades = new int[]{10, 9, 8, 7, 6, 5, 4, 3, 2, 1};

public static void main(String[] args) {
int i=0,j,totalSummation=0;
Scanner uInput = new Scanner(System.in);
System.out.println("Enter length of Array:");
int index = uInput.nextInt();
int[] Credit = new int[index];
for (i = 0; i <= Credit.length-1; i++) {
Credit[i] = uInput.nextInt();
System.out.println("credit is" + Credit[i]);
for ( j = 0; j <= Grades.length - 1; j++) {
totalSummation = +(Grades[j] * Credit[i]);
possibleCombination(totalSummation);
// System.out.println("total sum is " + totalSummation);
}
}
}
}

现在我想存储每次迭代中计算的值......含义为首先迭代,值为 10,9,8,7,6,5,4,3,2,1第二次迭代的值为 20,18,16,14,10,8,6,4,2

我想将第一次迭代的每个值与第二次迭代的所有值相加。即 10+20、10+18、10+16、10+14、10+10、10+8、10+6、10+4、10+29,8,7,6,5,4,3,2,1 类似

为了实现这一目标,我需要存储每次迭代的值,但我被困在这里,请帮助我解决这个问题,提前谢谢。

最佳答案

    import java.io.*;
import java.util.*;
import java.util.stream.Collectors;
public class Sample {
public static void main(String args[] ) throws Exception {

Scanner scan = new Scanner(System.in);
int nValue = scan.nextInt();
if(nValue<1 || nValue>100){
System.out.println("Array Value cannot be less than 1 or greater than 100");
}
int[] inputCredits = new int[nValue];
for( int i=0 ; i < nValue ; i++){
inputCredits[i]=scan.nextInt();
if(inputCredits[i]<1 || inputCredits[i]>5){
System.out.println("Credit Value cannot be less than 1 or greater than 5");
}
}
List<Integer> list1 = Arrays.stream(inputCredits).boxed().collect(Collectors.toList());
List<Integer> list2 = Arrays.asList(5,6,7,8,9,10);
//checked for basic constraints up till this point
//Next what we multiply all the inputted Credits with the possible grades and
// store in the list where x input Credits will produce a list of x*6 entries
// where first six entries are the possibilities for the first Credit, next 6
// entries for the 2nd credit and so on, having this knowledge we loop through // the list to get all the possible scores
List<Integer> permutedList = list1.stream().flatMap(i -> list2.stream().map(j -> i*j)).collect(Collectors.toList());

List<Integer> listForDistinctSet= new ArrayList<Integer>();
for(int i=0; i<6 ; i++){
listForDistinctSet.add(permutedList.get(i));
}
Set<Integer> distinctSet = new HashSet<Integer>();

for(int j=6,k=j+6;k<=permutedList.size();j=k,k=k+6){
Set<Integer> newSet = new HashSet<Integer>();
for(int i=0; i<listForDistinctSet.size(); i++){

for(; j<k ; j++){
int sum = listForDistinctSet.get(i) + permutedList.get(j);
newSet.add(sum);
}
j=k-6;
}
distinctSet=newSet;
listForDistinctSet = new ArrayList<>(distinctSet);
}
System.out.println(distinctSet.size());

}
}

关于java - java中如何将for循环每次迭代的结果存储到数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47631247/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com