#UVa:10193-All You Need Is Love

先將二進位字串轉成數字後,求兩數的最大公因數即可知道解答。

C++(0.000)

/*******************************************************/
/* UVa 10193 All You Need Is Love                      */
/* Author: Maplewing [at] knightzone.studio            */
/* Version: 2018/10/06                                 */
/*******************************************************/
#include <iostream>
#include <cstdio>
using namespace std;

int binaryStringToInt(string s){
  int num = 0;
  for(int i = 0 ; i < s.length() ; ++i){
    num <<= 1;
    num += s[i] - '0';
  }
  return num;
}

int gcd(int a, int b){
  if(a == 0 || b == 0) return a + b;
  while((a %= b) != 0 && (b %= a) != 0);
  return a + b;
}

int main(){
  int N;
  while(scanf("%d", &N) != EOF){
    for(int caseNumber = 1 ; caseNumber <= N ; ++caseNumber){
      string s1, s2;
      cin >> s1 >> s2;

      int num1 = binaryStringToInt(s1), num2 = binaryStringToInt(s2);
      printf("Pair #%d: ", caseNumber);
      if(gcd(num1, num2) != 1){
        printf("All you need is love!\n");
      }
      else{
        printf("Love is not all you need!\n");
      }
    }
  }
  return 0;
}
廣告

Comment

There is no comment on this post. Be the first one.

發表迴響

這個網站採用 Akismet 服務減少垃圾留言。進一步瞭解 Akismet 如何處理網站訪客的留言資料

%d 位部落客按了讚: