Please, could you help me?
I have runtime error. I guess problem is in reading input with while, when I don't use it I get wrong answer
What's wrong with my while?
I've got answer accepted Reading from input wasn't the problem, just had to remove stack and table and make it as simple as it gets. It's not so fast, but it's working. Right now I'm trying to optymize it.
import java.util.Scanner;
class Problem161{
public static long cycle_length(long num){
long count = 1;
while (num != 1){
if (num%2 == 0){
num = num / 2;
}
else{
num = num * 3 + 1;
}
count++;
}
return count;
}
public static void main(String args[]){
try{
Scanner scanner = new Scanner(System.in);
String str = scanner.nextLine();
while (str != null && !str.equals("")){
long i = Long.parseLong(str.split(" ")[0]);
long j = Long.parseLong(str.split(" ")[1]);
long a;
long b;
if (i < j){
a = i;
b = j;
}
else{
a = j;
b = i;
}
long max = 0;
for (long k = a; k <= b; k++){
long temp = cycle_length(k);
if (max < temp){
max = temp;
}
}
System.out.println("" + i + " " + j + " " + max);
str = scanner.nextLine();
}
}
catch (Exception e){
System.exit(0);
}
}
}
You have to download JDK. Search for 'JDK for windows' in google. (windows is just for example here, correct it for your os). Download and install it. It will help.
I'm a newbie here.. I have tried 2 questions, the 3n+1 problem and Minesweeper problem. I'm not sure whether my code is wrong or it is java problem, but I always get Runtime Error from the judge. I've looked through the code so many times but I still can't find the reason. Help me pls...!
import java.util.Scanner;
class Problem161{
public static long cycle_length(long num){
long count = 1;
while (num != 1){
if (num%2 == 0){
num = num / 2;
}
else{
num = num * 3 + 1;
}
count++;
}
return count;
}
public static void main(String args[]){
try{
Scanner scanner = new Scanner(System.in);
String str = scanner.nextLine();
while (str != null && !str.equals("")){
long i = Long.parseLong(str.split(" ")[0]);
long j = Long.parseLong(str.split(" ")[1]);
long a;
long b;
if (i < j){
a = i;
b = j;
}
else{
a = j;
b = i;
}
long max = 0;
for (long k = a; k <= b; k++){
long temp = cycle_length(k);
if (max < temp){
max = temp;
}
}
System.out.println("" + i + " " + j + " " + max);
str = scanner.nextLine();
}
}
catch (Exception e){
System.exit(0);
}
}
}
import java.util.Scanner;
class Problem162{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int m = scanner.nextInt();
int number = 1;
while (n != 0 && m != 0){
String line[] = new String[n];
char ch[][] = new char[n][m];
String result[][] = new String[n][m];
scanner = new Scanner(System.in);
for (int i = 0; i < n; i++){
line[i] = scanner.nextLine();
}
for (int i = 0; i < n; i++){
ch[i] = line[i].toCharArray();
}
for (int i = 0; i < n; i++){
for (int j = 0; j < m; j++){
if (ch[i][j] == '*'){
result[i][j] = "*";
}
else{
int count = 0;
for (int a = i-1; a <= i+1; a++){
if (a < 0 || a >= n){
continue;
}
for (int b = j-1; b <= j+1; b++){
if (b < 0 || b >= m){
continue;
}
if (a == i && b == j){
continue;
}
if (ch[a][b] == '*'){
count++;
}
else{
continue;
}
}
}
result[i][j] = String.valueOf(count);
}
}
}
//display
System.out.println("Field #" + number + ":");
for (int i = 0; i < n; i++){
for (int j = 0; j < m; j++){
System.out.print(result[i][j]);
}
System.out.println("");
}
System.out.println("");
number++;
n = scanner.nextInt();
m = scanner.nextInt();
}
}
}
Hi, could you help me? I'm trying to memorize previous results and speed up my solution, but I always get Runtime Error. I know the problem is with getting out from the while loop and I don't know why. Please help me
Thank you, but that's not the problem. When I'm applying values to tab[k] I'm still in for loop with values of k from i to j. I've checked, and program runs OK without these lines:
So I assume that problem lies there but I don't know why. When I compile it and check with my Visual C++ 2008 Express Edition I don't get any runtime errors at all
I also checked line like this instead of problematic lines:
Ha! There's difference in problem description. If you look at website, they say input is between 1 and 999999 but if you check out pdf file the input is between 1 and 9999. I've downloaded pdf file and worked on it and that's why I get errors (I've assumed that max input is 9999 and in fact it is 999999). Where should I call this bug to? I've already written to them using "Contact us" panel, is it enough?
Anyway, thank you mf, without your help I wouldn't notice it. I've submitted corrected code, it's not perfect because I can only declare size of table equal 200000 but it is still something, I've managed to get 0.02 sec time Oh yeah!