【习题 3-8 UVA - 202】Repeating Decimals

【链接】 我是链接,点我呀:)
【题意】

在这里输入题意

【题解】

余数出现循环节。 就代表出现了循环小数。

【代码】

#include <bits/stdc++.h>
using namespace std;

int a,b;
int bo[4000];
vector <int> v;

int main(){
	#ifdef LOCAL_DEFINE
	    freopen("rush_in.txt", "r", stdin);
	    freopen("rush_out.txt","w",stdout);
	#endif
	int kase = 0;
	while (~scanf("%d%d",&a,&b)){
		kase++;
	    memset(bo,0,sizeof bo);
	    v.clear();
		
		printf("%d/%d = ",a,b);
		printf("%d.",a/b);

		int cnt = 0;

		if (a%b==0){
		 	cnt = 1;
		 	printf("(0)");
		}else{
    	 	int x = a%b;
    	 	v.push_back(x*10/b);
    	 	bo[x] = 1;
    	 	int now = 1;
    	 	x = x*10%b;
      		while (1){
      		 	if (bo[x]){
      		 		cnt = now-bo[x]+1;
      		 		int j = 0;
      		 		for (int i = 0;i < bo[x]-1;i++) {
      		 		    j++;
      		 		    if (j>50) break;
      		 			putchar(v[i]+'0');
      		 		}
      		 		putchar('(');
      		 		for (int i = bo[x]-1;i < (int) v.size();i++) {
      		 			j++;
      		 			if (j > 50){
      		 				printf("...");
      		 			 	break;
      		 			}
      		 			putchar(v[i]+'0');

      		 		}
      		 		putchar(')');
      		 	 	break;
      			}
      			now++;
      			bo[x] = now;
      			v.push_back(x*10/b);
      			x = x*10%b;
      		}

  		}
  		puts("");
  		printf("   %d = number of digits in repeating cycle",cnt);
  		puts("");
  		puts("");
	}

	return 0;
}
posted @ 2017-11-08 12:34  AWCXV  阅读(141)  评论(0编辑  收藏  举报