习题 3-8 循环小数(Repeating Decimals, ACM/ICPC World Finals 1990, UVa202)
题目描述:
输入整数a和b (0<=a<=3000, 1<=b<=3000),输出a/b的循环小数表示以及循环节长度。例如a=5, b=43,小数表示为0.(116279069767441860465),循环节长度为21
第一想法就是字符串匹配,虽然走了弯路,好在最后AC了
#include<stdio.h>
#include<ctype.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
using namespace std;
const int MAX_N = 10000;
int a, b;
int resInt; // 存储结果的整数部分
char res[MAX_N]; // 存储结果的小数部分
char cycle[2*MAX_N]; // 存储循环节
int main(){
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
while(scanf("%d%d", &a, &b) == 2) {
// 当成功读取到a和b
memset(res, '0', sizeof(res)); // 重置结果和循环节
memset(cycle, '\0', sizeof(cycle));
// 计算整数部分
int numA = a, numB = b;
resInt = a/b