#UVa:10193-All You Need Is Love

灆洢 2018-10-06 01:30:15

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

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;
}

在〈“#UVa:10193-All You Need Is Love”〉中有 1 則留言

  1. […] #UVa:10193-All You Need Is Love […]

發表迴響

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