我有一个关于将某些数字提高到给定权力的练习。
我遇到的确切问题:
我们使用整数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
答案 0 :(得分:2)
这几乎肯定与输出中的尾随空格有关:
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);
}
答案 1 :(得分:0)
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();
}
答案 2 :(得分:0)
//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();
}
}
答案 3 :(得分:0)
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();
答案 4 :(得分:0)
请检查以下简单方法来解决此问题,但请注意,您可以使用递归降低时间复杂度,这里我无需递归:
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();
}
}
答案 5 :(得分:0)
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();
}
}
答案 6 :(得分:0)
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();
}
}
答案 7 :(得分:0)
这是经过优化和清理的完整代码-它将运行您的所有测试用例
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();
}
}
答案 8 :(得分:0)
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();
}
}
答案 9 :(得分:0)
试试这个完整的代码...
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();
}
}