簡體   English   中英

如何將for循環的每次迭代結果存儲到Java中的數組中

[英]How to store the result of each iteration of a for loop into an array in java

分數查找器(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的科目,並且特定的學生在這3個科目中的得分分別為A(減),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

我想將第一迭代的每個值與第二迭代的所有值相加。 i,e 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

為了實現這一點,我需要存儲每次迭代的值,但是我被困在這里,請大家幫我擺脫這個問題,謝謝。

如果您不想存儲在多維數組中,則需要將每個迭代存儲在一個多維數組中,如下所示。 但是此代碼僅適用於兩個學分。

public class CandidateCode {

        private static int possibleCombination(int input)
        {   int i=0;
            int[] a=new int[Grades.length];
             a[i]=input;
              return 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];
            int[] creditarr = new int[10];
            int[] credit1 = new int[10];
            int[] credit2 = new int[10];
            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]);
                     creditarr[j]=possibleCombination(totalSummation);
                     if(Credit[i]==1) {
                     credit1[j]=creditarr[j];  
                     }
                     if(Credit[i]==2){
                     credit2[j]=creditarr[j];
                     }
                    }
                }

            for(int k=0;k<credit1.length;k++) {
                for(int l=0;l<credit2.length;l++) {
                    int final_no=credit1[k]+credit2[l];
                    System.out.println("final_no :"  +final_no);
                }
            }
            }

         }

這是你想要的嗎?

我想下面的代碼將是最好的解決方案。

public class Candidate {
 private static final int[] Grades = new int[]{10, 9, 8, 7, 6, 5};

public static void main(String[] args) {
    // TODO Auto-generated method stub
    int i=0,score=0,totalsummation=0;
    Scanner uInput = new Scanner(System.in);
    System.out.println("Enter length of Array:");
    int arraylength = uInput.nextInt();             
    int[] credits= new int[arraylength];
    ArrayList<Integer> combination = new ArrayList<Integer>();
    for (i = 0; i <credits.length; i++) {
        credits[i] = uInput.nextInt();  
    }
    switch(credits.length) {
    case 1:
        for(int c1=10;c1>=5;c1--) {
            totalsummation = c1*credits[0];
            combination.add(totalsummation);
        }
        break;

    case 2:
        for(int g:Grades) {
            for(int c2=10;c2>=5;c2--) {
                totalsummation=g*credits[0]+c2*credits[1];
                combination.add(totalsummation);
            }
        }
        break;

    case 3:
            for(int g:Grades) {
            for(int c3=10;c3>=5;c3--) {
                totalsummation=g*credits[0]+c3*credits[1]+c3*credits[2];
                combination.add(totalsummation);
            }
        }
            break;
    case 4:
        for(int g:Grades) {
            for(int c4=10;c4>=5;c4--) {
                totalsummation=g*credits[0]+c4*credits[1]+c4*credits[2]+c4*credits[3];
                combination.add(totalsummation);    
            }
        }
        break;

    case 5:
        for(int g:Grades) {
            for(int c5=10;c5>=5;c5--) {
                totalsummation=g*credits[0]+c5*credits[1]+c5*credits[2]+c5*credits[3]+c5*credits[4];
                combination.add(totalsummation);
            }
        }
        break;

        default:
            System.out.println("Invalid Input");
    }



 ArrayList<Integer> distinctnos =(ArrayList<Integer>) combination.stream().distinct().collect(Collectors.toList());
     System.out.println(distinctnos.size());            }
}

希望這能回答您的問題。

    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());

       }
    }

好吧,下面我們在注釋中進行討論:您不需要兩個for循環。

for i = 0; i < array.size(); i++ {
    sum += grade[i]*credit[i]
 }

對不起,通用編碼風格。 是你想要的嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM