Try   HackMD

All You Need Is Love

題目連結 UVa 10193

中文簡述

給你兩個二進位字串,找到他們的公倍最大公因數

solution:

#include<bits/stdc++.h>
using namespace std;
int dec(string bin)
{
	int num=0,arr[bin.length()],i,j;
	if(bin[0]=='0')
	{
		return -1;
	}
	else
	{
		for(i=0;i<bin.length();i++)
		{
			arr[i]=(int)bin[i]-'0';
		}
		for(i=bin.length()-1,j=0;i>=0;i--,j++)
		{
			num+=arr[j]*pow(2,i);
		}
		return num;
	}
}
int gcd(int a,int b)
{
	if(a==-1 or b==-1)
	{
		return 0;
	}
	else
	{
		while ((a %= b) && (b %= a));
		{
			return a + b;
		}
	}
}
int main()
{
	int n,c;
	string a,b;
	cin>>n;

	for(int i=0;i<n;i++)
	{
		cin>>a>>b;

		cout<<"Pair #"<<i+1;
		int c=gcd(dec(a),dec(b));
		if(c!=0 and c!=1)
		{
			cout<<": All you need is love!"<<endl;
		}
		else
		{
			cout<<": Love is not all you need!"<<endl;
		
		}
	}
}
tags: UVA

回目錄 學習筆記