## If you get WA in problem 100, read me before post!

Moderator: Board moderators

### Re: If you get WA in problem 100, read me before post!

i got WA can anyone help me out
[//#include<conio.h>
#include<iostream>
int table[10000000]={0};
int count(unsigned int);
main()
{
int s[1000],e[1000],max[1000]={1},j;
unsigned int i=0;
while(!std::cin.eof())
{
//int i=0;
std::cin>>s[i]>>e[i];
j=i;
i++;}
for(int k=0;k<j;k++)
{
for(int i=s[k];i<=e[k];i++)
{
count(i);
}
for(int i=s[k];i<=e[k];i++)
{
if(max[k]<table[i])
{
max[k]=table[i];
}
}
//string ss=(string)s;
std::cout<<s[k]<<" "<<e[k]<<" "<<max[k]<<"\n";
}
//getch();
}
int count(unsigned int i)
{if(i<=10000000)
{
if(table[i]==0)
{
if(i%2==0)
{
table[i]=count(i/2)+1;
}
else if(i==1)
{
return 1;
}
else
{
table[i]=count((3*i)+1)+1;
}
}
return table[i];
}else
{
if (i%2==0)
return(count(i/2)+1);
else
return(count((3*i)+1)+1);}
}
]
GOLDENMASTER
New poster

Posts: 1
Joined: Wed Sep 21, 2011 4:41 pm

### Re: If you get WA in problem 100, read me before post!

I've been tinkering with this for a while, and I keep getting WA with almost no idea why
[code][#include <stdio.h>

unsigned int i, j, num1, count, count1;
unsigned int backw = 0;
unsigned long num2;

unsigned long nextnum(int n){
if(n%2==0){
return n/2;
} else{
return 3*n+1;
}
}

int main(void) {
while(scanf("%d %d", &i, &j)==2){;
if(i>j){
num1 = i;
i = j;
j = num1;
backw = 1;
}
for(num1=i;num1<=j;num1++){
num2 = (long)num1;
count = 1;
while(num2!=1){
num2=nextnum(num2);
count++;
}
if (count>count1){
count1 = count;
}
}
if(!backw){
printf("%d %d %d\n",i,j,count1);
} else {
printf("%d %d %d\n",j,i,count1);
}
count1 = 0;
}
return 0;
}
/code]
zachaakelly
New poster

Posts: 1
Joined: Sat Oct 15, 2011 7:49 am

### Re: If you get WA in problem 100, read me before post!

Could you please tell me why I get WY with this code ?
Code: Select all
`#include <iostream>#include <algorithm>using namespace std;unsigned long int calc(unsigned long int val){   int counter=1;   while(val!=1){      if(val%2==0){         val=val/2;         counter++;      }      else{         val=val * 3 +1;         counter++;      }   }   return counter;}int main() {   unsigned long int start, startCount, end, endCount, maxCount;   while(cin>> start >> end){   startCount=start;   endCount = end;   unsigned long int maxNum=max(start, end);   unsigned long int minNum=min(start, end);   maxCount = calc(minNum);   while(minNum != maxNum){      maxCount = max(maxCount, calc(minNum));      ++minNum;   }   cout << startCount << " " << endCount << " " << maxCount << endl;   }   return 0;}`
javacom
New poster

Posts: 3
Joined: Sun Dec 25, 2011 6:05 pm

### Re: If you get WA in problem 100, read me before post!

minNum != maxNum

should be

minNum <= maxNum
helloneo
Guru

Posts: 516
Joined: Mon Jul 04, 2005 6:30 am
Location: Seoul, Korea

### Re: If you get WA in problem 100, read me before post!

helloneo wrote:minNum != maxNum

should be

minNum <= maxNum

Thank you, it works. AC finally
javacom
New poster

Posts: 3
Joined: Sun Dec 25, 2011 6:05 pm

### Re: If you get WA in problem 100, read me before post!

I submit this code in C.

I don't know why...

Please tell me why. I'm nervous because I can't wait.

The outputs in my PC are correct.
----
Hablo español mejor que inglés xD.

Code: Select all
`#include <stdio.h>long maxCicles(long num);int main() {        long num1;    long num2;    long lowNum, bigNum;    long maxNum = 0;    int i;    long currNum = 0;            while (scanf("%ld", &num1) != EOF)    {          scanf("%ld", &num2);                    if (num1 > num2)          {              bigNum = num1;              lowNum = num2;          }          else          {              bigNum = num2;              lowNum = num1;          }                    for (i = lowNum; i<=bigNum; i++)          {              currNum = maxCicles(i);              if (currNum > maxNum)              {                 maxNum = currNum;                          }          }                    printf("%ld ",num1);          printf("%ld ",num2);          printf("%ld", maxNum);          printf("\n");              }    return 0;}long maxCicles(long num){     long count = 1;     long n = num;          while (n != 1)     {           if (n % 2 != 0)           {              n = (3*n)+1;           }           else           {              n = n / 2;                }           count++;     }               return count;}`
rgracia
New poster

