Runtime Error on CodeChef KOL1506 - java

When i compiled and ran this code on Eclipse, this runs fine but on Codechef it shows a runtime error. Can anyone help me find why it does not run on the website. The website link is: https://www.codechef.com/viewsolution/10987533
and the problem link is : https://www.codechef.com/problems/KOL1506/
import java.util.Scanner;
class SamosaBhai {
public static void main(String[] args){
int n =0;
Scanner keyboard = new Scanner(System.in);
Scanner input = new Scanner(System.in);
System.out.println("Enter number of test Cases:");
n = keyboard.nextInt();
int[] ans = new int[n];
for(int i = 0; i< n ;i++){
//no. of houses and power 'd' is stored here
String get = input.nextLine();
String[] numarray = get.split(" "); // splitting string by spaces
int num = Integer.parseInt(numarray[0]); // number of houses
int d = Integer.parseInt(numarray[1]); // power to be raised
// positions of the houses is stored here
String entry = input.nextLine();
Scanner scanner = new Scanner(entry);
int[] pos = new int[num];
for (int j= 0;j<num;j++) {
pos[j] = scanner.nextInt();
}
scanner.close();
ans[i] = postalCharge(pos, d, n);
}
keyboard.close();
input.close();
for(int p =0; p<n; p++)
System.out.println(ans[p]);
}
// function to calculate the postal charges between all the houses
public static int postalCharge(int[] location, int d, int n){
int total =0;
for (int i = 0; i<n; i++){
for(int j =0; j< n; j++){
int n1 = location[i];
int n2 = location[j];
total += Math.pow(Math.abs(n1-n2),d);
}
}
return total;
}
}

Related

Finding the minimum of scanner input in a method

I am trying to find the minimum of a variable input of the scanner class. I have as many inputs as the user wants but I cannot seem to find out how to find the minimum of multiple inputs. Any help would be appreciated.
public static void minimum(int count)
{
double input;
boolean lessThan;
double lesser = 0;
for(count = count; count > 0; count--)
{
System.out.print("Enter a double: ");
input = console.nextDouble();
lessThan = input < input;
if(lessThan = true)
{
lesser = input;
}
else
{
lesser = input;
}
}
System.out.println("The minimum is " + lesser);
}
public static void main(String [] args){
Scanner scanner = new Scanner(System.in);
System.out.println("How many inputs?: ");
int answer = scanner.nextInt();
int arr[] = new int[answer];
int y = 0;
while(y < answer){
System.out.println("Enter a value: ");
arr[y] = scanner.nextInt();
y++;
}
int smallNum = arr[arr.length - 1];
for(int i = arr.length; i > 0; i--){
if(smallNum > arr[i - 1]){
smallNum = arr[i - 1];
}
}
System.out.println("Minimum is: " + smallNum);
}
Here is the answer but my answer is a little bit different. Firstly I created an initialized array because we need to execute the code several times, then I stored the user inputs into the array after that I found the min value using array indexes.
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double arr[] = new double[5];
for (int i = 0; i < arr.length; i++) {
System.out.println("Enter the double");
arr[i] = scanner.nextDouble();
}
double min = arr[0];
for (int j=0;j<arr.length;j++){
if(arr[j]<min)
min=arr[j];
}
System.out.println("min value is"+" "+min);
}

I have to print the array , but for each loop is not working

