for 루프의 각 반복 결과를 Java의 배열에 저장하는 방법

지아

점수 찾기 (100 점)

Praveen은 학교에서 컴퓨터 과학 교사로 일자리를 찾고 있습니다. 그는 여러 학교에서 여러 번 거절 당했지만 이번에는 일자리를 구하기로 결심했습니다. 그는 학교 St. Mary의 교장에게갑니다.

교장은 자신의 학교에 채점 및 학점 시스템이 있다고 말합니다. N 개의 과목이 있고 각 과목은 그것에 첨부 된 학점 Ci가 있습니다 (1 <= i <= N). 각 학생은 각 과목에서 특정 성적을 받고 각 성적에는 다음과 같은 점수가 있습니다.

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

이제 각각 4 학점, 3 학점, 2 학점의 3 개 과목이 있고이 3 개 과목에서 각각 특정 학생의 점수가 A (마이너스), B 및 C 인 경우 그의 점수는 다음과 같이 계산됩니다. 총점 = Grade point의 곱의 합계 각 과목에 대한 해당 학점.

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

그는 Praveen이 각 과목에 다른 등급을 할당하여 N 과목의 학점을받은 학생에게 부여 할 수있는 총 개별 점수를 알려주기를 원합니다.

입력 형식 함수에는 단일 인수 (각각 해당 주제의 학점을 나타내는 N 요소의 1 차원 정수 배열 A)가 포함됩니다. 입력의 첫 번째 줄에는 배열의 크기를 나타내는 정수 N이 포함됩니다. i 번째 주제의 신용 Ci를 나타내는 정수를 포함하는 다음 N 줄의 입력

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 + 2는 9,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());

       }
    }

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

$ .each () 루프 내에서 $ .ajax () 비동기를 유지하지만 각 반복의 결과에 반응하는 방법

for 루프의 목록에 결과를 저장하는 방법

Pandas 데이터 프레임의 각 열과 각 셀을 반복하는 방법

for 루프의 각 반복을 데이터베이스 저장소의 변수에 할당하는 방법

R-루프 함수의 각 반복에 대해 벡터를 저장하는 방법

루프의 각 반복에 대해 지연을 두는 방법

plt.bar의 결과를 배열에 저장하는 방법

Pandas DataFrame의 행에 대한 반복 결과를 새 열에 저장하는 방법은 무엇입니까?

R에서 각 for 루프 반복의 결과를 연결된 문자열을 인쇄하는 방법

결과가 각 반복을 재설정 할 때 for 루프의 결과를 데이터 프레임에 추가하는 방법이 있습니까?

for 루프의 결과를 데이터 프레임에 저장하는 방법

R의 각 루프 반복 끝에 새 변수를 만드는 방법

Python에서 Alexnet의 그라디언트를 numpy 배열 (각 반복)로 저장하는 방법은 무엇입니까?

np 배열의 두 for 루프 안에 계산 된 결과를 저장하는 방법은 무엇입니까?

JSON 배열의 각 요소를 반복하고 행과 결합

LightGBM을 사용하여 GridSearchCV의 각 반복에서 모든 예측 결과를 저장하는 방법

C의 팩토리얼 알고리즘 : 이전 반복의 결과를 문자열에 저장하는 방법?

foreach 루프의 각 반복에서 반환 된 데이터를 비교하는 방법

모든 루프의 결과를 csv에 저장하는 방법

각 반복의 루프 출력을 데이터 프레임에 저장하는 방법

루프의 각 반복을 innerHTML로 저장하는 방법

루프의 각 반복이 내부 루프의 반복마다 한 번 발생하는 C에서 연속 루프를 만드는 방법

for 루프 반복의 결과를 사전에 저장

R에서 루프의 각 반복을 별도의 변수에 저장하는 방법이 있습니까?

루프에서 변수의 결과를 저장하는 방법

데이터 프레임에서 R의 모든 반복 루프 결과를 저장하는 방법

jq로 객체의 JSON 배열을 반복하고 각 루프의 각 객체에서 여러 변수를 가져오는 방법

각 반복에 대한 그래프 제목에 pearsonr 루프의 결과를 인쇄하는 방법

각 for 루프의 끝에서 결과를 저장하는 방법