KS18. a/b
描述
输入描述
两个整数a和b,其中输出描述
一个字符串,该分数的小数表现形式示例1
输入:
10 1
输出:
10
说明:
10/1 = 10示例2
输入:
1 2
输出:
0.5
说明:
1/2 = 0.5示例3
输入:
1 3
输出:
0.(3)
说明:
1/3 = 0.333333...示例4
输入:
1 6
输出:
0.1(6)
说明:
1/6 = 0.16666666....示例5
输入:
1 7
输出:
0.(142857)
说明:
1 / 7 = 0.1428571428...C 解法, 执行用时: 2ms, 内存消耗: 376KB, 提交时间: 2020-05-20
#include<stdio.h> #include<stdlib.h> void fun(int a, int b) { int m ; int n ; int s = 0; m = a; n = b; int temp1 = 0; s = m / n; temp1 = s; int da[10000]; int temp = 0; for (temp = 0; temp<10000;) { m = m % n * 10; if (m>0) { s = m / n; da[temp] = m; temp++; } else { break; } } int num = -1; int num1 = 0; for (int i = 0; i<temp; i++) { for (int j = i + 1; j<temp; j++) { if (da[i] == da[j]) { num1 = i; num = j; break; } } if (num>-1) break; } if (num > 0) { printf("%d", temp1); printf("."); for (int i = 0; i < num1; i++) { m = da[i]; s = m / n; printf("%d", s); } printf("("); for (int i = num1; i < num; i++) { m = da[i]; s = m / n; printf("%d", s); } printf(")"); } else{ printf("%g", (float)(a) / b); } } int main() { int a; int b; while(scanf("%d %d",&a, &b) != EOF) fun(a,b); return 0; }
C 解法, 执行用时: 2ms, 内存消耗: 376KB, 提交时间: 2020-05-18
#include<stdio.h> #include<stdlib.h> void fun(int a, int b) { int m ; int n ; int s = 0; m = a; n = b; int temp1 = 0; s = m / n; temp1 = s; int da[10000]; int temp = 0; for (temp = 0; temp<10000;) { m = m % n * 10; if (m>0) { s = m / n; da[temp] = m; temp++; } else { break; } } int num = -1; int num1 = 0; for (int i = 0; i<temp; i++) { for (int j = i + 1; j<temp; j++) { if (da[i] == da[j]) { num1 = i; num = j; break; } } if (num>-1) break; } if (num > 0) { printf("%d", temp1); printf("."); for (int i = 0; i < num1; i++) { m = da[i]; s = m / n; printf("%d", s); } printf("("); for (int i = num1; i < num; i++) { m = da[i]; s = m / n; printf("%d", s); } printf(")"); } else{ printf("%g", (float)(a) / b); } } int main() { int a; int b; while(scanf("%d %d",&a, &b) != EOF) fun(a,b); return 0; }