11 Ocak 2013 Cuma

Binary Conversion

Problem Description

Convert two binary numbers into two decimal numbers and compute their sum. Your program has to convert two binary numbers b1, b2 of 8 bits into two decimal numbers d1, d2 respectively. Then compute the result of (d1+d2).

Input File Format

The input consists of N cases. The first line of the input contains only one positive integer N indicating the number of test cases, followed by N following cases. Each case is exactly in one line with two binary numbers b1, b2 (at most 8 digits, and each digit is either 0 or 1) separated by one space. Note that 1 ? N ? 2147483647.

Output Format

For each case, print the result in one line.
Sample Input:
2
01010000 00000001
11110000 10000000
Sample Output:
81
368


  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <math.h>  
  4. void binary(int *result);  
  5. int main(int argc, char *argv[])  
  6. {  
  7.     int i,k,a=0,b=0;  
  8.     char c;  
  9.     scanf("%d%*c",&k);  
  10.     for(i=0;i<k;i++){  
  11.         a=0, b=0;  
  12.         binary(&a);  
  13.         getchar();  
  14.         binary(&b);  
  15.         getchar();  
  16.         printf("%d\n",a+b);  
  17.     }  
  18.     return 0;  
  19. }  
  20. void binary(int *result){  
  21.     char c;  
  22.     int i=0;  
  23.     for(i=0;i<8;i++){  
  24.     scanf("%c",&c);  
  25.     *result+=(int)floor(pow(2,7-i))*(c-'0');  
  26.     }  
  27. }  

完美數


Problem Description
An integer number is said to be a perfect number if its factors, including 1 (but not the number itself), sum to the number. For example, 6 is a perfect number because 6=1+2+3. Write a function perfect that determines if parameter number is a perfect number.
請勿用暴力破解
ex:
if(input<100)
printf("6 28 is perfect number\n");
Input Format
大於10的整數
Output Format
Print all the perfect numbers with the format “X is perfect number”.
Sample Input
100
Sample Output
6 28 is perfect number


  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. int isperfect(int val);  
  4. int main(int argc, char *argv[]) {  
  5.     int number,i;  
  6.     scanf("%d",&number);  
  7.     for (i=1;i<number;i++){  
  8.         if(isperfect(i))  
  9.         printf("%d ",i);  
  10.           
  11.     }  
  12.     printf("is perfect number\n");  
  13.     system("PAUSE");  
  14.     return 0;  
  15. }  
  16. int isperfect(int val)  
  17. {  
  18.  int i;  
  19.  int sum = 0;  
  20.  for (i = 1; i <= val/2; i++)  
  21.   if(val % i == 0)  
  22.    sum += i;  
  23.   
  24.   
  25.  if(sum == val)  
  26.   return 1;  
  27.  else  
  28.  return 0;  
  29.     
  30. }  


Fibonacci number

給予一個整數n,請撰寫Fibonacci number

F(1) = 1 ; F(2) = 1;
F( n ) = F(n-1) + F(n-2) ;n > 2

請用迴圈撰寫

Sample Input
5
11 

Sample Output
5
89


  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. int main(int argc, char *argv[]) {  
  4.      int n,fibonacci[100],i,sum;  
  5.      scanf("%d",&n);  
  6.      fibonacci[0]=0;   
  7.      fibonacci[1]=1;  
  8.      if(n==0)        
  9.         printf("0\n");  
  10.      else if(n==1)   
  11.         printf("1\n");  
  12.      else    
  13.         {  
  14.             for(i=2;i<=n;i=i+1)  
  15.                 {  
  16.                  fibonacci[i]=fibonacci[i-1]+fibonacci[i-2];  
  17.                  sum=fibonacci[i];  
  18.             }  
  19.             printf("%d\n",sum);  
  20.         }  
  21.     return 0;  
  22. }  

數字菱形輸出


給你一個數字,請你參考範例輸入輸出的形式顯示圖形樣式。

輸入說明輸入一個正整數,介於 1 ~ 9 。
輸出說明:請參考範例輸出。
範例:


