close

Give you two binary string.

If the greatest common devisor is greater than 1 then print "Pair #1: All you need is love!" (1 stands for index of cases).

If the greatest common devisor is 1 then print "Pair #1: Love is not all you need! " (1 stands for index of cases).

 

-----

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define MAX 31 /* Reserve 1 extra size will be safer. */
#define true 1
#define false 0

int main(int argc, char *argv[]) 
{
    int tes_cas, pair_num=1, num1, num2, flag, first=1; /* Test cases */
    char str1[MAX], str2[MAX];
    int i, j, k;
    
    scanf("%d", &tes_cas);
    while(tes_cas--)
    {
        /* Initialize */
        flag = false;
        
        if(first++ == 1)
            getchar(); /* Because of scanf there have a '\n' remains. */ /* Only the first time */
        /* Input */
        gets(str1);
        gets(str2);
        
        /* char to num */
        for(i=0; str1[i]; i++)
            str1[i] -= '0';
        i--;
        for(j=0; str2[j]; j++)
            str2[j] -= '0';
        j--; /* Back to the character which is in front of the '\0'. */
        
        /* Binary to decimal */
        for(k=0, num1=0; i>=0; i--, k++)
            num1 += str1[i] * (int)pow(2, k); /* From right to left. Every time the square plus 1. */
        for(k=0, num2=0; j>=0; j--, k++)
            num2 += str2[j] * (int)pow(2, k); /* From right to left. Every time the square plus 1. */
        
        /* 輾轉相除法 to find the G.C.D. */
        while(num1 > 1 && num2 > 1) /* 1 should be excluded. */
            if(num1 > num2)
                num1 -= num2;
            else
                num2 -= num1;
        
        if(!num1 || !num2) /* If any number == 0 then num1 and num2 have the G.C.D(>1) */
            flag = true;
            
        if(flag)
            printf("Pair #%d: All you need is love!\n", pair_num++);
        else
            printf("Pair #%d: Love is not all you need!\n", pair_num++);
    }
    return 0;
}
 

 

--------

package uva10193;

import java.util.Scanner;

/**
 *
 * @author awesq
 */
public class UVa10193 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        try (Scanner sc = new Scanner(System.in)) {
            int cases = sc.nextInt();
            sc.nextLine(); // For the remaining carriage return or line feed.
            for (int i = 1; i <= cases; i++) {
                int a = Integer.parseInt(sc.nextLine(), 2);
                int b = Integer.parseInt(sc.nextLine(), 2);
                if (getGCD(a, b) != 1) {
                    System.out.println("Pair #" + i + ": All you need is love!");
                } else {
                    System.out.println("Pair #" + i + ": Love is not all you need!");
                }
            }
        } catch (Exception e) {
        }
    }

    public static int getGCD(int a, int b) {
        if (b == 0) {
            return a;
        }
        return getGCD(b, a % b);
    }
}
 

arrow
arrow
    全站熱搜

    awesq123 發表在 痞客邦 留言(0) 人氣()