942 - Cyclic Numbers

All about problems in Volume 9. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

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

942 - Cyclic Numbers

Post by JCU » Mon Nov 26, 2007 4:41 pm

#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!!
I love coding ^^

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

Re: 942 - Cyclic Numbers

Post by rs17 » Thu May 15, 2008 2:56 pm

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.
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

Post by rs17 » Thu May 15, 2008 3:38 pm

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.
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

Post by brianfry713 » Wed Apr 09, 2014 10:10 pm

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!

Post Reply

Return to “Volume 9 (900-999)”