Sample Input:Sample Output:
1
2
3
1
ST13-1
ST13-2
  1. #include<stdio.h>  
  2. #include<stdlib.h>  
  3. void diamond(int n)  
  4. {  
  5.     int i,j,k;  
  6.     for(i=1;i<=n;i++)  
  7.     {  
  8.         k=n-i;  
  9.         for(j=1;j<=n-i;j++)  
  10.         {  
  11.             printf(" ");  
  12.         }  
  13.         for(j=1;j<=i;j++)  
  14.             printf("%d",j);  
  15.         for(j=i-1;j>=1;j--)  
  16.             printf("%d",j);  
  17.         for(j=1;j<=n-i;j++)  
  18.         {  
  19.             printf(" ");  
  20.         }  
  21.         printf("\n");  
  22.     }  
  23.     for(i=n-1;i>=1;i--)  
  24.     {  
  25.         k=n-i;  
  26.         for(j=1;j<=n-i;j++)  
  27.         {  
  28.             printf(" ");  
  29.         }  
  30.         for(j=1;j<=i;j++)  
  31.             printf("%d",j);  
  32.         for(j=i-1;j>=1;j--)  
  33.             printf("%d",j);  
  34.         for(j=1;j<=n-i;j++)  
  35.         {  
  36.             printf(" ");  
  37.         }  
  38.         printf("\n");  
  39.     }  
  40. }  
  41.       
  42. int main()  
  43. {  
  44.     int n;  
  45.     while(scanf("%d",&n)!=EOF)  
  46.     {  
  47.         diamond(n);  
  48.     }  
  49.     return 0;  
  50. }  

身分證驗證器


問題描述:
請勿使用陣列!
設計一個程式可以檢查身分證字號的正確性 ( 應檢查性別欄及檢查碼是否正確 ) 。
身分證字號共有十個碼,且有一定的編碼規則,其檢查編碼的規則如下:
A1N1N2N3N4N5N6N7N8N9
檢查碼
1 :代表男性
2 :代表女性
英文字 母
其中檢查碼的計算方法如下:
Step 1: 根據下表查出第一碼的英文字母對應到的兩位數代號。
字母ABCDEFGHJKLMNPQRSTUVXYWZIO
代號1011121314151617181920212223242526272829303132333435
Step 2: 令此代號之十位數為 X1 ,個位數為 X2 。例如 Y 的代號 31 ,則 X1=3 ; X2=1 。
Step 3: 運用下面的公式計算之。如果 P 可以被 10 整除,則此組身份證號碼是對的,反之則是錯的。
CAR34
輸入說明:
輸入身分證字號,第一碼為英文大寫。
輸出說明:
若身分證字號正確,印出「驗證正確」;不正確則反之。
範例:
Sample InputSample Output
A123456789CORRECT!!!
I147945294CORRECT!!!
W193867023CORRECT!!!
L163690274WRONG!!!



  1. #include<stdio.h>  
  2. #include<stdlib.h>  
  3. int id_num(char ch)  
  4. {  
  5.     switch(ch)  
  6.     {  
  7.         case 'A'return 10 ;break;   case 'P'return 23; break;  
  8.         case 'B'return 11 ;break;   case 'Q'return 24; break;  
  9.         case 'C'return 12 ;break;   case 'R'return 25; break;  
  10.         case 'D'return 13 ;break;   case 'S'return 26; break;  
  11.         case 'E'return 14 ;break;   case 'T'return 27; break;  
  12.         case 'F'return 15 ;break;   case 'U'return 28; break;  
  13.         case 'G'return 16 ;break;   case 'V'return 29; break;  
  14.         case 'H'return 17 ;break;   case 'W'return 32; break;  
  15.         case 'I'return 34 ;break;   case 'X'return 30; break;  
  16.         case 'J'return 18 ;break;   case 'Y'return 31; break;  
  17.         case 'K'return 19 ;break;   case 'Z'return 33; break;  
  18.         case 'L'return 20 ;break;     
  19.         case 'M'return 21 ;break;     
  20.         case 'N'return 22 ;break;     
  21.         case 'O'return 35 ;break;  
  22.     }  
  23. }  
  24. int main()  
  25. {  
  26.     int idnum[26]={10,11,12,13,14,15,16,17,34,18,19,20,21,22,35,23,24,25,26,27,28,29,32,30,31,33};  
  27.     char id[11];  
  28.     id[10]='\0';  
  29.     int i=0,tmp,total=0;  
  30.     for(i=0;i<10;i++)  
  31.     scanf("%c",&id[i]);  
  32.     if(id[0]<='z'&& id[0]>='a') id[0]-=32;   
  33.     tmp=id_num(id[0]);  
  34.     total=(tmp%10)*9+tmp/10+(id[1]-'0')*8+(id[2]-'0')*7+(id[3]-'0')*6+(id[4]-'0')*5+(id[5]-'0')*4+(id[6]-'0')*3+(id[7]-'0')*2+(id[8]-'0')*1;  
  35.     if((10-total%10) ==(id[9]-'0'))  
  36.         printf("CORRECT!!!\n");  
  37.     else  
  38.         printf("WRONG!!!\n");     
  39.     system("PAUSE");  
  40.   
  41. return 0;   
  42. }  

Binary Conversion

Problem Description Convert two binary numbers into two decimal numbers and compute their sum. Your program has to convert two binary number...