输入包含多组数据。每组数据包含三个正整数:被除数a和除数b(1≤a<b≤100),以及精度n(1≤n≤1000)。
对应每组数据,输出a/b的结果,小数后面保留n位(不到n位的补零)。
1 2 5<br/>2 3 3
0.50000<br/>0.666
#include<stdio.h> int main (){//the shorter,the better. int n,a,b; for(;~scanf("%d%d%d",&a,&b,&n);printf("\n")) for(printf("%d.",a/b);n>0;a=(a-a/b*b)*10,printf("%d",a/b),--n); }
import java.math.BigDecimal; import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner in=new Scanner(System.in); while(in.hasNext()){ BigDecimal bd1=new BigDecimal(in.next()); BigDecimal bd2=new BigDecimal(in.next()); int n=in.nextInt(); bd1=bd1.divide(bd2,n,BigDecimal.ROUND_FLOOR); System.out.println(bd1.toString()); } } }
由于精度n的取值范围为 1≤n≤1000
,所以老老实实算吧,别想偷懒。。。
模拟手动计算除法过程即可,补零
、上商
、求余
。
#include <iostream> using namespace std; int main() { int a = 0, b = 0, n = 0; //scanf返回值为正确输入数据的变量个数,当一个变量都没有成功获取数据时,此时返回-1 while (scanf("%d %d %d", &a, &b, &n) != - 1) { string resStr = ""; int tempNum = a / b; //获取整数位 resStr.append(to_string(tempNum) + "."); a %= b; //依次求出小数点后的各个位值 for (int i = 0; i < n; ++i) { if (a == 0) { //a == 0,说明能整除,后面补n - i个0 resStr.append(n - i, '0'); break; } //模拟小数除法,补零,上商,求余 a *= 10; tempNum = a / b; resStr.append(1, tempNum + '0'); a %= b; } printf("%s\n", resStr.c_str()); } return 0; } ———————————————— 版权声明:本文为CSDN博主「hestyle」的原创文章,遵循 CC 4.0 BY 版权协议,转载请附上原文出处链接及本声明。 原文链接:https://hestyle.blog.csdn.net/article/details/104700864
public class 圆周率 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); while(sc.hasNext()) { int n=sc.nextInt(); int m=sc.nextInt(); int len=sc.nextInt(); int ll=len; String str=""; boolean f=true; if(n<m) { str+="0."; n*=10; f=false; } while(true) { str+=n/m; n=n%m*10; if(f&&n<m) { f=false; str+="."; } if(str.contains(".")) { len--; } if(n==0||len==0) break; } if(len!=0) { int dot=str.indexOf("."); int end=str.length()-1; int l=ll-(end-dot+1)+1;//System.out.println(dot+","+end+","+len+","+l); while(l!=0) { str+="0"; l--; } } System.out.println(str); } } }
#include <bits/stdc++.h> using namespace std; int main() { double a,b; int n; while(cin >> a >> b >> n) { cout << setiosflags(ios::fixed) << setprecision(n) << a/b << endl; } return 0; } //为啥我这个输入1 3 788会有bug
思路 笔算的除法。 #include <iostream> #include <string> #include <sstream> using namespace std; string Int2String(int n) { stringstream ss; ss << n; string temp; ss >> temp; return temp; } string Remainder(int a_remainder, int b, int c) { string answer; int remainder = 0; for (int i = 0; i < c; i++) { a_remainder = a_remainder * 10; remainder = a_remainder % b; answer += a_remainder / b + '0'; a_remainder = remainder; } return answer; } int main() { //Remainder(2, 3, 1000); int a, b, c; string answer; while (cin >> a >> b >> c) { answer = Int2String(a / b); answer += "."; if (a%b == 0) { for (int i = 0; i < c; i++) { answer += "0"; } cout << answer << endl; answer = ""; continue; } int remainder = a % b; answer += Remainder(remainder, b, c); cout << answer << endl; answer = ""; } system("pause"); }
import java.util.*; import java.math.BigDecimal; public class Main { public static void main(String[] args) { Scanner read = new Scanner(System.in); while(read.hasNextInt()) { int a = read.nextInt(); int b = read.nextInt(); int n = read.nextInt(); BigDecimal aa = new BigDecimal(a); BigDecimal bb = new BigDecimal(b); System.out.println(aa.divide(bb, n, BigDecimal.ROUND_DOWN)); } } }
#include <stdio.h> int main() { int a, b, n, res, i; while(scanf("%d %d %d", &a, &b, &n)!=EOF) { printf("0."); for(i=0; i<n; i++) { res = a*10/b; if(i!=n-1) { printf("%d", res); } else { printf("%d\n", res); } a = a*10 % b; } } return 0; }
#include <cstdio> int main(){ int a,b,n; while(scanf("%d%d%d",&a,&b,&n)==3){ printf("0."); for(int i=0;i<n;i++){ printf("%d",a*10/b); a=a*10%b; } printf("\n"); } return 0; }
results=[] while True: try: nums=[int(s) for s in input().split(' ')] sts='0.' tmp=nums[0] for i in range(nums[2]): sts+='%d' % (tmp*10/nums[1]) tmp=tmp*10%nums[1] results.append(sts) except: break for res in results: print (res)
import java.util.Scanner; public class Main { public static void main(String[] args){ Scanner cin = new Scanner(System.in); int a, b, n; String answer; while(cin.hasNext()){ a = cin.nextInt(); b = cin.nextInt(); n = cin.nextInt(); answer = deliver (a,b,n); System.out.println(answer); } cin.close(); } private static String deliver(int a, int b, int n) { String answer = ""; answer += a/b + "."; while(n>0){ answer += a*10 /b; a = a*10 %b; n--; } return answer; } } //有一个弊端: //因为用的是String而不是StringBuffer,因此要处理的数据量过大时,效率会降低。 //此时最好用StringBuffer来处理
#include<iostream> #include<string> using namespace std; int main() { int a, b, length; while (cin >> a >> b >> length) { string res; if (a / b < 10) { char ch = a / b + '0'; res = res + ch; res = res + '.'; } else { int c = a / b; while (c) { char ch = c % 10 + '0'; res = ch + res; } res = res + '.'; } a = a % b; for (int i = 0; i < length; i++) { char ch = a * 10 / b + '0'; res += ch; a = a * 10 % b; } cout << res << endl; } return 0; }
import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in = new Scanner(System.in); while(in.hasNext()){ int a = in.nextInt(); int b = in.nextInt(); int n = in.nextInt(); System.out.print("0."); for(int i=0;i<n;i++){ a*=10; System.out.printf("%d",a/b); a%=b; } System.out.println(); } } }第一次提交提示超时,第二次又可以了。这
#include <stdlib.h> #include <stdio.h> #include <string.h> #define N /* 模拟竖式计算,遇到精度不够的就在后头补零继续除 */ //typedef struct { // //} ; int main(){ int n,a,b; while((scanf("%d%d%d",&a,&b,&n))!=EOF){ printf("%d",a/b); if(n)printf("."); while(n--){ a=(a-a/b*b)*10; printf("%d",a/b); } printf("\n"); }; return 0; }