简体   繁体   English

java中练习的意外结果

[英]Unexpected results with excercise in java

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、b 和 n 来创建以下系列:

 (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.以 a、b 和 n 的形式为您提供 q 个查询。 For each query, print the series corresponding to the given a, b, and n values as a single line of n space-separated integers.对于每个查询,将与给定 a、b 和 n 值对应的系列打印为一行 n 个空格分隔的整数。

Input Format输入格式
The first line contains an integer, q, denoting the number of queries.第一行包含一个整数 q,表示查询的数量。 Each line i of the q subsequent lines contains three space-separated integers describing the respective ai, bi, and ni values for that query.后续 q 行中的每一行 i 包含三个空格分隔的整数,分别描述该查询的 ai、bi 和 ni 值。

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.每个系列必须按顺序打印为一行 n 个空格分隔的整数。

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 ;请注意,您完全可以避免调用Math.pow ,因为可以使用位移表达式1 << j来计算 2 的幂; multiplication of b by 1 << j is equivalent to shifting b left by j : b乘以1 << j相当于将b左移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();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM