I think, Then you should exchange the values.

Moderator: Board moderators
Code: Select all
# include <stdio.h>
int main()
{
int i=0, max;
int one, two;
while(scanf("%d %d",&one,&two)==2)
{
int j=one;
int k=j,max=0;
while(j<=two)
{
one=j;
i=0;
while(one!=1)
{
if(one%2==0)
{
one=one/2;
i++;
}
else if(one%2==1)
{
one=3*one+1;
i++;
}
}
i++;/*for one=1*/
if(max>i)
max=max;
else
max=i;
j++;
}
printf("%d %d %d\n",k,two,max);
}
return 0;
}
Code: Select all
#include <iostream>
int main() {
int n=0, length = 1, max = 0;
int i,j;
while (std::cin >> i >> j) {
for (int I=i;I<j;I++) {
n = I; // set 'n' to the current cycle
// perform the necessary operations on 'n' until its value is 1
while (n != 1) {
if (n%2 == 0)
n/=2;
else
n = 3*n+1;
length++;
}
// check if the current cycle length is greater than the max value
if (length > max)
max = length;
// reset the length variable so we can check the max value again next cycle
length = 1;
}
std::cout << i << " " << j << " " << max << "\n";
max = 0;
}
while (std::cin) std::cin.get();
return 0;
}
Code: Select all
import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
try
{
Scanner scan = new Scanner(System.in);
String input;
int i, j, k;
for(;scan.hasNext();)
{
input = scan.nextLine();
String[] temp = input.split(" ");
i = Integer.parseInt(temp[0]);
j = Integer.parseInt(temp[1]);
if(i>j)
{
k = i;
i = j;
j = k;
}
System.out.println(input + " " + solution(i, j));
}
}
catch(Exception e)
{
System.exit(0);
}
}
static int solution(int i, int j)
{
int n, a, count, result=0;
for(a=i; a<=j; a++)
{
n = a;
for(count=1;; count++)
{
if(n == 1)
break;
else if(n%2 == 0)
n = n/2;
else
n = 3*n + 1;
}
if(count > result)
result = count;
}
return result;
}
}
try changing yourRon wrote:To x140l31,line contains a '\n' also...Code: Select all
System.out.println(line + " " + c);
so you have to print i and j before it, when you take values in integers.
then print c .
your remaining code is ok...!!!
Code: Select all
............
i = Integer.parseInt(temp[0]);
j = Integer.parseInt(temp[1]);
int mini = Math.min(i, j);
int minj = Math.max(i, j);
System.out.println(i + " " + j + " " solution(mini, minj));
............
Code: Select all
#include<cstdio>
#include<iostream>
using namespace std;
int main()
{
unsigned long int n=0,max=0,x=0,s=0,e=0,i=0,s1=0,e1=0;
bool first=true;
while(cin>>s)
{
if(first==false) cout<<endl;
first=false;
cin>>e;
if(s>e)
{
s1=e;
e1=s;
}
else
{
s1=s;
e1=e;
}
max=0;
for(i=s1;i<=e1;i++)
{
n=i;
x=0;
while (n > 1)
{
if (n%2 != 0)
n=3*n + 1;
else
n= n / 2;
x++;
}
if(max<x+1) max=x+1;
}
cout<<s<<" "<<e<<" "<<max;
}
cout<<endl;
return 0;
}
Code: Select all
#include <iostream>
#include<iomanip>
using namespace std;
int main(int argc,char *argv[])
{
long int i=0,j=0;
while(scanf("%ld %ld",&i,&j)
!=EOF)
{
// cin>>i>>j;
// scanf("%ld",&i);
// scanf("%ld",&j);
cout<<i<<" "<<j<<" ";
if(i>j)
{long int x;
x=i;
i=j;
j=x;}
int *flag= new int[j-i+1];
long int a=0,n=1,count=1;
for (a=0;a<=j-i+1;a++)
flag[a]=1;
for(a=i;a<=j;a++)
{
long long x=a;
count=1;
if (flag[a-i])
{
while(x!=1)
{
if (x%2)
x=3*x+1;
else
x=x/2;
if(x>=i && x<=j)
flag[x-i]=0;
count++;
}
}
n=count>n?count:n;
}
cout<<n;
delete[] flag;
}
}
Code: Select all
int *flag= new int[j-i+1];
Code: Select all
int *flag= new int[1000000];