Page 1 of 1

942 - Cyclic Numbers

Posted: Mon Nov 26, 2007 4:41 pm
by JCU
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cmath>
using namespace std;

void ntoi(char str[],int n)
{
char temp[100000] = {0};
int i = 0;
if(n == 0)
{
str[0] = '0';
str[1] = 0;
return;
}
while(n != 0)
{
temp = (n%10)+48;
i++;
n/=10;
}
for(int j = 0;j < i;j++)
str[i-j-1] = temp[j];
str = 0;
}

int main()
{
int datas;
int n1;
int n2;
int i;
int j;
int f;
int len;
char b[10000];
char temp[10000];
int q;
int run;
int counter;
cin >> datas;

while(datas--)
{
run = 1;
cin >> n1;
cin >> n2;

if(n1%n2 == 0)
{
cout << n1/n2 << ".0\n";
continue;
}

q = n1/n2;
ntoi(temp,q);
strcpy(b,temp);
len = strlen(b);
b[len+1] = 0;
b[len] = '.';
f = 0;
counter = 1;
n1 %= n2;

{
int a[100000] = {0};
while(1)
{
if(n1 == 0)
{
run = 1;
break;
}

if(a[n1] != 0)
{
counter = a[n1];
run = 2;
break;
}
else
a[n1] = counter++;

n1 *= 10;
q = n1/n2;
ntoi(temp,q);
strcat(b,temp);
n1%=n2;
}
}

if(run == 1)
{
cout << b << endl;
}
else
{
i = 0;
while(b != '.')
cout << b[i++];
cout << b[i++];

while(counter != 1)
{
cout << b[i++];
counter--;
}
cout << "(";
while(b != 0)
cout << b[i++];
cout << ")\n";
}


}

return 0;
}

my code is above
the online system is show me runtime error
please give me some advice

thanks a lot!!

Re: 942 - Cyclic Numbers

Posted: Thu May 15, 2008 2:56 pm
by rs17
I can't tell you how angry I am when I realize that the numerator and denominator in the problem can be very large(exceed long long in C/C++) after lots of TLEs & REs. :evil:

The problem setter should add this information in the problem statement I think.

Re: 942 - Cyclic Numbers

Posted: Thu May 15, 2008 3:38 pm
by rs17
The problem setter is very irresponsible because I've got Accepted after I submitted this code:

Code: Select all

#include<stdio.h>
#include<assert.h>
char s[10000];
int main()
{
    int i;
    while(gets(s))
    for(i=0; s[i]; i++)
    assert(s[i]>='0' && s[i]<='9' || s[i]==' ');
    return 0;
}
Maybe the site admin. should make this problem unsubmitable, until the correct test data has been updated successfully.

Re: 942 - Cyclic Numbers

Posted: Wed Apr 09, 2014 10:10 pm
by brianfry713
I have created a dataset for this problem and emailed the admins. All input values fit in a 32 bit signed integer.