紫书 习题2-5 分数化小数

 1 #include<stdio.h> //基础版
 2 #define MAX 110
 3
 4 int main(void)
 5 {
 6     int a, b, c;
 7     scanf("%d %d %d",&a,&b,&c);
 8
 9     int integer = a/b;
10     int remainderTemp=a%b;
11     int arr[MAX];
12
13     for(int i = 0; i< c; i++){
14         int resultTemp = remainderTemp * 10;
15         arr[i] = resultTemp/b;
16         remainderTemp = resultTemp%b;
17     }
18     printf("%d.",integer);
19
20     for(int i = 0; i < c;i++){
21         printf("%d",arr[i]);
22     }
23
24     getchar();
25     getchar();
26     return 0;
27 }
 1 #include<stdio.h>//四舍五入版
 2 #include<math.h>
 3 int main()
 4 {
 5     int a,b,c;
 6     int cas=1;
 7     while(scanf("%d%d%d",&a,&b,&c))
 8     {
 9         if(a==0&&b==0&&c==0) break;
10         int x=floor((double)a/b);
11         int i,s;
12
13         printf("Case %d: %d.",cas++,x);
14         for(i=1;i<c;i++)
15         {
16             a=a*10;
17             s=floor((double)a/b);
18             printf("%d",s%10);    //注意输出的是s%10
19             a=a%b;
20         }
21         a=a*10;
22         s=floor((double)a/b+0.5);//注意把最后一位单独分离四舍五入
23         printf("%d",s);
24         printf("\n");
25     }
26     return 0;
27 }
时间: 2024-12-17 17:14:34

紫书 习题2-5 分数化小数的相关文章

分数化小数(decimal) 白书习题 2-5

1 /* 2 分数化小数(decimal) 白书习题 2-5 3 输入正整数 a , b , c , 输出 a/b 的小数形式,精确到小数点后 c 位 .a,b<=10^6 , c <= 100. 4 输入包含多组数据,结束标志为 a = b = c = 0 ; 5 */ 6 #include<stdio.h> 7 int main() 8 { 9 int a,b,c,y; //y用来存储 a/b 的余数 10 while(scanf("%d%d%d",&

【模拟】10216 - 分数化小数

[模拟]10216 - 分数化小数 Time Limit: 1000MS Memory Limit: 25600KB 30分骗分算法 # include<cmath> # include<stdio.h> # include<stack> # include<iostream> # include<algorithm> using namespace std; stack<int>S; const int maxn=10000; in

分数化小数

Fractions to Decimals 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 s

白书p35 习题2-4 分数化小数(decimal)

#include<stdio.h> int main() { int a,b,c; while(scanf("%d%d%d",&a,&b,&c)!=EOF) { if(a==0&&b==0&&c==0) break; int y=a%b; printf("%d.",a/b); for(int i=0;i<c-1;i++) { y*=10; printf("%d",y/b);

[LeetCode]58. Fraction to Recurring Decimal分数化小数

Given two integers representing the numerator and denominator of a fraction, return the fraction in string format. If the fractional part is repeating, enclose the repeating part in parentheses. For example, Given numerator = 1, denominator = 2, retu

分数化小数(C++)

输入整数a, b ,c, 输出a/b的小数形式,精确到小数点后c位,a, b ≤ 10^6, c ≤ 100.输入包含多组数据, 结束标记为a = b = c = 0. 答案如下 #include<iostream> #include<cmath> using namespace std; int main() { int a, b, c, m; int con[100]; cin >> a >> b >> c; while (a != 0 &a

每日一练第5天:分数化小数

输入正整数a,b,c,输出a/b的小数形式,精确到小数点后c位.a,b≤10 6 ,c≤100.输入包含多组数据, 结束标记为a=b=c=0. 样例输入: 1 6 4 0 0 0 样例输出: Case 1: 0.1667 这道题靠计算机本身来做除法是会出现精度问题的,所以要模拟数学上的除法: 1 #include <stdio.h> 2 3 int main() 4 { 5 int a, b, c, ct = 1; 6 while(3 == scanf("%d%d%d",

22. 分数化小数 decimal

题目: 输入正整数a, b,c,输出 a / b 的小数形式,精确到小数点后 c 位.a ,b <= 10^6, c <= 100.输入包含多组数据,结束标记为 a = b = c = 0. 样例输入: 1 6 4 0 0 0 样例输出: Case 1: 0.1667 思路: 按照步骤计算即可. 代码: #include <iostream>#include <iomanip>using namespace std; int main(){ int a = 0, b =

紫书 习题3-5

#include <iostream> #include <cstdio> #include <algorithm> using namespace std; const char inst[] = "ABLR"; const int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; int main(void) { int t = 0; char s[5][6]; char c; while ((s[0