Code: Select all
15 3
1 2971
Code: Select all
15/3 = 5.(0)
1 = number of digits in repeating cycle
1/2971 = 0.(00033658700774150117805452709525412319084483338943...)
2970 = number of digits in repeating cycle
Moderator: Board moderators
Code: Select all
15 3
1 2971
Code: Select all
15/3 = 5.(0)
1 = number of digits in repeating cycle
1/2971 = 0.(00033658700774150117805452709525412319084483338943...)
2970 = number of digits in repeating cycle
Code: Select all
15 3
1 2971
Code: Select all
15/3 = 5.(0)
1 = number of digits in repeating cycle
1/2971 = 0.(00033658700774150117805452709525412319084483338943...)
2970 = number of digits in repeating cycle
Code: Select all
#include <iostream>
#include <string>
#include <map>
using namespace std;
string ans = "", ans2, ans3;
map<int, int> M, pos;
int vagfol, mod, p, len, l;
bool flag = false;
void find_cycle(int a, int b){
if(a < b){
a *= 10;
}
while(a < b){
ans += '0';
a *= 10;
}
if(M[a] == 1){
p = pos[a];
return;
}
M[a] = 1;
pos[a] = ans.size();
mod = a % b;
int d = a / b;
string number = "";
while(d != 0){
int r = d % 10;
char c = r + 48;
number += c;
d /= 10;
}
for(int i = number.size() - 1; i >= 0; i--){
ans += number[i];
}
if(M[mod] == 1){
p = pos[mod];
return;
}
if(mod == 0){
flag = true;
p = 0;
return;
}
M[mod] = 1;
pos[mod] = ans.size();
find_cycle(mod, b);
}
void add_dot(int a, int b){
if(a < b){
vagfol = 0;
}
else{
vagfol = a / b;
a = a % b;
}
if(a == 0){
flag = true;
return ;
}
if(a < b){
M[a] = 1;
pos[a] = ans.size();
a *= 10;
}
while(a < b){
ans += '0';
a *= 10;
}
find_cycle(a, b);
}
int main()
{
int a, b, s, e;
while(cin >> a >> b){
s = a;
e = b;
vagfol = 0;
M.clear();
pos.clear();
ans = "";
ans2 = "";
ans3 = "";
flag = false;
add_dot(a, b);
if(flag){
l = 1;
ans2 = ans;
ans3 = "0";
}
else{
len = ans.size(), l = ans.size();
for(int i = 0; i < p; i++)
ans2 += ans[i];
if(len > 50){
ans3 = ans.substr(p, 50);
ans3 += "...";
}
else
ans3 = ans.substr(p, ans.size() - p);
}
cout << s << "/" << e << " = " << vagfol << ".";
if(ans2.size() != 0)
cout << ans2 ;
cout << "(" <<ans3 << ")" << endl;
cout << " " << l - p << " = number of digits in repeating cycle\n\n";
}
return 0;
}
I've got WA for 5 times, then after I added a newline at the end, I got AC.little joey wrote:Eric: Your WA is in fact a runtime error caused by extra empty lines at the end of the input. The statement Readln (Nu, De); tries to read the values, but there are no more left to read.
I added a line to your code and got accepted:
[pascal]Begin
While not eof Do
Begin
if eoln then begin readln; continue; end;
Readln (Nu, De);
...[/pascal]
This line just eats the empty line and loops back to the while-statement that checks for an end-of-file.
It is very irritating that the online judge doesn't report runtime errors for Pascal programs, but gives Wrong Answer instead. I have reported this long time ago, but nobody seems to care. So always be aware that your WA is in fact a runtime error.