To sum up the exact conversion between decimals and scores.
First is the fractional conversion to decimal, this is relatively simple, first look question http://acm.hdu.edu.cn/showproblem.php? PID = 1, 2522
Input n to obtain the exact representation of 1/n. If there is a loop section, only the smallest one is output.
After a manual simulation, we will find that each operation is the remainder of the last Division X 10, and then continue the next calculation. When will a loop occur? It is obviously the repetition of the remainder.
You can create a hash or set by repeating the remainder judgment. Here N is not very large, and you can simply use an array.
In addition, pay attention to the process of Division during writing.
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn = 1e5 + 10;bool vis[maxn];int main() {int n, T; scanf("%d", &T);while (T--) {scanf("%d", &n);memset(vis, false, sizeof(vis));int num = abs(n), nowmod = 1, nowval;vis[1] = true; vis[0] = true;if (num == 1) {puts("1"); continue;}if (n < 0) putchar(‘-‘);printf("0.");while (1) {nowval = (nowmod * 10) / num;nowmod = (nowmod * 10) % num;printf("%d", nowval);if (vis[nowmod]) break;vis[nowmod] = true;}putchar(‘\n‘);}return 0;}
Another Look At The http://icpc.ahu.edu.cn/OJ/Problem.aspx? Id = 441
Enter two numbers a and B (0 <A <= B <= 1000), output A/B accurately, and output the minimum cycle length.
The processing method is the same as the one above. Just try to remove it. The processing of the length is to replace the original vis array with the position where the previous number appears.
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn = 1000 + 10;int pre_pos[maxn];int main() {int A, B;while (scanf("%d%d", &A, &B), A) {memset(pre_pos, -1, sizeof(pre_pos));A %= B;if (A == 0) {puts(".\nThis expansion terminates.");continue;}int nowmod = A, nowval, len, nowpos = 0;pre_pos[A] = 0;bool terminal = false;putchar(‘.‘);while (1) {nowpos++;nowval = (nowmod * 10) / B;nowmod = (nowmod * 10) % B;if (nowval == 0 && nowmod == 0) {terminal = true; break;}printf("%d", nowval);if (pre_pos[nowmod] != -1) {len = nowpos - pre_pos[nowmod];break;}pre_pos[nowmod] = nowpos;}putchar(‘\n‘);if (terminal) puts("This expansion terminates.");else printf("The last %d digits repeat forever.\n", len); }return 0;}
Then we will give you a decimal number and specify the cyclic section for you to calculate the score. Http://icpc.ahu.edu.cn/OJ/Problem.aspx? Id = 364
This type of problem is more troublesome than the above one. You need to push the formula.
Give you a decimal number n = 0.a( B). The circle section is enclosed in parentheses. Suppose the length of a is Lena, and the length of B is lenb.
N * 10 ^ Lena = A. (B) ----> N * 10 ^ Lena-A = 0. (B)
Set Y = 0. (B)
Y * 10 ^ lenb = B. (B)
Y * 10 ^ lenb-B = 0. (B) = y
Y = B/(10 ^ lenb-1) = N * 10 ^ Lena-
N = (B + A * (10 ^ lenb-1)/(10 ^ Lena * (10 ^ lenb-1 ))
Calculate the values of the numerator and denominator respectively, and then calculate the GCD. Just try again.
#include <cstdio>#include <cstring>#include <iostream> using namespace std; typedef long long LL; LL gcd(LL a,LL b) { return b == 0 ? a : gcd(b,a % b);} LL pow10(LL a) { LL ret = 1; while(a--) ret *= 10; return ret;} void display(int a,int b) { int n = 0,m = 0,ta = a,tb = b; while(ta) { n++; ta /= 10; } while(tb) { m++; tb /= 10; } LL denominator,numerator,div; denominator = pow10(n) * (pow10(m) - 1); numerator = a * (pow10(m) - 1) + b; div = gcd(denominator,numerator); cout << numerator / div << "/" << denominator / div << endl;} int main() { int T; cin >>T; while(T--) { int a,b; cin >> a >> b; display(a,b); } return 0;}
HDU 2522 & aoj 441 & aoj 364 converts decimals and scores