版权声明:如需转载,记得标识出处 https://blog.csdn.net/godleaf/article/details/82949106
思路:
假设 A = 7, B = 3 ;
第一行的蓝色矩形是 7 ,第二行以及后面的蓝色矩形都是 3,黄色部分是减去得到的值;可以发现的规律是,只要黄色比B长,相减就能得到不一样的值,除非相等,假设两个值相等,值为 x,那么得到的是0 x x 0 这样的循环数;
如果黄色的部分比B短又如何处理? 那么处理的方法还是和上面的一样,只不过是B不断的减去黄色的部分;像这样的辗转相减可以通过辗转相除法计算出相减得次数;
注意要特判A和B中一个为0,一个不为0输出2的情况;
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<queue>
#include<map>
#include<stack>
#include<sstream>
#include<vector>
#include<string>
#include<set>
using namespace std;
//using namespace __gnu_pbds;
#define IOS ios::sync_with_stdio(false); cin.tie(0);
#define REP(i,n) for(int i=0;i<n;++i)
int read(){
int r=0,f=1;char p=getchar();
while(p>'9'||p<'0'){if(p=='-')f=-1;p=getchar();}
while(p>='0'&&p<='9'){r=r*10+p-48;p=getchar();}return r*f;
}
//typedef tree<pair<long long,int>,null_type,less< pair<long long,int> >,rb_tree_tag,tree_order_statistics_node_update> rbtree;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<long long,long long> pll;
const int Maxn = 2e5+10;
const long long LINF = 1e18;
const int INF = 0x3f3f3f3f;
const int Mod = 10001;
const double PI = acos(-1.0);
ll solve (ll x, ll y) {
ll ans = 0;
while (y) {
ans+=x/y;
ll r = x%y;
x = y; y = r;
}
return ans;
}
int main (void)
{
int T;
ll a, b;
cin >> T;
for (int cas = 1; cas <= T; ++cas) {
cin >> a >> b;
cout << "Case #" << cas << ": ";
if (a != b && (!a || !b)) cout << "2" << endl;
else cout << solve (a, b)+1 << endl;
}
return 0;
}