2009-06-04 20:22:43來源不明

ACM 10193 10193 - All You Need Is Love

作法:數學(GCD最大公因數)

先轉成10進位的數,再作展轉

/**********************************************************/

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int gcd(int a,int b)              
 {              
  int temp;              
  while(a%b)                    
   {                    
    temp=a;                    
    a=b;                    
    b=temp%b;                               
   }              
   return b;              
 }
main()
{
 int n,nn,a;
 char x[100],y[100];
 scanf("%d",&n);
 for(nn=1;nn<=n;nn++)
  {
    scanf("%s %s",x,y);
    int xx=strlen(x),yy=strlen(y),num1=0,num2=0,temp=1;
    for(a=xx-1;a>=0;a--)
         {
            if(x[a]=='1') num1=num1+temp;
            temp=temp*2;
          }
    temp=1;
    for(a=yy-1;a>=0;a--)
         {
            if(y[a]=='1') num2=num2+temp;
            temp=temp*2;
          }
    printf("Pair #%d: ",nn);
    if(gcd(num1,num2)==1)
     printf("Love is not all you need!\n");
    else printf("All you need is love!\n");
  }
 return 0;
}