Posts: 1
Joined: Sun May 27, 2012 12:57 am

### Re: If you get WA in problem 100, read me before post!

Test your code on the sample input given in the problem statement and see if it matches the sample output. On my computer, on input:
201 210
201 210 125
201 210 89
brianfry713
Guru

Posts: 1742
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: If you get WA in problem 100, read me before post!

Here is my code . I couldn,t find any problem at this.. Someone help me please.....

/*100 - the 3n + 1 */
#include<stdio.h>
long int first, second;
main()
{
long int i, j, a;
int cycle, temp=0;
while(scanf("%ld%ld", &i, &j)==2)
{
first = i;
second = j;

cycle=0;
for(first; first<=second; first++)
{
a = first;
while(a!=1)
{
if(a%2==1)
{
a = 3 * a + 1;
++cycle;
continue;
}
else
{
a = a/2;
++cycle;
continue;
}
}
if(cycle>temp)
temp = cycle;
cycle = 0;
}
printf("%ld %ld %d\n",i, j, (temp+1));
temp=0;
}

return 0;

}
sulaimansust
New poster

Posts: 4
Joined: Fri Jul 27, 2012 1:53 pm

### Re: If you get WA in problem 100, read me before post!

What if i>j?
brianfry713
Guru

Posts: 1742
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: If you get WA in problem 100, read me before post!

Hi everyone!
I can't understand what's wrong in my code (it gives me WA). Thank you!

Code: Select all
`#include <iostream>using std::cin;using std::cout;void scambio(int &n, int &m){   int temp;   temp=m;   m=n;   n=temp;}int main(){   int a,b;   int max,length,num;   while (cin >> a >> b)      {      max=0;      if (a>b)         scambio(a,b);      for (int i=a;i<=b;i++)         {         num=i;         length=1;            while (num!=1)               {               if (num%2==0)                  num/=2;               else                  //(num*=3)++;                  num=num*3+1;               length++;               }                     if (max < length)            max = length;         }      cout << a << " " << b << " " << max << "\n";      }   return 0;}`

P.S. I tried to remove the procedure scambio (maybe the controller doesn't support it) but nothing changed!
LucaLuca
New poster

Posts: 3
Joined: Fri Aug 31, 2012 5:25 pm

### Re: If you get WA in problem 100, read me before post!

Input:
Code: Select all
`1 1010 1`
Correct output:
Code: Select all
`1 10 2010 1 20`
You can use swap(), but it won't fix your issue in this problem.
brianfry713
Guru

Posts: 1742
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: If you get WA in problem 100, read me before post!

brianfry713 wrote:Input:
Code: Select all
`1 1010 1`
Correct output:
Code: Select all
`1 10 2010 1 20`
You can use swap(), but it won't fix your issue in this problem.

I uses a procedure to put in the correct order the input...if a=10 and b=1 then with the procedure scambio a=1 and b=10 and the output becomes correct.

The output of the program has to be continuous or alternated with input?

My program does this for example:

INPUT:1 10
OUTPUT: 1 10 20
INPUT:10 1
OUTPUT: 1 10 20
LucaLuca
New poster

Posts: 3
Joined: Fri Aug 31, 2012 5:25 pm

### Re: If you get WA in problem 100, read me before post!

I changed like this but it doesn't work:

Code: Select all
`#include <iostream>using std::cin;using std::cout;using std::endl;void scambio(int &n, int &m){   int temp;   temp=m;   m=n;   n=temp;}int main(){   int a,b;   int max,length,num;   bool order=0;      while (cin >> a >> b)      {      max=0;      if (a>b)           {         scambio(a,b);         order=1;           }      for (int i=a;i<=b;i++)         {         num=i;         length=1;            while (num!=1)               {               if (num%2==0)                  num/=2;               else                  //(num*=3)++;                  num=num*3+1;               length++;               }                     if (max < length)            max = length;         }        if (order)           scambio(a,b);      cout << a << " " << b << " " << max << endl;      }   return 0;}`

I used again the procedure scambio and output becomes like you have said:

INPUT:
Code: Select all
`1 1010 1`

OUTPUT
Code: Select all
`1 10 2010 1 20`

Before it did:

INPUT:
Code: Select all
`1 1010 1`

OUTPUT
Code: Select all
`1 10 201 10 20`
LucaLuca
New poster

Posts: 3
Joined: Fri Aug 31, 2012 5:25 pm

### Re: If you get WA in problem 100, read me before post!

Try input:
Code: Select all
`10 11 10`
brianfry713
Guru

Posts: 1742
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: If you get WA in problem 100, read me before post!

Here is a tip: in C, use long int for the main variable types and test these inputs/outputs:

Code: Select all
`1 10100 200201 210900 10001000 900999999 9999901 10 20100 200 125201 210 89900 1000 1741000 900 174999999 999990 259`
renatov
New poster

Posts: 20
Joined: Fri Sep 21, 2012 6:33 am

PreviousNext