## 942 - Cyclic Numbers

Moderator: Board moderators

JCU
New poster
Posts: 2
Joined: Mon Nov 26, 2007 4:35 pm

### 942 - Cyclic Numbers

#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cmath>
using namespace std;

void ntoi(char str[],int n)
{
char temp = {0};
int i = 0;
if(n == 0)
{
str = '0';
str = 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;
char temp;
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 = {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

thanks a lot!!
I love coding ^^

rs17
New poster
Posts: 5
Joined: Tue Jul 03, 2007 5:49 am
Location: UNKNOWN
Contact:

### Re: 942 - Cyclic Numbers

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. The problem setter should add this information in the problem statement I think.
How did I fall in love with PERL?

rs17
New poster
Posts: 5
Joined: Tue Jul 03, 2007 5:49 am
Location: UNKNOWN
Contact:

### Re: 942 - Cyclic Numbers

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;
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.
How did I fall in love with PERL?

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

### Re: 942 - Cyclic Numbers

I have created a dataset for this problem and emailed the admins. All input values fit in a 32 bit signed integer.
Check input and AC output for thousands of problems on uDebug!