Re: If you get WA in problem 100, read me before post!
Posted: Fri Jun 27, 2014 6:23 am
but problem says...
All integers will be less than 10000 and greater than 0.
All integers will be less than 10000 and greater than 0.
Code: Select all
import java.util.*;
import java.math.*;
import java.io.*;
public class Main {
// cache for already computed cycle lengths
public static int[] cache = new int[1000000];
// a function that returns the
// next number in the sequence
public static long next(long n) {
if (n % 2 == 0)
return n / 2; // if n is even
else
return 3 * n + 1; // if n is odd
}
public static int cycleLength(long n) {
// our base case
// 1 has a cycle length of 1
if (n == 1)
return 1;
// check if we've already cached the
// cycle length of the current number
if (n < 1000000 && cache[(int)n] != 0)
return cache[(int)n];
// the cycle length of the current number is 1 greater
// than the cycle length of the next number
int length = 1 + cycleLength(next(n));
// we cache the result if the
// current number is not too big
if (n < 1000000)
cache[(int)n] = length;
return length;
}
public static void main(String[] args) throws Exception {
Scanner in = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out, true);
// while there is some input to read
while (in.hasNextInt()) {
int i = in.nextInt(),
j = in.nextInt(),
from = Math.min(i, j),
to = Math.max(i, j),
max = 0;
// loop through all the numbers
// and find the biggest cycle
for (int n = from; n <= to; n++) {
max = Math.max(max, cycleLength(n));
}
out.printf("%d %d %d\n", i, j, max);
}
}
}
Code: Select all
#include <stdio.h>
#define MAX 1000000
static int cache[MAX] = {0};
long long next(long long n){
if(n & 1)
return n + (n << 1)+1;
else
return (n >> 1);
}
int cl(long long n){
int l;
if(n == 1)
return 1;
if(n < MAX && cache[(int)n] != 0)
return cache[(int)n];
l = cl(next(n))+1;
if(n < MAX)
cache[(int)n] = l;
return l;
}
int main(){
int start,end,i,r,max,tmp,s,e;
printf("Input start&end:\n");
while(scanf("%d%d",&start,&end)){
s = start;
e = end;
if(start > end){
tmp = start;
start = end;
end = tmp;
}
max = 0; /*critical*/
for(i = start; i <= end; ++i){
r = cl(i);
if(max < r)
max = r;
}
printf("%d %d %d\n",s,e,max);
}
}
Code: Select all
while(scanf("%d%d",&start,&end)){
Code: Select all
while(scanf("%d%d",&start,&end)==2){
Code: Select all
//Runtime Error
import java.util.Scanner;
public class Main {
private static int counts=0;
private static Scanner sc;
private static void calcProb(long i){
counts++;
if(i == 1){return;}
if(i%2 == 1){calcProb(3*i + 1);}
else{calcProb(i/2);}
}
public static void main(String[] args){
sc = new Scanner(System.in);
while(true){
long i = sc.nextLong();
long j = sc.nextLong();
long max=0;
long z = i, x = j;
if(i > j){
long d = i; i = j; j = d;
}
for(long k = i;k<=j;k++){
calcProb(k);
if(max < counts) max = counts;
counts = 0;
}
System.out.println(z + " " + x + " " + max);
if(!sc.hasNextLine()){return;}
}
}
}
Code: Select all
while(sc.hasNext()){
..
..
System.out.println(z + " " + x + " " + max);
}
Code: Select all
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int a,b;
int max=0,count=0;
cin>>a>>b;
if(a>b) swap(b,a);
for(int i=a;i<=b;i++)
{
while (i!=1)
{
if(i%2==0)
i=i/2;
else
i=3*i+1;
count++;
}
}
if(max<count)
{
max=count;
}
cout <<a <<" "<<b<<" "<< max;
}
brianfry713 wrote:Input:AC output:Code: Select all
9 1
Code: Select all
9 1 20
Code: Select all
#include <vector>
#include <stdio.h>
#include <iostream>
#include <algorithm>
using namespace std;
int find_cycle_length(int b)
{
// Finds the max cycle of b
int max_cycle = 1;
while (b != 1)
{
if (b % 2 != 0)
{
b = (3 * b) + 1;
++max_cycle;
}
else if (b % 2 == 0)
{
b /= 2;
++max_cycle;
}
}
return max_cycle;
}
int find_max_cycle(vector <int>& b)
{
vector <int> temp;
for (int i = 0; i < b.size(); ++i)
{
int buffer = b[i];
temp.push_back(find_cycle_length(buffer));
}
int max_cycle = *max_element(temp.begin(), temp.end());
return max_cycle;
}
int main()
{
int i = 0; // First number
int j = 0; // Second number
int size = 0; // Determines the size of the vector buffer
int counter = 0; // Used to fill buffer
cin >> i >> j;
if (j > i) {
size = (j - i) + 1;
counter = i;
}
else if (i > j) {
size = (i - j) + 1;
counter = j;
}
else if (i == j)
{
size = 1;
counter = i;
}
vector<int> buffer(size); // Used to store all numbers i to j
for (int x = 0; x < buffer.size(); ++x) // fill buffer
{
buffer[x] = counter;
++counter;
}
cout << i << " " << j << " " << find_max_cycle(buffer) << endl;
}