我是编码新手。使用来自控制台的输入时,程序的输入始终显示 0 输出
I am new to coding. The input of the program is showing always 0 output while using input from console
当我 运行 我的代码输出时总是显示 0。这段代码是关于二进制搜索的。我正在使用缓冲区 reader 作为输入。
import java.util.*;
import java.lang.*;
import java.io.*;
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
Main mainclass= new Main();
int t =Integer.parseInt(in.readLine());
for(int i = 0;i<t;i++)
{ StringTokenizer tokenizer = new StringTokenizer(in.readLine());
int N = Integer.parseInt(tokenizer.nextToken());
int C = Integer.parseInt(tokenizer.nextToken());
int[] arr= new int[N];
for(int x=0;x<N;x++)
{ arr[i] =Integer.parseInt(in.readLine());}
int res= mainclass.bs(N,C,arr);
System.out.println(res);
}
}
it is bs() method inside Main class. I am actually solving problem on aggresive cows of SPOJ.
here is the link: https://www.spoj.com/problems/AGGRCOW/
public int bs(int N,int C,int[] arr)
{ Arrays.sort(arr);
int left = 0;int right = arr[N-1]-arr[0];int mid = arr[N-1]-arr[0];
int check = 0; int max= -1;
while(left<right){
int temp= checker(mid,arr);
if(temp>=C)
{ if(max<mid)
max=mid;
left = mid+1;
mid = (left+right)/2; }
else
{
right= mid;
mid=(left+right)/2;
}
}
return max;
}
public int checker(int mid,int[] arr)
{ int N = arr.length;int f=0;int cows=1;
for(int i=0;i<N-1;i++)
{
if((arr[i+1]-arr[f])>=mid)
{
f=i+1;
cows++;
}
}
return cows;
}
}
找到你的问题,它是填充初始数组。
我在这里自己修改了你的代码,使用 Scanner 和 split(也删除了一些不相关的代码),但问题是一样的:
import java.util.Arrays;
import java.util.Scanner;
class Main
{
public static void main (String[] args)
{
Scanner scan = new Scanner(System.in);
int t =scan.nextInt(); scan.nextLine();
for(int i = 0;i<t;i++)
{
String[] input = scan.nextLine().split(" ");
int N = Integer.parseInt(input[0]);
int C = Integer.parseInt(input[1]);
int[] arr= new int[N];
for(int x=0;x<N;x++){
arr[i] = scan.nextInt();
scan.nextLine();
}
int res= bs(N,C,arr);
System.out.println("RES = " + res);
}
}
public static int bs(int N,int C,int[] arr) {
Arrays.sort(arr);
int left = 0;int right = arr[N-1]-arr[0];int mid = arr[N-1]-arr[0];
int max= -1;
while(left<right){
int temp= checker(mid,arr);
if(temp>=C){
if(max<mid)
max=mid;
left = mid+1;
mid = (left+right)/2; }
else {
right= mid;
mid=(left+right)/2;
}
}
return max;
}
public static int checker(int mid,int[] arr){
int f=0;int cows=1;
for(int i=0;i<arr.length-1;i++) {
if((arr[i+1]-arr[f])>=mid) {
f=i+1;
cows++;
}
}
return cows;
}
}
你的问题出在这里:
for(int x=0;x<N;x++){
arr[i] = scan.nextInt();
scan.nextLine();
}
i
的值永远不会改变,因此您将所有这些整数放在数组的同一索引上。将其更改为:
for(int x=0;x<N;x++){
arr[x] = scan.nextInt();
scan.nextLine();
}
当我 运行 我的代码输出时总是显示 0。这段代码是关于二进制搜索的。我正在使用缓冲区 reader 作为输入。
import java.util.*;
import java.lang.*;
import java.io.*;
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
Main mainclass= new Main();
int t =Integer.parseInt(in.readLine());
for(int i = 0;i<t;i++)
{ StringTokenizer tokenizer = new StringTokenizer(in.readLine());
int N = Integer.parseInt(tokenizer.nextToken());
int C = Integer.parseInt(tokenizer.nextToken());
int[] arr= new int[N];
for(int x=0;x<N;x++)
{ arr[i] =Integer.parseInt(in.readLine());}
int res= mainclass.bs(N,C,arr);
System.out.println(res);
}
}
it is bs() method inside Main class. I am actually solving problem on aggresive cows of SPOJ. here is the link: https://www.spoj.com/problems/AGGRCOW/
public int bs(int N,int C,int[] arr)
{ Arrays.sort(arr);
int left = 0;int right = arr[N-1]-arr[0];int mid = arr[N-1]-arr[0];
int check = 0; int max= -1;
while(left<right){
int temp= checker(mid,arr);
if(temp>=C)
{ if(max<mid)
max=mid;
left = mid+1;
mid = (left+right)/2; }
else
{
right= mid;
mid=(left+right)/2;
}
}
return max;
}
public int checker(int mid,int[] arr)
{ int N = arr.length;int f=0;int cows=1;
for(int i=0;i<N-1;i++)
{
if((arr[i+1]-arr[f])>=mid)
{
f=i+1;
cows++;
}
}
return cows;
}
}
找到你的问题,它是填充初始数组。
我在这里自己修改了你的代码,使用 Scanner 和 split(也删除了一些不相关的代码),但问题是一样的:
import java.util.Arrays;
import java.util.Scanner;
class Main
{
public static void main (String[] args)
{
Scanner scan = new Scanner(System.in);
int t =scan.nextInt(); scan.nextLine();
for(int i = 0;i<t;i++)
{
String[] input = scan.nextLine().split(" ");
int N = Integer.parseInt(input[0]);
int C = Integer.parseInt(input[1]);
int[] arr= new int[N];
for(int x=0;x<N;x++){
arr[i] = scan.nextInt();
scan.nextLine();
}
int res= bs(N,C,arr);
System.out.println("RES = " + res);
}
}
public static int bs(int N,int C,int[] arr) {
Arrays.sort(arr);
int left = 0;int right = arr[N-1]-arr[0];int mid = arr[N-1]-arr[0];
int max= -1;
while(left<right){
int temp= checker(mid,arr);
if(temp>=C){
if(max<mid)
max=mid;
left = mid+1;
mid = (left+right)/2; }
else {
right= mid;
mid=(left+right)/2;
}
}
return max;
}
public static int checker(int mid,int[] arr){
int f=0;int cows=1;
for(int i=0;i<arr.length-1;i++) {
if((arr[i+1]-arr[f])>=mid) {
f=i+1;
cows++;
}
}
return cows;
}
}
你的问题出在这里:
for(int x=0;x<N;x++){
arr[i] = scan.nextInt();
scan.nextLine();
}
i
的值永远不会改变,因此您将所有这些整数放在数组的同一索引上。将其更改为:
for(int x=0;x<N;x++){
arr[x] = scan.nextInt();
scan.nextLine();
}