close

題目取自於ITSA協作平台
題目:計算陣列中所有元素的立方和

問題描述:
試撰寫一個程式,宣告一個 1 維的整數陣列,並計算元素中所有元素的立方和。

輸入說明
任意輸入六個整數。

輸出說明:
輸出所有元素的立方和。

範例:

Sample Input:

Sample Output:

30 20 10 5 34 44

160613

 

Code:

  1. import java.util.Scanner;  
  2. public class Main{  
  3.     public static void main(String args[]){  
  4.         Scanner sc = new Scanner(System.in);      
  5.         while(sc.hasNext()){  
  6.             int sum=0;  
  7.             int arr[] = new int[6];  
  8.             for(int a=0;a<6;a++){  
  9.                 //將每筆測資存到陣列
  10.                 arr[a] = sc.nextInt();
  11.                 //sum每次都讓他加上測資的立方
  12.                 sum+=arr[a]*arr[a]*arr[a];
  13.             }  
  14.             System.out.println(sum);  
  15.         }  
  16.     }  
  17. }  
arrow
arrow
    創作者介紹
    創作者 ニャー提督 的頭像
    ニャー提督

    ニャー提督的提督室

    ニャー提督 發表在 痞客邦 留言(0) 人氣()