Re: problem 100 runtime error!!!
Posted: Tue Sep 07, 2010 10:50 pm
Do not use files. You are supposed to read from standard input (java's System.in) and write to standard output.
Code: Select all
#include <iostream>
using namespace std;
int main()
{
long long a,c,b,*arr,i,max=0,n,num;
while(cin >> a >> b){
arr = new long long[b-a];
if(a>b){
c = a;
a = b;
b = c;
}
max = 0;
for(i=0;i<b-a+1;i++){
arr[i] = a+i;
}
for(i=0;i<b-a+1;i++){
n = arr[i];
num=1;
while(n!=1){
num++;
if(n<=b && n>=a)
arr[n-a] = 1;
if(n%2 == 0){
n = n/2;
}
else
n = 3*n + 1;
}
if(max < num)
max=num;
}
cout << a << " " << b << " " << max<<endl;
}
return 0;
}
kindly let me knw why I am getting runtime error message
Hi,
This is an automated response from UVa Online Judge.
Your submission with number 8368446 for the problem 100 - The 3n + 1 problem has failed with verdict Runtime error.
This means that the execution of your program didn't finish properly. Remember to always terminate your code with the exit code 0.
Best regards,
The UVa Online Judge team
Code: Select all
< your code >
Code: Select all
import java.io.*;
import java.util.*;
public class Main
{
static int longestChain = 0;
public static void main (String[] args) throws FileNotFoundException
{
//long time = System.currentTimeMillis();
Scanner sc = new Scanner (new FileReader ("DATA1.txt"));
while (sc.hasNext ())
{
longestChain = 0; // Reset
int i = sc.nextInt ();
int j = sc.nextInt ();
int min = Math.min (i, j);
int max = Math.max (i, j);
for (int a = min ; a <= max ; a++)
{
sequence (a, 1);
}
System.out.println (i + " " + j + " " + longestChain);
}
sc.close ();
//long time2 = System.currentTimeMillis();
//System.out.println ("time: "+ (time2-time));
}
public static void sequence (int n, int counter)
{
if (n == 1)
{
longestChain = Math.max (counter, longestChain);
return;
}
else if (n % 2 == 0)
{
sequence (n / 2, counter + 1);
}
else
{
sequence (3 * n + 1, counter + 1);
}
}
}
Code: Select all
#include <iostream>
#include <fstream>
using namespace std;
//Functions
int createCycle(int startPt, int endPt);
int main()
{
ifstream in_stream;
int startNum, endNum;
in_stream.open("input.txt");
in_stream >> startNum;
while(!in_stream.eof())
{
in_stream >> endNum;
cout << startNum << " "
<< endNum << " "
<< createCycle(startNum, endNum) << endl;
in_stream >> startNum;
}
//system("PAUSE");
return 0;
}
int createCycle(int startPt, int endPt)
{
int count = 0;
int maxCount = 0;
while(startPt <= endPt)
{
int num = startPt;
while(num > 1)
{
if((num % 2) == 0)
num /= 2;
else
num = num*3+1;
count++;
}
startPt++;
if (count > maxCount)
maxCount = count + 1;
count = 0;
}
return maxCount;
}
Code: Select all
deleted after AC.
Code: Select all
#include <iostream>
using namespace std;
unsigned int arr[1000001];
unsigned int comp(unsigned int z)
{
unsigned int y;
if (arr[z]==0)
{
if (z%2==0)
y=z/2;
else
y=(3*z)+1;
arr[z] = comp(y)+1;
}
return arr[z];
}
int main()
{
unsigned int a, b, i, j, x, cnt, maxcnt;
arr[1]=1;
while( cin >> a >> b )
{
i=a;
j=b;
if (i>j)
swap( i, j);
maxcnt=0;
cnt =0;
for (x=i;x<=j;x++)
{
cnt = comp(x);
maxcnt=maxcnt<cnt?cnt:maxcnt;
}
cout<<a<<" "<<b<<" "<<maxcnt<<"\n";
}
return 0;
}
Code: Select all
#include <iostream>
using namespace std;
unsigned int arr[1000001];
unsigned int comp(unsigned int z)
{
unsigned int y;
if (arr[z]==0)
{
if (z%2==0)
y=z/2;
else
y=(3*z)+1;
arr[z] = comp(y)+1;
}
return arr[z];
}
int main()
{
unsigned int a, b, i, j, x, cnt, maxcnt;
arr[1]=1;
while( cin >> a >> b )
{
i=a;
j=b;
if (i>j)
swap( i, j);
maxcnt=0;
cnt =0;
for (x=i;x<=j;x++)
{
cnt = comp(x);
maxcnt=maxcnt<cnt?cnt:maxcnt;
}
cout<<a<<" "<<b<<" "<<maxcnt<<"\n";
}
return 0;
}
Code: Select all
unsigned int comp(unsigned int z)
{
if(z<=1000001)
{
if (arr[z]==0)
{
if (z%2==0)
arr[z] = comp(z/2)+1;
else
arr[z] = comp((3*z)+1)+1;
}
return arr[z];
}
else
{
if (z%2==0)
return (comp(z/2)+1);
else
return (comp((3*z)+1)+1);
}
}
Code: Select all
unsigned int comp(unsigned int z)
{
if(z<=1000001)
{
if (arr[z]==0)
{
if (z%2==0)
arr[z] = comp(z/2)+1;
else
arr[z] = comp((3*z)+1)+1;
}
return arr[z];
}
else
{
if (z%2==0)
return (comp(z/2)+1);
else
return (comp((3*z)+1)+1);
}
}
Code: Select all
#include <iostream>
#include <vector>
using namespace std;
int coll(vector<int>& v, long long int a) {
if(a >= v.size()) {
v.resize(a+2);
}
if(v[a] != 0) {
return v[a];
}
else {
if(a%2 == 0) {
v[a] = coll(v, a/2) + 1;
return v[a];
}
else {
v[a] = coll(v, 3*a + 1) + 1;
return v[a];
}
}
}
int main() {
vector<int> p(3);
p[0] = 0;
p[1] = 1;
p[2] = 2;
int n1, n2;
while(cin >> n1 >> n2) {
int current;
int max = 0;
if(n1 >= n2) {
swap(n1, n2);
}
for(int i = n1; i <= n2; ++i) {
current = coll(p, i);
if(current > max) {
max = current;
}
}
cout << n1 << " " << n2 << " " << max << endl;
}
}