poj2499

题目的意思:有一个节点信息(a,b),它的左子树为(a+b,b),右子树为(a,a+b)。现在给你一个节点,求从根节点(1,1)向左发展多少次,向右发展多少次能到达所给的节点

#include <iostream>
using namespace std; 

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
    
    
	int n,a,b,id = 1;
	cin>>n;
	while(n--){
    
    
		int l = 0;
		int r = 0;
		cin>>a>>b;
		while(a!=1||b!=1){
    
    
			if(a>b){
    
    
				l += a/b;
				if(a%b==0){
    
    
					a = 1;
					l--;
				}else{
    
    
					a %= b;
				}
			}else{
    
    
				r += b/a;
				if(b%a==0){
    
    
					b = 1;
					r--;
				}else{
    
    
					b %= a;
				}
			}
		}
		cout<<"Scenario #"<<id++<<":"<<endl;
		cout<<l<<" "<<r<<endl<<endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_45880043/article/details/111504168