답안 #6813

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
6813 2014-07-08T02:22:37 Z gs12117 분수 (KPI13_fractions) C++
0 / 1
0 ms 1104 KB
#include<stdio.h>
long long int n;
long long int a,b,c,d;
long long int x[1000];
long long int y[1000];
long long int xl,yl;
int main(){
	long long int i,j,t;
	scanf("%lld",&n);
	for(i=0;i<n;i++){
		scanf("%lld%lld%lld%lld",&a,&b,&c,&d);
		for(xl=0;;xl++){
			if(b==0)break;
			x[xl]=a/b;
			t=a%b;
			a=b;
			b=t;
		}
		for(yl=0;;yl++){
			if(d==0)break;
			y[yl]=c/d;
			t=c%d;
			c=d;
			d=t;
		}
		for(j=0;;j++){
			if(x[j]!=y[j]||j==xl||j==yl)break;
		}
		if(j==xl){
			x[j]=y[j]+1;
		}
		else if(j==yl){
			x[j]=x[j]+1;
		}
		else if(x[j]<y[j]){
			if(j==yl-1&&x[j]+1==y[j]){
				if(j==xl-2&&x[j+1]==2){
					j++;
					x[j]=1;
					j++;
					x[j]=2;
				}
				else{
				if(xl!=j+1&&x[j+1]==1){
					x[j]++;
				}
				j++;
				x[j]=2;
				}
			}
			else{
				x[j]++;
			}
		}
		else{
			if(j==xl-1&&y[j]+1==x[j]){
				if(j==yl-2&&y[j+1]==2){
					x[j]=y[j];
					j++;
					x[j]=1;
					j++;
					x[j]=2;
				}
				else{
					x[j]=y[j];
					if(yl!=j+1&&y[j+1]==1){
						x[j]++;
					}
					j++;
					x[j]=2;
				}
			}
			else{
				x[j]=y[j]+1;
			}
		}
		a=1;
		b=0;
		while(j>=0){
			t=a;
			a=b;
			b=t;
			a+=x[j]*b;
			j--;
		}
		printf("%lld %lld\n",a,b);
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 1104 KB Output is correct
2 Correct 0 ms 1104 KB Output is correct
3 Correct 0 ms 1104 KB Output is correct
4 Incorrect 0 ms 1104 KB Output isn't correct
5 Halted 0 ms 0 KB -