我正在提高我在黑客等级上的java编程技能,我正试图解决这个问题。Java Loop。
我的输入适用于第一个 a , b 和 n < / em> ,但第二个值不起作用:
2
0 2 10
5 3 5
输出:
2 6 14 30 62 126 254 510 1022 2046
8 19 36 65 118
JAVA代码:
public class Solution {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int number =sc.nextInt();
int loop=1;
int result=0;
/*2
0 2 10
5 3 5
2 6 14 30 62 126 254 510 1022 2046
8 14 26 50 98
*/
while(loop<=number){
int a=sc.nextInt();
int b=sc.nextInt();
int c=b;
int n=sc.nextInt();
result=0;
for(int x=1;x<=n;x++){
result=a+(1*b)+result;
System.out.printf(" "+result +" ");
b*=2;
}
loop++;
}
}
}
答案 0 :(得分:3)
试试这个:
static int[] javaLoops(int a, int b, int n) {
int[] result = new int[n];
for (int i = 0, k = 1, e = a + k * b; i < n; ++i, k *= 2, e += k * b)
result[i] = e;
return result;
}
public void main(String[] args) {
System.out.println(Arrays.toString(javaLoops(0, 2, 10)));
// -> [2, 6, 14, 30, 62, 126, 254, 510, 1022, 2046]
System.out.println(Arrays.toString(javaLoops(5, 3, 5)));
// -> [8, 14, 26, 50, 98]
}
答案 1 :(得分:1)
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 j=0;j<t;j++){
int a = in.nextInt();
int b = in.nextInt();
int n = in.nextInt();
int x=1;
int temp=a;
for(int i=1;i<=n;i++){
temp+=(x*b);
System.out.print(temp+" ");
x*=2;
}
System.out.println();
}
in.close();
}
}
答案 2 :(得分:0)
你在for(x)循环的每次迭代中都会再次添加一个!每个输出的术语只应包括一次。
此外,您的白色间距看起来不正确。每行输出都会有一个尾随空格,每个测试用例之间没有新行。
这似乎是一个有效的解决方案:
import java.util.Scanner;
public class JavaLoops {
public static void main(String[] args) {
/*
INPUT
2
0 2 10
5 3 5
OUTPUT
2 6 14 30 62 126 254 510 1022 2046
8 14 26 50 98
*/
Scanner in = new Scanner(System.in);
int t, tc, a, b, c, n, i;
tc = in.nextInt();
for (t = 0; t < tc; ++t) {
a = in.nextInt();
b = in.nextInt();
c = 0;
n = in.nextInt();
for (i = 0; i < n; ++i) {
System.out.print(a + c + b);
c += b;
b *= 2;
if (i < n - 1) {
System.out.print(" ");
}
}
System.out.println();
}
}
}
答案 3 :(得分:0)
试试这个!
import java.util.Scanner;
public class Loops {
public static void main(String[] args) {
int a, b, n, t,i = 0;
Scanner sc = new Scanner(System.in);
t = sc.nextInt();
while(i < t){
int sum = 0;
a = sc.nextInt();
b = sc.nextInt();
n = sc.nextInt();
for (int k = 0; k < n; k++) {
if(k < 1)
sum += a + ((int)Math.pow(2, k) * b);
else
sum += ((int)Math.pow(2, k) * b);
System.out.printf("%s ",sum);
}
System.out.println();
i++;
}
}
}
答案 4 :(得分:0)
试试这个
import java.util.*;
import java.io.*;
class Solution{
static void series(int a, int b, int n) {
for (int i = 0, k = 1, e = a + k * b; i < n; ++i, k *= 2, e += k * b)
System.out.print(e+" ");
}
public static void main(String[] args) {
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();
series(a,b,n);
System.out.println("");
}
}
}
答案 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();
for(int i=0;i<t;i++){
int a = in.nextInt();
int b = in.nextInt();
int n = in.nextInt();
int sum = 0;
int pow1 = 0;
for(int j = 0; j<n;j++){
pow1 = pow1 + ((int)Math.pow(2,j) * b);
sum = a + pow1 ;
System.out.print(sum + " ");
}
System.out.println();
}
in.close();
}
}
答案 6 :(得分:0)
public static void EasyJavaLoop2(){
Scanner in = new Scanner(System.in);
int t=in.nextInt();
int a =0 , b = 0 , n = 0;
for(int k=0;k<t;k++){
a = in.nextInt();
b = in.nextInt();
n = in.nextInt();
int sum = 0;
int stage = 1;
sum += a+(stage*b);
for (int i = 0; i <= n-1; i++) {
System.out.print(sum + " ");
stage=stage*2;
sum += (stage*b);
}System.out.println("");
}
in.close();
}
答案 7 :(得分:-1)
{% %}