簡體   English   中英

java中練習的意外結果

[英]Unexpected results with excercise in java

我有一個關於將某些數字提高到給定冪的練習。 我遇到的確切問題是:
我們使用整數 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)

以 a、b 和 n 的形式為您提供 q 個查詢。 對於每個查詢,將與給定 a、b 和 n 值對應的系列打印為一行 n 個空格分隔的整數。

輸入格式
第一行包含一個整數 q,表示查詢的數量。 后續 q 行中的每一行 i 包含三個空格分隔的整數,分別描述該查詢的 ai、bi 和 ni 值。

輸出格式
對於每個查詢,在新行上打印相應的系列。 每個系列必須按順序打印為一行 n 個空格分隔的整數。

我試過這個代碼:

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();
        }


    }
}

但它未能通過測試,即使“預期輸出”和實際輸出看起來相同。 我嘗試尋找其他解決方案,但我發現的解決方案與我自己的沒有太大區別。

輸入

2
0 2 10
5 3 5

預期產出

2 6 14 30 62 126 254 510 1022 2046
8 14 26 50 98

輸出

2 6 14 30 62 126 254 510 1022 2046
8 14 26 50 98

這幾乎肯定與輸出中的尾隨空格有關:

2 6 14 30 62 126 254 510 1022 2046 | <<= Trailing space
8 14 26 50 98 | <<= Trailing space

修復您的輸出如下:

for(int j = 0; j < n; j++) {
    if (j != 0) {
        System.out.print" ");
    }
    num += (((int) Math.pow(2, j)) * b);
    System.out.print(num);
}

請注意,您完全可以避免調用Math.pow ,因為可以使用位移表達式1 << j來計算 2 的冪; 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();

請檢查以下簡單方法來解決此問題,但請注意,您可以使用遞歸降低時間復雜度,這里我不使用遞歸:

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();

    }

}

這是經過更多優化和清理的完整代碼 - 它將運行您的所有測試用例

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();
  }
}

試試這個完整的代碼...

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