2017-02-25 73 views
0
import java.util.*; 
public class Main { 

     Scanner input = new Scanner (System.in); 
     int n = 0, tn = 0, time = 0;int sum=0; 
     int t = input.nextInt(); //no. of test cases 
     for (int i =0; i<t; i++) 
     { 
      n = input.nextInt();//no. of timings   
      for (int j = 0; j<n; j++) 
      { 
       tn = input.nextInt(); //individual time 

       sum=0; 
       sum+=tn; 
       sum*=2; 
      } 
      System.out.println(t+". "+sum); 
     } 

    } 
} 

My output沒有得到以下代碼實際輸出:

output I am supposed to get

誰能告訴我在哪裏出了錯?

回答

1

1)要設置每次sum=0同時採取新的輸入,這樣你就失去了,因此以前的值最後一次

sum=30 
sum= 30*2 = 60 

所以復位sum=0當你與你的第一個輸入的情況下完成

2.)您需要在將所有數值合計後進行乘法運算,以便當您有sum的所有值時individual time的數值爲

for (int i = 0; i < t; i++) { 
    n = input.nextInt();// no. of timings 
    for (int j = 0; j < n; j++) { 
     tn = input.nextInt(); // individual time 
     // add all values first 
     sum += tn; 
    } 
    // multiply the total of values with 2 
    System.out.println(i + ". " + (sum * 2)); 

    // now set sum=0 for next case 
    sum = 0; 
} 

測試用例輸出:

2 
3 
10 
20 
30 
2. 120 // output of first case 
2 
100 
50 
2. 300 // output of second case 
+0

@Madhurya克里希南也出事了? –

+1

謝謝你的工作完美! –

+0

是的,我剛剛做了:) –