import java.util.Scanner;
public class TeacherCoins {
public static void main(String[] args){
int k;
System.out.print("Enter total number of coins :");
Scanner sc = new Scanner(System.in);
k =sc.nextInt();
int [] arr= new int[k-1];
System.out.print("Enter array :");
for(int i=0;i<=(k-1);i++)
{
Scanner sc1 = new Scanner(System.in);
arr[i] =sc1.nextInt();
}
for(int element :arr)
{
System.out.println("Print array");
System.out.println(element);
}
}
I am giving output using Scanner class. But is not printing the array.
You don't need to declare the Scanner again inside the loop. Another thing that you should do to be sure of your code, is to have this condition on the loop if i < arr.length. Lastly, I moved the "Print array" message outside the last for.
public class TeacherCoins {
public static void main(String[] args){
int k;
System.out.print("Enter total number of coins :");
Scanner sc = new Scanner(System.in);
k =sc.nextInt();
int [] arr = new int[k];
System.out.print("Enter array :");
for(int i=0; i<arr.length; i++) {
arr[i] =sc1.nextInt();
}
System.out.println("Print array");
for(int element :arr){
System.out.println(element);
}
}
}
There are two problems with this code,
1. You don't need to create a new Scanner object for each user input.
2. You are declaring an array size of k-1 and then asking user input k times.
public static void main(String[] args) {
int k;
System.out.print("Enter total number of coins :");
Scanner sc = new Scanner(System.in);
k = sc.nextInt();
int[] arr = new int[k]; // To store k elements, you need k sized array
System.out.print("Enter array :");
for (int i = 0; i <= (k - 1); i++) {
// Scanner sc1 = new Scanner(System.in); / / Not required here
arr[i] = sc.nextInt();
}
System.out.println("Print array");
for (int element : arr) {
System.out.println(element);
}
}
There is only one problem in your code you are declaring the size of array as k-1 instead declare it for k elements.You just need to give each inputs in new line. For more refer this :Scanner class.
And your for each loop is correct and working.
import java.util.Scanner;
public class TeacherCoins {
public static void main(String[] args){
int k;
System.out.print("Enter total number of coins :");
Scanner sc = new Scanner(System.in);
k =sc.nextInt();
int [] arr= new int[k];//it was giving java.lang.ArrayIndexOutOfBoundsException
System.out.print("Enter array :");
for(int i=0;i<=(k-1);i++)
{
Scanner sc1 = new Scanner(System.in);//not required
arr[i] =sc1.nextInt();//use arr[i] =sc.nextInt();
}
for(int element :arr)
{
System.out.println("Print array");
System.out.println(element);
}
}
}
Above code will work. But you don't need new scanner objects for taking input, creating only single object will work.
import java.util.Scanner;
public class TeacherCoins {
public static void main(String[] args) {
int k;
System.out.print("Enter total number of coins :");
Scanner sc = new Scanner(System.in);
k = sc.nextInt();
int[] arr = new int[k];
System.out.print("Enter array :");
for (int i = 0; i < arr.length; i++) {
arr[i] = sc.nextInt();
}
sc.close();
System.out.println("Print array");
for (int element : arr) {
System.out.println(element);
}
for (int i = 0; i < arr.length; i++) {
int count = 0;
for (int j = 0; i < arr.length; j++) {
arr[i] = arr[j];
count++;
}
System.out.println(arr[i] + " " + count);
}
}
}

Looping a dice simulation

I seem to be having some trouble looping my dice simulator. I've placed the body of my code in a do-while loop but when the output prints to console it adds the previous output from the last simulation. I've tried initializing my "n" variable in a couple different places but have had no luck. Any help would be greatly appreciated.
My code follows...
import java.util.Random;
import java.util.Scanner;
public class Trash{
public static int getInt(Scanner scan) {
int input;
while ( !scan.hasNextInt() ) {
String garbage = scan.nextLine();
System.out.println("Please enter an integer. ");
}
input = scan.nextInt();
scan.nextLine();
return input;
}
public static void main(String [] args){
Scanner scan = new Scanner(System.in);
Random rand = new Random();
int dice1, dice2;
int [] frequency = new int [13];
int [] rolls = new int [13];
String again;
String stars = "";
double percentage;
do{
System.out.println("Enter the number of trials:");
int n = getInt(scan);
for (int i = 0; i < n; i++) {
dice1 = rand.nextInt(6)+1;
dice2 = rand.nextInt(6)+1;
frequency[dice1+dice2]++;
}
System.out.printf("Outcome\tFrequency\tPercentage\t\tHistogram\n");
for (int i = 2; i < frequency.length; i++) {
stars = "";
for ( int j = 0; j < frequency[i]; j++) {
stars = stars + "*";
}
percentage = (frequency[i] * 100.0) / n;
System.out.printf("%7d\t%9d\t%10.2f\t\t%1.50s\n",i,frequency[i], percentage, stars);
}
System.out.println("Run simulation again?");
String answer = scan.nextLine();
again = answer.toLowerCase();
} while (again.equals("yes"));
}
}
The reason is because you are not re-initializing the array when you re-started the simulation again.
do {
// re-initialize here
frequency = new int [13];
rolls = new int [13];
System.out.println("Enter the number of trials:");
// the rest of your code
} while (again.equals("yes"));

Creating a loop for Scanner for arrays

I want to create a loop that asks for the Scanner to input each number one after the another for a certain amount in an array (I'm thinking of 10). Any suggestions?
import java.util.Scanner;
public class AssignSeven
{
public static void main(String[] args)
{
int [] array1 = new int[10];
System.out.println("Enter 10 numbers");
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
for (int i = 0; i < 9; i++)
{
array1[i] = a;
}
}
}
change to
for (int i = 0; i < 9; i++)
{
int a = sc.nextInt();
array1[i] = a;
}
or even
for (int i = 0; i < 9; i++)
{
array1[i] = sc.nextInt();
}
It's simple, you can just assign the value of the scanner object's input to the indices of the array:
import java.util.Scanner;
public class AssignSeven
{
public static void main(String[] args)
{
int [] array1 = new int[10];
System.out.println("Enter 10 numbers");
Scanner sc = new Scanner(System.in);
// Where you had the original input
// int a = sc.nextInt();
for (int i = 0; i < 9; i++)
{
// Instead of array1[i] = a; you have
array1[i] = sc.nextInt();
}
}
}
Hope this helped!

How do I add user input to an Array in java?

public static void main(String[] args) {
Scanner user_input = new Scanner(System.in);
int i;
int n;
String a;
System.out.println("Enter the Class:");
a = user_input.next();
System.out.println("Enter the number of Students:");
n = user_input.nextInt();
for (i= 1; i <= n; i++) {
String g = a + i;
System.out.println(g);
}
}
This is my program. It gets user input for the Class and prints the Roll Number for the students.
For example: If the class is 10A and the number of students is 10, it prints a series like 10A1 , 10A2, 10A3 ... 10A10
How do I get the program to store these as elements in an array?
For example:
array[0] = 10A1;
array[1] = 10A2;
array[2] = 10A3;
etc.
Your code should look like this:
public static void main (String args[])
{
Scanner user_input = new Scanner(System.in);
int i;
int n;
String a;
System.out.println("Enter the Class:");
a = user_input.next();
System.out.println("Enter the number of Students:");
n = user_input.nextInt();
String []strings = new String[n]; // Creating an are of string with the given number
for(i= 0; i < n ;){
strings[i] = a + ++i; // Storing strings on to the array !
System.out.println(strings[i-1]);
}
}
You can just edit each index in your current for loop:
String[] arr;
for(i=0; i < n ; i++){
int j = i+1;
String g = a + j;
System.out.println(g);
arr[i] = g;
}
So all your printed g's will be part of the array arr.
First, declare a String array of the appropriate size.
Second, in your for loop, assign the strings you are currently printing, to positions in the array.
String[] things = new String[n];
for (i=1; i <= n; i++) {
String g = a + i;
System.out.println(g);
things[i-1] = g;
}
The strings are now in an array.
Following code is modified, for storing values in array.
public static void main(String[] args) {
// TODO code application logic here
Scanner user_input = new Scanner(System.in);
int i;
int n;
String a;
System.out.println("Enter the Class:");
a = user_input.next();
System.out.println("Enter the number of Students:");
n = user_input.nextInt();
String[] arr = new String[n]; // create string array of size n.
for(i= 1; i <= n ; i++){
String g = a + i;
System.out.println(g);
arr[i-1]=g; // assign your g veriable vale to array index
}
for(String s : arr){
System.out.println(s); // print your array
}
}

Categories

Resources