Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

如此水题,PE两次

Posted by KatrineYang at 2016-07-25 18:07:35 on Problem 1140
第一次是没看到输出换行,第二次是没注意如果长度刚好是50的倍数的时猴,最后不能输出换行,厕试数据竟然刚好有长度是50倍数的,醉了
#include <iostream>
#include <stdio.h>
using namespace std;

int GCD(int A, int B){
	if(A == 0) return B;
	if(B == 0) return A;
	if(A <= B) return GCD(A, B%A);
	return GCD(B, A%B);
}

int MAX(int a, int b){
	return a>b ? a : b;
}

int main() {
	int A, B;
	while(1){
		scanf("%d%d", &A, &B);
		if(A == 0 && B == 0) return A+B;
		int gcd = GCD(A, B);
		A /= gcd;
		B /= gcd;
		int two = 0, five = 0;
		int C = B;
		while(C%2 == 0){
			two++;
			C /= 2;
		}
		while(C%5 == 0){
			five++;
			C /= 5;
		}
		int K = MAX(two, five);
		if(C == 1){
			printf(".");
			for(int i = 0; i < K; i++){
				printf("%d", A*10/B);
				A = (A*10)%B;
			}
			printf("\nThis expansion terminates.\n");
			continue;
		}
		int D = 0;
		int prod = 1;
		while(1){
			prod = (prod*10)%C;
			D ++;
			if(prod == 1) break;
		}
		int cf = D+K;
		printf(".");
		int alr = 1;
		for(int i = 0; i < cf; i++){
			printf("%d", A*10/B);
			A = (A*10)%B;
			alr ++;
			if(alr%50 == 0 && i != cf-1){//一定要判断是否是最后一个
				printf("\n");
			}
		}
		printf("\nThe last %d digits repeat forever.\n", D);
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator