【模拟】第一题 分数化小数(fracdec.pas/c/cpp)

来源:互联网 发布:淘宝好做还是微店好做 编辑:程序博客网 时间:2024/06/02 20:00

【题目描述】

写一个程序,输入一个形如N/D的分数(N是分子,D是分母),输出它的小数形式。如果小数有循环节的话,把循环节放在一对括号中,例如:

1/3=33333333  写成0.(3)

41/333=0.123123123…  写成0.(123)

用xxx.0表示整数

典型的转化例子:

1/3=0.(3)

22/5=4.4

1/7=0.(142857)

2/2=1.0

3/8=0.375

45/56=0.803(571428)

【输入格式】

单独的一行包括被空格分开的N和D,1≤N,D≤100000。

【输出格式】

输出一行,小数的表示方法上面已经说得很明白了。

【样例输入】

45 56


【样例输出】

0.803(571428)



好吧这道题很简单其实就是个模拟…

想想做除法是怎么做的就是了

但是这道题我只有80分…

因为我只处理了有循环的…那种除得尽的根本没管…

代码如下

#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>#include <cmath>using namespace std;int A;int B;int Vst[1000020];int Left[1000020];int O[1000020];int tot;void init_file(){freopen("fracdec.in",  "r", stdin);freopen("fracdec.out", "w", stdout);}void read_data(){scanf("%d%d", &A, &B);}void work(){     printf("%d.", A / B);     if(A % B == 0)     {          printf("0");          exit(0);     }     while(A % B != 0)     {             tot ++;             A = (A % B) * 10;             O[tot] = A / B;             if (Vst[A] == 0) Left[A] = tot;             Vst[A]++;             if (Vst[A] > 1)             {                 for(int i = 1; i < Left[A]; i++)                         printf("%d", O[i]);                 printf("(");                             for(int i = Left[A]; i < tot; i++)                             {                                     printf("%d", O[i]);                             }                 printf(")");                 exit(0);             }     }     for(int i = 1; i <= tot; i++)     {             printf("%d", O[i]);     }}int main(){init_file();read_data();work();return 0;}


原创粉丝点击