所以我在HackerRank(Project Euler Q1)link here上解决了这个问题,并使用以下代码来解决它
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
int T = 0;
ArrayList<Integer> N = new ArrayList<Integer>();
String[] Input = args;
if (args.length>1){
T = Integer.parseInt(args[0]);
if(T<1||T>Math.pow(10,6))
System.exit(0);
}
if (args.length-1 == T){
for (int i=1;i<args.length;i++){
N.add(Integer.parseInt(args[i]));
int sum3=0,sum5=0,sum15=0;
int count=0;
while (count<N.get(i-1)){
sum3+=count;
count+=3;
}
count =0;
while (count<N.get(i-1)){
sum5+=count;
count+=5;
}
count =0;
while (count<N.get(i-1)){
sum15+=count;
count+=15;
}
N.set(i-1,(sum3+sum5-sum15));
}
}
for(int j=0;j<N.size();j++)
System.out.println(N.get(j));
}
}
这在IDE上给出了以下输出:
23
2318
输入时:
2
10
100
这与HackerRank的预期输出相匹配,但是当我在网站上使用此代码时,它说:
输入(stdin)
2
10
100
你的输出(标准输出)
~ no response on stdout ~
预期产出
23
2318
编译器消息
Wrong Answer
我能观察到的一件事是我无法在HackerRank的循环内打印任何东西。解决方案是什么?
答案 0 :(得分:0)
你并没有像HackerRank想要的那样读取STDIN。你应该做这样的事情,而不是从args那里得到你的意见:
Scanner sc = new Scanner(System.in);
int numberOfTestCases = sc.nextInt();
等等。
答案 1 :(得分:0)
我只是运行了这段代码,将其编译好并提交了。
import java.io.*;
public class Solution{
public static void main(String[] args) throws IOException
{
//Input
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());
int[] N = new int[T];
for(int t = 0; t < T; N[t++] = Integer.parseInt(br.readLine())){
}
br.close();
br = null;
//Solve
long[] V = new long[T];
for(int t = 0; t < T; ++t){
int n = N[t] - 1;
V[t] = 3*nSum(n/3) + 5*nSum(n/5) - 15*nSum(n/15);
}
//Print
StringBuilder sb = new StringBuilder();
for(int t = 0; t < T; sb.append(V[t++]).append("\n")){
}
System.out.print(sb);
}
public static long nSum(int n){
long v = n;
return (v*v + v) >> 1;
}
}