# Leo's程式筆記
## 三角形
```c=
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define max(a,b) (a>b)? a:b
#define min(a,b) (a>b)? b:a
int swap(int *a , int *b)
{
int temp = *a ;
*a = *b ;
*b = temp ;
}
int main()
{
float tri[3] = {15,3,16};
int area = 0 ;
float s = (tri[0] + tri[1] + tri[2] )/2.0;
/* input */
/* bubble sort */
for (int i = 0 ; i< 3 ; i ++)
{
for (int j = 0 ; j < 3 - i - 1 ; j ++)
{
if (tri[j] > tri[j+1])
{
swap(tri+j,tri+j+1);
}
}
}
/* whther is tri */
if (!(tri[0] + tri[1] > tri[2] ))
{
printf("Cannot form a triangle.\n");
return 0 ;
}
else
{
for (int i = 0 ; i < 3 ; i++)
{
if (i != 2 )
{
printf("%.0f > ",tri[i]) ;
}
else
{
printf("%.0f",tri[i]) ;
}
}
printf("\n") ;
/* type of */
if ((tri[0] * tri[0] + tri[1] * tri[1]) > tri[2] * tri[2] )
{
printf("It is acute triangle.");
}
else if ((tri[0] * tri[0] + tri[1] * tri[1]) == tri[2] * tri[2])
{
printf("It is Right triangle.");
}
else if ((tri[0] * tri[0] + tri[1] * tri[1]) < tri[2] * tri[2])
{
printf("It is Obtuse triangle.");
}
printf("Area = %.2f \n", sqrt(s*(s-tri[0])*(s-tri[1])*(s-tri[2])));
}
printf("Hello world!\n");
return 0;
}
```
## 回文
```c=
int main()
{
char int_str[100] = {0} ;
int length = 0 ;
int isaPalindrome = 1 ;
printf("Enter your word: ");
scanf("%s",int_str);
printf("\nstring length = ");
/* 判斷多長 */
for (int i = 0 ; int_str[i]!='\0' ; i ++)
{
length ++ ;
// printf("%d " , int_str[i]);
}
printf("%d\n\n",length);
/* Whether is a 回文 */
/* odd */
for (int i = 0 ; i < length /2 ; i ++)
{
if (int_str[i] != int_str[length-1 -i])
{
printf("It is not a Palindrome.\n");
return 0 ;
}
}
printf("It is a Palindrome.\n");
return 0;
}
```
## 身分證
```c
int Vertify (char *id)
{
int sum = 0 ;
int id2number[] = {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};
int firstword = id2number[id[0] - 'A'] ;
/* first word */
sum += (firstword/10) + firstword%10 ;
/* last word */
sum += (id[9]) ;
/* 1~9 words process */
for (int i = 1 ; i < 9 ; i ++)
{
sum += id[i] * (9-i) ;
}
printf("SUM = %d\n" , sum) ;
if (sum%10 == 0 )
{
return 1;
}
else
{
return 0;
}
}
```
## 插入排序法
```c=
int insert_sort (int *arr , int n )
{
int j = 0 ;
int temp ; /* store temporary */
for (int i = 1 ; i < n ; i ++)
{
temp = arr[i] ;
for( j = i ; j > 0 && temp < arr[j-1]; j --)
{
/* move back the array */
arr[j] = arr[j-1] ;
}
/* insert value to the space of array */
arr[j] = temp ;
}
}
```
## 最大公因數
```c=
int gcd(int a, int b )
{
if (b == 0)
return a ;
else
return gcd(b, a%b) ;
}
```
## 生肖
```c=
int main()
{
int ref_year = 2003 ; /* goat year */
int in_year = 2002 ;
int delta_years ;
char *p[12] ={"goat","monkey","chicken","dog","pig","rat","cow","tiger","rabbit","dragon","snake","horse"};
// printf("Please input your birth year:%d") ;
// scanf("%d",&in_year) ;
/* Calculate the diffence btw ref and inyear */
delta_years = ((in_year - ref_year))%12 ;
/* negative+12 shift to right index */
if (delta_years <0)
delta_years+=12 ;
printf("Your birth year is ");
printf("%s",*(p+delta_years)) ;
return 0;
}
```
## 閏年
```c=
int IsLeap(int years)
{
if ((years % 4 == 0 && (years % 100) != 0) || (years %400 == 0 ))
{
return 1 ;
}
else
{
return 0 ;
}
}
```