2012年1月13日 星期五

acm 10963

#include <stdio.h>
#include <math.h>
int main()
{
  int i,data,a,y1,y2,temp,first=0;
 
  while(scanf("%d",&data)==1)
  {   
      
     int flag =1; 
     data--; 
     if(first)
        printf( "\n" );
     first++;
     scanf("%d",&a);    
     scanf("%d %d",&y1,&y2);
     temp= abs(y1-y2);
    
     for(i=0;i<a;i++)
     {
       scanf("%d %d",&y1,&y2);             
       if(abs(y1-y2)!=temp)
          flag=0;            
     }
    
     if(flag)
       printf("Yes\n");
     else                   
       printf("No\n");                   
  } 
     
 system("pause");
 return 0;   
}

acm 10929 You can say 11

 [心得]
    if((abs(odd-even))%11==0)  ,(abs(odd-even)) 記得要括弧,不然會一直錯!


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

char s[1002];

int main()
{
  while(gets(s))
  {
    int i,length,num;
    int odd=0,even=0;
    if(s[0]=='0'&& s[1]=='\0')
      break;
   
    length =strlen(s);               
    for(i=0;i<length;i++)
    {              
      if((i%2)== 1) /*奇數位元*/
        odd += s[i]-48;
      else        /*偶數位元*/
        even+= s[i]-48;                         
    }
  
    if((abs(odd-even))%11==0)
      printf("%s is a multiple of 11.\n",s);
    else 
      printf("%s is not a multiple of 11.\n",s);        
                   
  } 
 
 system("pause");
 return 0;   
}

acm 10924 Prime Words

[心得] //0.004

#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main()
{
  char s[30];
  int ans[53];
  int i,length,sum,flag;
   
  while(gets(s)!=NULL)
  {
     memset(ans,0,sizeof(ans));               
     length =strlen(s);
     sum=0;
     flag=0;                  
     for(i=0;i<length;i++)
     {
       if(isupper(s[i]))
          ans[(s[i]-38)]+=1;  /* 大寫A ASCII從65map到陣列ans27的位置 */
       else  
          ans[(s[i]-96)]+=1;  /* 小寫a ASCII從97map到陣列ans1的位置 */             
     } 
     for(i=1;i<53;i++)
     {            
       if(ans[i])
          sum=sum+ans[i]*i;                  
     }
     for(i=2;i<sum;i++)
     {
       if(sum %i ==0)
          flag=1;                    
     }
     if(flag)
       printf("It is not a prime word.\n");
     else 
       printf("It is a prime word.\n");
                      
  }   
  system("pause");
  return 0;   
}

acm 10903 Rock-Paper-Scissors Tournament

[心得] //0.256

1.memset也可以初始化 布林陣列 ,memset define 在 "string.h"裡
2. res[]陣列是用來記錄選手i贏的次數
3.flag[]陣列是,表示這次比賽如果不是平手,=>用來記錄選手i 這次比賽有意義
4, time[] 陣列是用來紀錄選手i到底比賽幾次(但是平手不紀錄)

#include <stdio.h>
#include <string.h>
#include <stdbool.h>
 
int i,j=0,number,time[150],game,p1,p2,res[150];
char m1[10],m2[10];

int main()
{

  while(scanf("%d %d",&number,&game)==2)
  {
                 
     if(number==0)
        break;
     if(j>0)
        printf("\n");
     j++;
    
     bool flag[number+1];
     game= game*number*(number-1)/2;
     memset(res,0,sizeof(res));
     memset(time,0,sizeof(time));
     memset(flag,false,sizeof(flag));
    
     for(i=0;i<game;i++)
     {
    
      scanf("%d %s %d %s",&p1,m1,&p2,m2);
        
       if(m1[0]=='r' && m2[0]=='p') 
           {res[p2]+=1; flag[p1]=flag[p2]=true; time[p1]+=1;time[p2]+=1;}
       else if (m1[0]=='r' && m2[0]=='s')  
           {res[p1]+=1; flag[p1]=flag[p2]=true; time[p1]+=1;time[p2]+=1;} 
       else if (m1[0]=='p' && m2[0]=='r')   
           {res[p1]+=1; flag[p1]=flag[p2]=true; time[p1]+=1;time[p2]+=1;}
       else if (m1[0]=='p' && m2[0]=='s')    
           {res[p2]+=1; flag[p1]=flag[p2]=true; time[p1]+=1;time[p2]+=1;;}
       else if (m1[0]=='s' && m2[0]=='p')    
           {res[p1]++;  flag[p1]=flag[p2]=true; time[p1]+=1;time[p2]+=1;}
       else if (m1[0]=='s' && m2[0]=='r')
           {res[p2]+=1; flag[p1]=flag[p2]=true; time[p1]+=1;time[p2]+=1;}
    
     }
     for(i=1;i<=number;i++)
     {
        if( flag[i] == false)
           printf("-\n");
        else
        {                    
           printf("%.3f\n",(double)res[i]/time[i]);
        }
     } 
  } 

  system("pause");
  return 0;   
}

