I have an exercise about raising certain numbers to a given power. The exact one I have problems with:
We use the integers a, b, and n to create the following series:
(a + 2^0 * b), (a + 2^0 * b + 2^1 * b), ... ,(a + 2^0 * b + 2^1 * b + ... + 2^n-1 * b)
You are given q queries in the form of a, b, and n. For each query, print the series corresponding to the given a, b, and n values as a single line of n space-separated integers.
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 ai, bi, and ni values for that query.
Output Format
For each query, print the corresponding series on a new line. Each series must be printed in order as a single line of n space-separated integers.
I tried this code:
import java.util.*;
import java.lang.Math.*;
class Playground {
public static void main(String[ ] args) {
Scanner in = new Scanner(System.in);
int q = in.nextInt();
for(int i = 0; i < q; i++) {
int a = in.nextInt();
int b = in.nextInt();
int n = in.nextInt();
int num = a;
for(int j = 0; j < n; j++) {
num += (((int) Math.pow(2, j)) * b);
System.out.print(num + " ");
}
System.out.println();
}
}
}
But it failed the test, even though the "Expected output" and the actual output looked the same. I tried searching for other solutions, but the ones I found were not that different from my own.
Input
2
0 2 10
5 3 5
Expected Output
2 6 14 30 62 126 254 510 1022 2046
8 14 26 50 98
Output
2 6 14 30 62 126 254 510 1022 2046
8 14 26 50 98
This is almost certainly related to the trailing space in your output:
2 6 14 30 62 126 254 510 1022 2046 | <<= Trailing space
8 14 26 50 98 | <<= Trailing space
Fix your output as follows:
for(int j = 0; j < n; j++) {
if (j != 0) {
System.out.print" ");
}
num += (((int) Math.pow(2, j)) * b);
System.out.print(num);
}
Note that you can avoid calling Math.pow
at all, because powers of 2 can be computed using bit shift expression 1 << j
; multiplication of b
by 1 << j
is equivalent to shifting b
left by j
:
for(int j = 0; j < n; j++) {
if (j != 0) {
System.out.print" ");
}
num += (b << j);
System.out.print(num);
}
public static void main(String []argh){
Scanner in = new Scanner(System.in);
int t=in.nextInt();
for(int i=0;i<t;i++){
int a = in.nextInt();
int b = in.nextInt();
int n = in.nextInt();
int power = 1,sum=0;
for(int j=0;j < n;j++)
{
sum=a+(power*b);
System.out.print(sum+" ");
power = power * 2;
power++;
}
System.out.println("");
}
in.close();
}
//where 2^0*b, 2^0*b + 2^1*b, 2^0*b + 2^1*b + 2^2*b .....,2^(k+1) - 1
import java.util.*;
import java.io.*;
import java.lang.Math;
class Solution{
public static void main(String []argh){
Scanner in = new Scanner(System.in);
int t=in.nextInt();
for(int i=0;i<t;i++){
int a = in.nextInt();
int b = in.nextInt();
int n = in.nextInt();
int count = 0;
for(int j=0;j<n;j++) {
System.out.print((int)(a+b*(Math.pow(2,j + 1)-1))+" ");
}
System.out.println();
}
in.close();
}
}
for (int j = 0; j < n; j++ ){
if(j==0){
output = output + (a + (int)Math.pow(2,j) * b);
}
else{
output = output + ((int)Math.pow(2,j) * b);
}
System.out.print(output+" ");
}
System.out.println();
Please check the following simple method to solve this problem but mind it, you can decrease time complexity using recursion, here I am going without recursion:
import java.util.*;
import java.io.*;
import java.lang.Math;
class Solution{
public static void main(String []argh){
Scanner in = new Scanner(System.in);
int t=in.nextInt();
for(int i=0;i<t;i++){
int a = in.nextInt();
int b = in.nextInt();
int n = in.nextInt();
double sum=0;
for(int j=1;j<=n;j++)
{
sum=a;
for(int k=0;k<j;k++)
{
sum=sum+(b*Math.pow(2, k));
}
System.out.print((int)sum+" ");
}
System.out.println();
}
in.close();
}
}
import java.util.*;
import java.io.*;
class Solution{
public static void main(String []argh){
Scanner in = new Scanner(System.in);
int t=in.nextInt();
int a =0,b=0,n=0;
for(int i=0;i<t;i++){
a = in.nextInt();
b = in.nextInt();
n = in.nextInt();
arrange(a,b,n);
}
in.close();
}
public static void arrange(int a,int b,int n){
int sum = a+b;
for(int j=1; j<=n; j++){
System.out.print(sum+" ");
sum+=((Math.pow(2,j))*b);
}
System.out.println();
}
}
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int TestCase = sc.nextInt();
while (TestCase-- > 0) {
int a = 0, b = 0, n = 0;
a = sc.nextInt();
b = sc.nextInt();
n = sc.nextInt();
int sum = a + b;
for (int i = 1; i < n;) {
System.out.print(sum + " ");
sum += ((int) Math.pow(2, i) * b);
i++;
if (i == n) {
System.out.print(sum);
}
}
System.out.println();
}
sc.close();
}
}
Here's the full code with more optimized and cleaned - It will run all your test cases
import java.util.*;
import java.io.*;
class Solution
{
public static void main(String []argh)
{
Scanner in = new Scanner(System.in);
int t=in.nextInt();
for(int i=0;i<t;i++)
{
int a = in.nextInt();
int b = in.nextInt();
int n = in.nextInt();
int s=0;
s=s+a;
for(int j=0;j<n;j++)
{
s+=(Math.pow(2,j))*b;
System.out.print(s+" ");
}
System.out.println();
}
in.close();
}
}
import java.util.*;
import java.io.*;
class Solution{
public static void main(String []argh){
Scanner in = new Scanner(System.in);
int t=in.nextInt();
for(int i=0;i<t;i++){
int result = 0;
int a = in.nextInt();
int b = in.nextInt();
int n = in.nextInt();
for (int j = 0; j < n; j++ ){
if(j==0){
result = result + (a + (int)Math.pow(2,j) * b);// for (a + 2^0 * b)
}
else{
result = result + ((int)Math.pow(2,j) * b); // for (a + 2^0 * b + 2^1 * b)
}
System.out.print(result+" ");
}
System.out.println();
}
in.close();
}
}
Try this completed code...
import java.util.*;
import java.io.*;
class Solution{
public static void main(String []argh){
Scanner in = new Scanner(System.in);
int t=in.nextInt();
for(int i=0;i<t;i++){
int a = in.nextInt();
int b = in.nextInt();
int n = in.nextInt();
for(int x=0; x<n; x++){
a+=(Math.pow(2,x)*b);
System.out.print(a+" ");
}
System.out.println();
}
in.close();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.