0

runtime error due to timeout

import java.util.*; import java.io.*; class series{ public static void main(String []args){ Scanner in = new Scanner(System.in); System.out.println(""); int q=in.nextInt(); double sum; int a=0,b=0,n=0; int i,j,k; for(int p=0;p<q;p++){ a = in.nextInt(); b = in.nextInt(); n = in.nextInt(); } in.close(); for(i=1;i<=q;i++) { for(j=1;j<=n-1;j++) { sum = 0.0; for(k=1;k<=j;j++) { sum = sum + ((Math.pow(2,k-1))*b); } System.out.println(a + sum); } System.out.println(); }}}

26th Jun 2018, 2:57 PM
Smriti Rastogi
Smriti Rastogi - avatar
2 Respuestas
+ 2
You got two problems. I fixed one of them. https://code.sololearn.com/cDfJIwpb62h5 Your inner most loop was incrementing j instead of k. (CHANGED) Your reading a, b, & n and overwriting them, until you enter the last set.
26th Jun 2018, 3:20 PM
John Wells
John Wells - avatar
0
required series ( a+2^0.b) , (a+2^0.b +2^1.b) , (a+2^0.b +2^1.b +2^2.b),.......(a+2^0.b +2^1.b.....2^(n-i).b) Input Format The first line contains an integer,q , denoting the number of queries. Each line i of the q subsequent lines contains three space-separated integers describing the respective a,b,n values for that query. Sample Input 2 0 2 10 5 3 5 Sample Output 2 6 14 30 62 126 254 510 1022 2046 8 14 26 50 98
26th Jun 2018, 3:02 PM
Smriti Rastogi
Smriti Rastogi - avatar