2012年1月11日 星期三

acm 10878: Decode the tape

[心得] 0.008

1. ,每行都是一個ASCII 碼,每行只要注意 'o'出現的位置,轉換成相對應的ASCII碼

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

int main()
{

   int length,i,sum,counter=0;
   char s[20];
   char output[100000];
  
       gets(s);
       
       while(gets(s)!=NULL)
       {  
          if(strcmp(s,"___________")==0)
            break;
         sum=0;
       /*  for(i=0;i<=11;i++)
         printf("s[%d]=%c\n",i,s[i]);*/
      
          for(i=0;i<=11;i++)
          {  
              if(s[i]=='o')
              {
                  if((9-i)==0)
                    sum =sum+1;
                  else if((9-i)==1)
                    sum =sum+2;
                  else if((9-i)==2) 
                    sum =sum+4;
                  else 
                    sum = sum+pow(2,(8-i));
                     
              } 
          
          }
             /*printf("%c\n",sum); */
             output[counter]=sum;
             counter++;
                         
       } 
      
        for(i=0;i<counter;i++)
           printf("%c",output[i]); 
        /*counter=0; 
        printf("\n");*/

   system("pause");
   return(0);   
}

acm 10812: Beat the Spread!

[[心得]  0.004

#include <stdio.h>

int main()
{
  int a,b,data;

  while(scanf("%d",&data)==1)
  {
       int k;                          
       for(k=0;k<data;k++)
       {
           int x,y;      
           scanf("%d %d",&a,&b);
           if(b>a||(a+b)%2!=0)
              printf("impossible\n");  
           else
           {
              x = (a+b)/2;
              y = a-x;
              if(x<0||y<0)
                 printf("impossible\n"); 
              else
                 printf("%d %d\n",x,y);                             
           }  
       }                 
  }
   
   
  system("pause");
  return 0;   
}

acm 10789: Prime Frequency

[心得] 0.004 
用一個MAP(255 ASCII 最大好像255)陣列,存每個字母出現次數,  如果出現次數大於2才需要判斷(用暴力法判斷是否為質數) ,1非質數

#include <stdio.h>
#include <stdbool.h>

int record;
bool isprime(int temp)
{
  int k;
  for(k=2;k<temp;k++)  
  {                  
      if(temp%k==0)
      {
         return false;
      }  
  }
  record =1;
  return  true;   
}
int main()
{

  int data,i,length,counter=0;
  bool flag= false;
 
  scanf("%d",&data);
  scanf("\n");
  while(data)
  {

     char s[2002];
     while(gets(s)!=NULL)
     {   
              
         int map[256]={0};
         record=0;
         data--;   
         counter++;
         length =strlen(s);
         printf("Case %d: ",counter);
        
         /*for(i=0;i<length;i++)
           printf("s[%d]= %c\n",i,s[i]);*/
        
         for(i=0;i<length;i++)
             map[s[i]]++; 
        
         /*for(i=0;i<255;i++)
           printf("map[%d]= %d\n",i,map[i]); */
                                     
         for(i=0;i<255;i++)                
         {
             if(map[i]>=2)                             
                flag = isprime(map[i]);
             if(flag)
             {
                printf("%c",i);
                flag =false;
             }
          }
          if(!record)
             printf("empty\n");
          else
             printf("\n"); 
      }                   

  }
   
   
 system("pause");
 return 0;   
}

acm 10783: Odd Sum

[心得]  0.004
判斷奇數 ,照題目說的做就好了

#include <stdio.h>
int main()
{
   int data;
   int a,b,i,sum=0,counter=1;
   scanf("%d",&data);
   while(data)
   {   
       int sum =0;                
       scanf("%d %d",&a,&b);
       for(i=a;i<=b;i++)
       if(i%2==1)
         sum = sum+ i;               
       printf("Case %d: %d\n",counter,sum);                           
       counter++; 
       data--;
                 
   }
   
  system("pause");
  return 0;
}

acm 10696: f91

[心得] //0.091

#include <stdio.h>

 
int f91( int a)
{
  if(a<=100)
   {
      return f91(f91(a+11));
   }
  else
  {
   
     return (a-10);
  } 
  
}

int main()
{
  int a,b;
 
  while(scanf("%d",&a)==1)
  {
                         
     if(a==0)   
      break;
       b=a;
       b=f91(b);
       printf("f91(%d) = %d\n",a,b);
                     
  }
      
  system("pasue");
  return 0;
    
}

C acm10673 Play with Floor and Ceil

對任何2個整數 x 和 k,存在另2個整數 p 和 q 使得:

要證明上面的式子是一件相當容易的事,所以我們不會要求你去做。我們要你做的事甚至更容易一些。給你 x 和 k 的值,請你找出 p 和 q 使得上面的式子成立
對每組測試資料輸出一列,含2個整數 p 和 q 。假如存在不只一組答案,輸出任何一個都可以。

 [心得] 0.340

1. % (mod)只能用int ,所以變數宣告為double,運算時必須type casting 成int 型態
2. Solution
  
CASE:(整除) 
  中floor 和ceiling 相同(表示x/k可整除),
     ==> 等號兩邊同乘k
  ==> kx= px+qx;
     ==> k=p+q; (最簡單的方式,令p為0, k=q)


CASE : (差 1)
  中floor 和ceiling差1,
  最簡單的解,令p=-x, q=x;

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

int main()
{
  long int x,k,p,q;
  int i,n;

  scanf("%d",&n);
  while(n)
  {
    
     for(i=0;i<n;i++)
     {
        scanf("%ld %ld",&x,&k);
        if( (int)x%(int)k ==0)
        {
            p=0; q=k;
        }
        else /*floor(x/k) ceil(x/k) 差 1*/
        {    
           p=(0L-x);
           q=x;
        }
        printf("%ld %ld\n",p,q);               
     }
     n--;              
 
  } 
  system("pause");
  return 0;   
}

2012年1月10日 星期二

acm 10589 Area

[心得]  0.096

1.一開始就有注意到 int 最好不要和 浮點數比較的問題,所已全部變數宣告成double
   這樣在DEV C++裡雖然答案是對的 但是送上去會得到WA,所以只好參考網路上別人的答案把int 運算都 type casting 成double,才AC!
2.每個點與ABCD 的距離都小於半徑!



#include <stdio.h>


int main()
{
  
   int a,n;
   while(scanf("%d %d",&n,&a)==2)
   { 
      if(n==0 &&a==0)
         break;            
      int i,counter=0;            
      double x,y;
      for(i=0;i<n;i++)
      {
          scanf("%lf %lf",&x,&y);
          if((x*x)+(y*y)<= (double)a*a&&
             (a-x)*(a-x)+(y*y)<=(double)a*a&&
             (a-x)*(a-x)+(a-y)*(a-y)<=(double)a*a&&
             (x*x)+(y-a)*(y-a)<=(double)a*a)
             counter++;
             
      }
     
      printf("%.5lf\n",((double)counter/(double)n*(double)a*(double)a));             
   }
    return 0;
}

2012年1月9日 星期一

acm 10550 Combination Lock

[心得] 0.008
1.順時針轉鎖,但是實際上轉盤是逆時針轉
   假設輸入a b c d四個數字:
  a->b 是順時鐘轉, (轉盤實際是逆時針轉)
           case : a>b (a-b)*9
           case :a<b  (40-b+a)*9

  b->c 是逆時針,(轉盤實際是順時針轉)
           case : b>c (40-b+c)*9
           case:  b<c (b-c)*9
  c->d 同 a->b

#include <stdio.h>

int angle(int x,int y)
{
    if(x<y)
       return((40+x-y)*9);
    else
       return ((x-y)*9);
}


int angle_rev(int x,int y)
{
    if(x<y)
       return ((y-x)*9);
    else
       return((40-x+y)*9); 
}
int main()
{
  int a,b,c,d;
 
  while( scanf("%d %d %d %d",&a,&b,&c,&d)!=EOF)
  {
     int angle_a=1080;   
    if(a==0&&b==0&&c==0&&d==0)
      break;
    
    angle_a+=angle(a,b)+angle_rev(b,c)+angle(c,d); 
    printf("%d\n",angle_a); 
 
  }   
  system("pause");
  return 0;   
}

acm 10499 The Land of Justice

[心得] 0.036
球體表面積公式為 4*π*R*R
1.如果切成 一半,可以想像多兩個大圓面積 為2*(R*R*π)
    ==>2*(R*R*π) /4*π*R*R =50%
 2.如果球體等分成3塊, 表面積會多 6*(1/2*R*R*π)  (想像每個切面都是1/2個大圓面積)
    ==>3*(R*R*π) /4*π*R*R =75%
 3.如果球體等分成3塊, 表面積會多 8*(1/2*R*R*π)
    ==>4*(R*R*π) /4*π*R*R =100%



#include <stdio.h>

int main()
{
   long long int number;
   while(scanf("%lld",&number)!=EOF)
   {
      if(number <0)
         break;                            
      if(number == 1) 
         printf("0%%\n");
      else
         printf("%lld%%\n",25*number);          
   }
  system("pause");
  return 0;
}

acm 10473: Simple Base Conversion

 [心得] :0.012
1.題目說這個數10進位的值一定小於 231 
   所以可以用2的31次方數值試看看
 2.用long long int宣告 scanf 記得用%lld

3. (網路上別人寫的文章)
字串轉數字
int nValue;
float fValue;
CString szValue = "224";
sscanf(szValue,"%d",&nValue);
szValue = "5.82";
sscanf(szValue,"%f",&fValue);
以上為sscanf()的用法
第一個參數為我們所要轉換的字串
第二個參數則是該字串的格式 如果我們要將他轉成整數 則裡面就是"%d"
如果是要轉浮點數 裡面就是"%f"
轉好的結果就會放在第三個參數所指的位置裡面
特別要注意 第三個參數是要傳入指標 不是傳入數值

#include <stdio.h>

int main()
{
   char a[4000];
   long long int number;
   while(gets(a)!=NULL)
   {
     if(a[0]=='-') break;
    
     if(a[1]=='x')
     {
       sscanf(a,"%x" ,&number),
       printf("%lld\n", number);  
     }
     else
     {
        sscanf(a ,"%lld" ,&number),
        printf("0x%X\n", number);  
     }        
   }
  system("pause");
  return 0;
}

2012年1月8日 星期日

acm 10370

 [心得]  0.016
1, answer[answer_count]=(double)100/num*counter;
    一開始寫成answer[answer_count]=(double)(100/num)*counter,小數點後都是0,要注意
2.num 一開始為了方便宣告成double,結果連續scanf()一直有問題,要宣告成int
3. printf("%.3lf%%\n", answer[i]); 注意印出百分比 %% 要寫兩個

#include <stdio.h>
int main()
{
  int i,data,counter,answer_count=0;
  int score[3000],sum,num;
  double answer[3000];
  double average;
  scanf("%d",&data);
  while(data)
  { 
     sum=0; counter=0;
     scanf("%d",&num);

     for(i=0;i<num;i++)
     {  
        scanf("%d",&score[i]);
        sum=sum+score[i];
     }    
     average = sum/num;
     for(i=0;i<num;i++)
     {
        if(score[i]>average)
        counter++;
     }
     answer[answer_count]=(double)100/num*counter;
     answer_count++;
     data--;   
    
  } 
    for(i=0;i<answer_count;i++)
    {
       printf("%.3lf%%\n", answer[i]);
    }
   
  /*system("pause");*/
  return 0;   
}

acm 10340 All in All

[心得]  0.24
原來我題目看錯了!XD 頭昏!
VERDI vivaVittorioEmanueleReDiItalia YES
我前面兩個寫的都是比對要完全相同才行 t裡的字串要完全相同才判斷正確,
其實不用只要 當你移走t字串中的某些字元後,剩下的字串就是s。就可以了!
也就是 s在t中不需要完全相連!

#include <stdio.h>
#include <string.h>
#include <stdbool.h>
int main()
{
  char s[1000000],t[1000000]; 
  int i,j,counter;
  bool flag = false;
  int temp;
  int length;
  while(scanf("%s %s",s,t)!=EOF)
  {
 
    int t_length = strlen(t);
    int s_length = strlen(s);
    j=0;
    for(i=0;i<t_length;i++)
    {     
       if(t[i]==s[j]&& j<s_length)
       {  
            j++;
            if(j==s_length)
           {
                flag = true;
                i=t_length;
                break;
           }
                               
       }
               
    }                    

     if(flag)
     {
       flag = false;
       printf("Yes\n");
     }
     else
       printf("No\n");            
  }
  system("pause");
  //return 0;   
}



[心得] WA 不知道邏輯哪裡錯了!有空再看!
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
int main()
{
  char s[1000000],t[1000000]; 
  int i,j,counter;
  bool flag = false;
  int temp;
  int length;
  while(scanf("%s %s",s,t)!=EOF)
  {
 
    counter=0;
    int t_length = strlen(t);
    int s_length = strlen(s);
    for(i=0;i<t_length;i++)
    {    j=0;
         temp=i;
         counter =0;
       while(t[temp]==s[j]&& j<s_length)
       {  
           temp++;
           j++;
           counter++;
           if(counter==s_length)
           {
                flag = true;
                i=t_length;
                break;
           }
                               
       }
               
    }                    

     if(flag)
     {
       flag = false;
       printf("Yes\n");
     }
     else
       printf("No\n");            
  }
  system("pause");
  //return 0;   
}


[心得] WA
不懂為什麼用 strstr判斷 會WA,只好重寫一支,不知道是否於字串超長有關係!
剛剛GOOGL了一下, 使用 strstr的確有長度限制!

#include <stdio.h>
#include <string.h>
int main()
{
  char s[100000],t[100000];
  while(scanf("%s %s",s,t)!=EOF)
  {
    int length,comp;
    char *pos;  
    pos = strstr(t,s);
    if(pos!=NULL)
      printf("YES\n");   
    else
      printf("NO\n");
           
  } 
   
 system("pause");
 return 0;
}

acm 10300 Ecological Premium

[心得]  0.004

1.double 要用lf

#include <stdio.h>
int main()
{
  int i,counter,data; 
  double award,answer[21];
  counter=0;
  scanf("%d",&data);
 
  while(data)
  { 
    double a,b,c;
    int famer;
    data--;
    award=0;
  
             
    scanf("%d\n",&famer);
    for(i=0;i<famer;i++)
    {
                    
       scanf("%lf %lf %lf",&a,&b,&c);
       award = award+a*c;                
    }  
    answer[counter]= award;
    counter++;                  
                         
  }   
    for(i=0;i<counter;i++)
      printf("%.0f\n",answer[i]);
   system("pause");
   //return 0;
}

ACM Solved 20 Ranking17159

ACM 10222 Decode The Mad Man

[心得] :0.004
1. ACM 10082 WERTYU差不多
2.注意大寫要轉換成小寫
3.\ 要打成\\ 上次打錯   XD!

#include <stdio.h>
#include <string.h>

int main()
{
 int c;
 char *pos;  
 char key[]="1234567890-=qwertyuiop[]\\asdfghjkl;'zxcvbnm,./";
 while((c=getchar())!=EOF)
 {       
    if(isupper(c))
       c= tolower(c);        
    pos= strchr(key,c);         
    putchar((pos)?key[pos-key-2]:c);                     
 }

 system("pause");
 //return 0;   
}

ACM 10209 Is This Integration ?

[心得] :0.044



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

#define PI  2.0*acos(0.0)
#define line_area(r)  (r) * (r) * (1  + PI * 1.0 / 3.0 - sqrt(3))
#define point_area(r) (r) * (r) * (-4 + PI * 1.0 / 3.0 + sqrt(3) * 2)
#define square_area(r) (r) * (r) * (4  - PI * 2.0 / 3.0 - sqrt(3))

int main()
{
   double a;
   while(scanf("%lf",&a)==1)
   {
     printf("%.3lf %.3lf %.3lf\n",line_area(a),point_area(a),square_area(a));
   }

  system("pause");  
 // return 0;
}