USACO-Fractions to Decimals
2016-06-21 11:29
218 查看
Write a program that will accept a fraction of the form N/D, where N is the numerator and D is the denominator and print the decimal representation. If the decimal representation has a repeating sequence of digits, indicate the sequence by enclosing it in brackets. For example, 1/3 = .33333333…is denoted as 0.(3), and 41/333 = 0.123123123…is denoted as 0.(123). Use xxx.0 to denote an integer. Typical conversions are:
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)
PROGRAM NAME: fracdec
INPUT FORMAT
A single line with two space separated integers, N and D, 1 <= N,D <= 100000.
SAMPLE INPUT (file fracdec.in)
45 56
OUTPUT FORMAT
The decimal expansion, as detailed above. If the expansion exceeds 76 characters in length, print it on multiple lines with 76 characters per line.
SAMPLE OUTPUT (file fracdec.out)
0.803(571428)
题意:给一个分数,输出小数,循环小数部分要有括号。
模拟
代码:
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)
PROGRAM NAME: fracdec
INPUT FORMAT
A single line with two space separated integers, N and D, 1 <= N,D <= 100000.
SAMPLE INPUT (file fracdec.in)
45 56
OUTPUT FORMAT
The decimal expansion, as detailed above. If the expansion exceeds 76 characters in length, print it on multiple lines with 76 characters per line.
SAMPLE OUTPUT (file fracdec.out)
0.803(571428)
题意:给一个分数,输出小数,循环小数部分要有括号。
模拟
代码:
/* ID:iam666 PROG:fracdec LANG:C++ */ #include <cstdio> #include <algorithm> #include <cstring> #include <iostream> using namespace std; //bool Hash[10002]; //long a[10002],b[10005]; int main (void) { //freopen("fracdec.in","r",stdin); //freopen("fracdec.out","w",stdout); long n,m; cin>>n>>m; bool Hash[100002]; memset(Hash,false,sizeof(Hash)); Hash[0]=true; long d=n%m; if(d==0) { printf("%ld.0\n",n/m); return 0; } long a[100002],b[100005]; long l=0,p=0; memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); a[++p]=n/m; long ii=0; long yy=a[1]; if(yy==0) ii++; while(yy>0) { ii++; yy/=10; } ii++;//位数 printf("%ld.",a[1]); b[p]=d; while(!Hash[d]) { Hash[d]=true; d=d*10; a[++p]=d/m; d=d%m; b[p]=d; } long i=1; long bo=false; for(long j=1;j<=p;j++) { if(b[j]==d) { l=j+1; //l=j; bo=true; break; } } for(i=2;i<=p;i++) { if(i==l&&bo) { printf("("); i--; ii++; bo=false; } else { printf("%ld",a[i]); ii++; if(ii%76==0) printf("\n");} } if(d) printf(")\n"); else if(ii%76) printf("\n"); return 0; }
相关文章推荐
- USACO 3.2.2:Stringsobits
- USACO 3.1 Shaping Regions
- 【解题报告】【USACO】酸奶工厂
- usaco-Calf Flac
- USACO1.1 Friday the Thirteenth 比较分析
- USACO1.1 Greedy Gift Givers 比较分析
- USACO1.1 PROB Your Ride Is Here 比较分析
- USACO入门以及提交格式
- usaco/1.1 Your Ride Is Here
- usaco/1.1 Greedy Gift Givers
- usaco/1.1 Friday the Thirteenth
- usaco/ 1.1 Broken Necklace
- USACO/fact4 3.2.1
- USACO/spin 3.2.3
- USACO/rockers 3.4.4 动态规划
- USACO 2.2 Party Lamps
- USACO 2.2 Runaround Numbers
- USACO 2.2 Subset Numbers
- USACO 2.2 Preface Numbering
- USACO 2.1 Ordered Fractions