每日一练第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", &a, &b, &c))
 7     {
 8         if(0 == a && 0 == b && 0 == c) break;
 9         int integer = a / b;                        // 获得整数部分
10         printf("Case %d: %d.", ct++, integer);
11         a %= b;         // 获得余数
12         int i = 1;
13         while(i++ < c)  // 进行c-1次模数学拟除法求出小数点后c-1位,因为要四舍五入,所以最后一位单独处理
14         {
15             a *= 10;
16             printf("%d", a / b);
17             a %= b;
18         }
19         a *= 10;
20         printf("%d\n", ((a % b) * 10 / b > 5) ? (a / b + 1) : (a / b)); // 判断最后一位的下一位是否大于5,进行四舍五入
21     }
22     return 0;
23 }
时间: 2024-08-10 21:15:45

每日一练第5天:分数化小数的相关文章

【模拟】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

分数化小数(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",&

[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

紫书 习题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 result

白书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);

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 =

CSS3每日一练之内容处理-嵌套编号

出处:http://www.w3cfuns.com/thread-5592229-1-17.html 1.大标题一   1.子标题   2.子标题   3.子标题2.大标题二   1.子标题   2.子标题3.大标题三   1.子标题   2.子标题   3.子标题 这种问题,你可能会觉得直接定义两个计数器,分别编号不就行了,于是就写出了如下代码: <!DOCTYPE HTML> <html> <head> <meta charset="gb2312&q