---
# System prepended metadata

title: CP1 HW01 參考答案
tags: ["CP1\_", HW01]

---

# HW01 參考答案


## 1.1 Print Colorful Words

:::spoiler (Credit: 41171216H 石Ｏ軒)
```c=
#include<stdio.h>
#include<stdint.h>

int main() {
	printf("Now in those days there was in the land of Helsinki a young\n   scholar named \033[0;34mLinus the Torvald\033[m. \033[0;34mLinus\033[m was a devout man, a\n   disciple of \033[0;34mRMS\033[m and mighty in the spirit of \033[0;34mTuring\033[m, \033[0;34mvon\n   Neumann\033[m and \033[0;34mMoore\033[m. One day as he was meditating on the\n   Architecture, \033[0;34mLinus\033[m fell into a trance and was granted a\n   vision. And in the vision he saw a great Penguin, serene and\n   well-favoured, sitting upon an ice floe eating fish. And at\n   the sight of the Penguin \033[0;34mLinus\033[m was deeply afraid, and he\n   cried unto the spirits of \033[0;34mTuring\033[m, \033[0;34mvon Neumann\033[m and \033[0;34mMoore\033[m for\n   an interpretation of the dream.\n");
	printf("\nAnd in the dream the spirits of \033[0;34mTuring\033[m, \033[0;34mvon Neumann\033[m and \033[0;34mMoore\033[m\n   answered and spoke unto him, saying, \033[0;31m\"Fear not,\033[m \033[0;34mLinus\033[m\033[0;31m, most\n   beloved hacker. You are exceedingly cool and froody. The\n   great Penguin which you see is an Operating System which you\n   shall create and deploy unto the earth. The ice-floe is the\n   earth and all the systems thereof, upon which the Penguin\n   shall rest and rejoice at the completion of its task. And\n   the fish on which the Penguin feeds are the crufty Licensed\n   codebases which swim beneath all the earth’s systems. The Penguin\n   shall hunt and devour all that is crufty, gnarly and\n   bogacious; all code which wriggles like spaghetti, or is\n   infested with blighting creatures, or is bound by grave and\n   perilous Licences shall it capture. And in capturing shall\n   it replicate, and in replicating shall it document, and in\n   documentation shall it bring freedom, serenity and most cool\n   froodiness to the earth and all who code therein.\"\033[m\n");
	printf("\n\033[0;34mLinus\033[m rose from meditation and created a tiny Operating System\n   Kernel as the dream had foreshewn him; in the manner of \033[0;34mRMS\033[m,\n   he released the Kernel unto the World Wide Web for all to\n   take and behold. And in the fulness of Internet Time the\n   Kernel grew and replicated, becoming most cool and\n   exceedingly froody, until at last it was recognised as\n   indeed a great and mighty Penguin, whose name was Tux. And\n   the followers of \033[0;34mLinus\033[m took refuge in the Kernel, the\n   Libraries and the Utilities; they installed Distribution\n   after Distribution, and made sacrifice unto the GNU and the\n   Penguin, and gave thanks to the spirits of \033[0;34mTuring\033[m, \033[0;34mvon\n   Neumann\033[m and \033[0;34mMoore\033[m, for their deliverance from the hand of\n   Microsoft. And this was the beginning of the Fourth Age, the\n   age of Open Source.\n");
	printf("\t\t\t\033[0;34mLinus Torvalds\033[m\n\033[0;34mLinus Torvalds\033[m is a Finnish computer scientist renowned for\n   creating the Linux operating system. He released Linux's\n   kernel in 1991, making it open-source, and subsequently, it\n   became an iconic symbol of the free software movement. \033[0;34mlinus\033[m'\n   leadership transformed Linux into a globally collaborative\n   project, widely used in servers, embedded systems, and\n   supercomputers. He is highly respected for his contributions\n   to free software and collaborative spirit, playing a pivotal\n   role within the open-source community.\n");
	printf("\t\t\t      \033[0;34mRMS\033[m\n\033[0;34mRichard Matthew Stallman\033[m, often referred to as \033[0;34mRMS\033[m, is a pioneering\n   American computer programmer and software freedom advocate.\n   He founded the Free Software Foundation (FSF) and initiated\n   the GNU project in the early 1980s, laying the foundation for\n   the modern free software and open-source movement. \033[0;34mStallman\033[m\n   is renowned for his unwavering commitment to software freedom,\n   promoting the use of free and open-source software, and creating\n   the General Public License (GPL) to safeguard users' rights.\n   His contributions have had a profound and lasting impact on\n   the world of technology and open software development.\n");
	printf("\t\t\t     \033[0;34mTuring\033[m\n\033[0;34mAlan Mathison Turing\033[m, a British mathematician, logician, and\n   computer scientist, is celebrated for his groundbreaking\n   contributions to the field of computer science and artificial\n   intelligence. \033[0;34mTuring\033[m is most notably known for developing\n   the \033[0;34mTuring\033[m machine, a theoretical construct that laid the\n   foundation for modern computing. During World War II, he\n   played a pivotal role in deciphering the German Enigma code,\n   a significant contribution to the Allied victory. \033[0;34mTuring\033[m's\n   work also introduced the concept of algorithms and the \033[0;34mTuring\033[m\n   test, a benchmark for machine intelligence. His legacy continues\n   to influence computer science, cryptography, and the quest\n   for artificial intelligence.\n");
	printf("\t\t\t   \033[0;34mVon Neumann\033[m\n\033[0;34mJohn von Neumann\033[m was a Hungarian-American mathematician, physicist,\n   and computer scientist. He made substantial contributions to\n   numerous fields, notably in the development of game theory,\n   quantum mechanics, and the architecture of modern computers.\n   \033[0;34mVon Neumann\033[m's concept of the \033[0;34mvon Neumann\033[m architecture served\n   as the basis for contemporary computer design, where a central\n   processing unit sequentially executes instructions stored in\n   memory. His multidisciplinary approach and pioneering work\n   in computing and mathematics left an indelible mark on science\n   and technology, earning him recognition as one of the most\n   influential intellectuals of the 20th century.\n");
	printf("\t\t\t      \033[0;34mMoore\033[m\n\033[0;34mGordon Earle Moore\033[m is an American engineer and co-founder of Intel\n   Corporation. He is renowned for \033[0;34mMoore\033[m's Law, an empirical\n   observation he made in 1965, predicting that the number of\n   transistors on a microchip would double approximately every\n   two years, leading to exponential growth in computing power\n   and reduced costs. \033[0;34mMoore\033[m's Law has become a guiding principle\n   in the semiconductor industry, driving innovation and shaping\n   the digital age. Gordon \033[0;34mMoore\033[m's contributions revolutionized\n   the technology sector, making him an influential figure in\n   the advancement of modern electronics and computing.\n");
	return 0;	
}
```
:::

## 1.2 Multiplication
:::spoiler Code (Credit: 41247046S 吳Ｏ宇)
```c=
#include<stdio.h>
#include<stdint.h>

int main(){
    
    int32_t c1 = 100, c2 = 100, ans = 0, ansl = 0, a = 0, b = 0;
    printf("Please enter the first number: ");
    if (scanf("%d", &c1) != 1) {
        printf("\x1b[31merror input\n\x1b[0m");
        return 0;
    }
    printf("Please enter the second number: ");
    if (scanf("%d", &c2) != 1) {
        printf("\x1b[31merror input\n\x1b[0m");
        return 0;
    }

    if(c1>99 || c1<0 || c2>99 || c2<0) {
        printf("\x1b[31mWrong input, your input must between 0 ~ 99.\n\x1b[0m");
        return 0;
    }
    
    ans = c1*c2;
    a = c1*(c2%10);
    b = c1*(c2/10);

    if(ans<10){
        ansl=1;
    } else if(ans<100){
        ansl=2;
    } else if(ans<1000){
        ansl=3;
    } else {
        ansl=4;
    }

    switch (ansl) {
        case 1: 
            if(c1>=10){
                printf("  %d %d\n", c1/10, c1%10);
                printf("*)  0\n");
                printf("-----\n    0\n");
            } else if(c2>=10){
                printf("    0\n");
                printf("*)%d %d\n", c2/10, c2%10);
                printf("-----\n    0\n");
            } else {
                printf("  %d\n*)%d\n---\n  %d\n", c1, c2, ans);
            }

            break;

        case 2:
            if(c1<10){
                printf("    %d\n", c1);
            } else {
                printf("  %d %d\n", c1/10, c1%10);
            }
            if(c2<10){
                printf("*)  %d\n", c2);
            } else {
                printf("*)%d %d\n", c2/10, c2%10);
            }
            printf("-----\n  %d %d\n", ans/10, ans%10);

            break;

        case 3:
            if(c1<10){
                printf("      %d\n", c1);
            } else {
                printf("    %d %d\n", c1/10, c1%10);
            }
            if(c2<10){
                printf("*)    %d\n", c2);
            } else {
                printf("*)  %d %d\n", c2/10, c2%10);
            }
            printf("-------\n");

            if(c1<10 || c2<10){
                printf("  %d %d %d\n", ans/100, ans%100/10, ans%10);
            } else {
                if(a<10) printf("      %d\n", a);
                else if (a<100) printf("    %d %d\n", a/10, a%10);
                else printf("  %d %d %d\n", a/100, a%100/10, a%10);

                if(b<10) printf("    %d\n", b);
                else printf("  %d %d\n", b/10, b%10);

                printf("-------\n  %d %d %d\n", ans/100, ans%100/10, ans%10);
            }

            break;

        case 4:
            printf("      %d %d\n", c1/10, c1%10);
            printf("*)    %d %d\n---------\n", c2/10, c2%10);
            if(a<10) printf("        %d\n", a);
            else if (a<100) printf("      %d %d\n", a/10, a%10);
            else printf("    %d %d %d\n", a/100, a%100/10, a%10);
            if(b<10) printf("      %d\n", b);
            else if (b<100) printf("    %d %d\n", b/10, b%10);
            else printf("  %d %d %d\n", b/100, b%100/10, b%10);
            printf("---------\n  %d %d %d %d\n", ans/1000, ans%1000/100, ans%100/10, ans%10);

            break;

    }
    return 0;
}
```
:::

## 1.3 Area

:::spoiler Code
```c=
#include <stdio.h>
#include <stdint.h>

int main() {
    int32_t ax, ay, bx, by; // Coordinates of points A and B

    // Get the two points as input
    printf("Please enter the point A (x,y): ");
    if (scanf("%d,%d", &ax, &ay) != 2) {
        printf("Error: Invalid input format.\n");
        return 1;
    }

    printf("Please enter the point B (x,y): ");
    if (scanf("%d,%d", &bx, &by) != 2) {
        printf("Error: Invalid input format.\n");
        return 1;
    }

    // Check if the points are distinct or the line is parallel to axis X/Y
    if (ax == bx && ay == by) {
        printf("Error: Points A and B are not distinct.\n");
        return 1;
    }
    if (ax == bx) {
        printf("Error: The line is parallel to axis Y.\n");
        return 1;
    }
    if (ay == by) {
        printf("Error: The line is parallel to axis X.\n");
        return 1;
    }

    // Find the equation of the line AB using the point-slope form
    // y - y1 = m(x - x1)
    // where m is the slope and (x1, y1) is a point on the line

    long double m = (long double)(by - ay) / (bx - ax); // Slope of the line

    // Find the x and y intercepts of this line using the slope-intercept form
    long double y_intercept = ay - (m * ax);
    long double x_intercept = -1 * (y_intercept / m);

    // Calculate the area of the triangle
    long double area = 0.5 * x_intercept * y_intercept;
    if(area < 0) area = -area;

    // Print error message if area is 0
    if (area == 0) {
        printf("Error: Area is 0. Please enter valid points.\n");
        return 1;
    }

    printf("Area: %.2Lf\n", area);

    return 0;
}

```
:::

## 1.4 Bidding System for Contract Bridge
:::spoiler Code (Credit: 41247030S 陳Ｏ宏)
```c=
#include <stdio.h>
#include <stdint.h>

int main(){
	int32_t c1=0, c2=0, c3=0, c4=0, c5=0, c6=0, c7=0, c8=0, c9=0, c10=0, c11=0, c12=0, c13=0;
	int32_t HCP=0, S=0, H=0, D=0, C=0, SAKQ=0, HAKQ=0;	 
	printf("1st card: ");
	scanf("%d", &c1);
	if(c1 < 1 || c1 >  52){ printf("Wrong input\n"); return 0; } 

	printf("2nd card: ");
	scanf("%d", &c2);
	if(c2 < 1 || c2 >  52){ printf("Wrong input\n"); return 0; }

	printf("3rd card: ");
	scanf("%d", &c3);
	if(c3 < 1 || c3 >  52){ printf("Wrong input\n"); return 0; }

	printf("4th card: ");
	scanf("%d", &c4);
	if(c4 < 1 || c4 >  52){ printf("Wrong input\n"); return 0; }

	printf("5th card: ");
	scanf("%d", &c5);
	if(c5 < 1 || c5 >  52){ printf("Wrong input\n"); return 0; }

	printf("6th card: ");
	scanf("%d", &c6);
	if(c6 < 1 || c6 >  52){ printf("Wrong input\n"); return 0; }

	printf("7th card: ");
	scanf("%d", &c7);
	if(c7 < 1 || c7 >  52){ printf("Wrong input\n"); return 0; }

	printf("8th card: ");
	scanf("%d", &c8);
	if(c8 < 1 || c8 >  52){ printf("Wrong input\n"); return 0; }

	printf("9th card: ");
	scanf("%d", &c9);
	if(c9 < 1 || c9 >  52){ printf("Wrong input\n"); return 0; }

	printf("10th card: ");
	scanf("%d", &c10);
	if(c10 < 1 || c10 >  52){ printf("Wrong input\n"); return 0; }

	printf("11th card: ");
	scanf("%d", &c11);
	if(c11 < 1 || c11 >  52){ printf("Wrong input\n"); return 0; }

	printf("12th card: ");
	scanf("%d", &c12);
	if(c12 < 1 || c12 >  52){ printf("Wrong input\n"); return 0; }

	printf("13th card: ");
	scanf("%d", &c13);
	if(c13 < 1 || c13 >  52){ printf("Wrong input\n"); return 0; }
	
	//wrong

	//hcp

	//S

	//1
	if(c1 >= 1 && c1 <= 13){
		S++;
		if(c1 == 1) HCP += 4;
		if(c1 ==11) HCP += 1;
		if(c1 ==12) HCP += 2;
		if(c1 ==13) HCP += 3;

		if(c1 == 1 || c1 == 12 || c1 == 13) SAKQ ++;
	}
	//2
	if(c2 >= 1 && c2 <= 13){
		S++;
		if(c2 == 1) HCP += 4;if(c2 ==11) HCP += 1;if(c2 ==12) HCP += 2;if(c2 ==13) HCP += 3;
	
		if(c2 == 1 || c2 == 12 || c2 == 13) SAKQ ++;
	}
	//3
	if(c3 >= 1 && c3 <= 13){
		S++;
		if(c3 == 1) HCP += 4;if(c3 ==11) HCP += 1;if(c3 ==12) HCP += 2;if(c3 ==13) HCP += 3;

		if(c3 == 1 || c3 == 12 || c3 == 13) SAKQ ++;
	}
	//4
	if(c4 >= 1 && c4 <= 13){
		S++;
		if(c4 == 1) HCP += 4;if(c4 ==11) HCP += 1;if(c4 ==12) HCP += 2;if(c4 ==13) HCP += 3;

		if(c4 == 1 || c4 == 12 || c4 == 13) SAKQ ++;
	}
	//5
	if(c5 >= 1 && c5 <= 13){
		S++;
		if(c5 == 1) HCP += 4;if(c5 ==11) HCP += 1;if(c5 ==12) HCP += 2;if(c5 ==13) HCP += 3;
	
		if(c5 == 1 || c5 == 12 || c5 == 13) SAKQ ++;
	}
	//6
	if(c6 >= 1 && c6 <= 13){
		S++;
		if(c6 == 1) HCP += 4;if(c6 ==11) HCP += 1;if(c6 ==12) HCP += 2;if(c6 ==13) HCP += 3;

		if(c6 == 1 || c6 == 12 || c6 == 13) SAKQ ++;
	}
	//7
	if(c7 >= 1 && c7 <= 13){
		S++;
		if(c7 == 1) HCP += 4;if(c7 ==11) HCP += 1;if(c7 ==12) HCP += 2;if(c7 ==13) HCP += 3;
	
		if(c7 == 1 || c7 == 12 || c7 == 13) SAKQ ++;
	}
	//8
	if(c8 >= 1 && c8 <= 13){
		S++;
		if(c8 == 1) HCP += 4;if(c8 ==11) HCP += 1;if(c8 ==12) HCP += 2;if(c8 ==13) HCP += 3;
	
		if(c8 == 1 || c8 == 12 || c8 == 13) SAKQ ++;
	}
	//9
	if(c9 >= 1 && c9 <= 13){
		S++;
		if(c9 == 1) HCP += 4;if(c9 ==11) HCP += 1;if(c9 ==12) HCP += 2;if(c9 ==13) HCP += 3;

		if(c9 == 1 || c9 == 12 || c9 == 13) SAKQ ++;
	}
	//10
	if(c10 >= 1 && c10 <= 13){
		S++;
		if(c10 == 1) HCP += 4;if(c10 ==11) HCP += 1;if(c10 ==12) HCP += 2;if(c10 ==13) HCP += 3;
	
		if(c10 == 1 || c10 == 12 || c10 == 13) SAKQ ++;
	}
	//11
	if(c11 >= 1 && c11 <= 13){
		S++;
		if(c11 == 1) HCP += 4;if(c11 ==11) HCP += 1;if(c11 ==12) HCP += 2;if(c11 ==13) HCP += 3;

		if(c11 == 1 || c11 == 12 || c11 == 13) SAKQ ++;
	}
	//12
	if(c12 >= 1 && c12 <= 13){
		S++;
		if(c12 == 1) HCP += 4;if(c12 ==11) HCP += 1;if(c12 ==12) HCP += 2;if(c12 ==13) HCP += 3;
	
		if(c12 == 1 || c12 == 12 || c12 == 13) SAKQ ++;
	}
	//13
	if(c13 >= 1 && c13 <= 13){
		S++;
		if(c13 == 1) HCP += 4;if(c13 ==11) HCP += 1;if(c13 ==12) HCP += 2;if(c13 ==13) HCP += 3;

		if(c13 == 1 || c13 == 12 || c13 == 13) SAKQ ++;
	}

	//H
	//1
      if(c1 >= 14 && c1 <= 26){
          H++;
          if(c1 == 14) HCP += 4;
          if(c1 ==24) HCP += 1;
          if(c1 ==25) HCP += 2;
          if(c1 ==26) HCP += 3;
  
          if(c1 == 14 || c1 == 25 || c1 == 26) HAKQ ++;
      }
      //2
      if(c2 >= 14 && c2 <= 26){
          H++;
          if(c2 == 14) HCP += 4;if(c2 ==24) HCP += 1;if(c2 ==25) HCP += 2;if(c2 ==26) HCP += 3;
  
          if(c2 == 14 || c2 == 25 || c2 == 26) HAKQ ++;
      }
      //3
      if(c3 >= 14 && c3 <= 26){
          H++;
          if(c3 == 14) HCP += 4;if(c3 ==24) HCP += 1;if(c3 ==25) HCP += 2;if(c3 ==26) HCP += 3;
  
          if(c3 == 14 || c3 == 25 || c3 == 26) HAKQ ++;
      }
      //4
      if(c4 >= 14 && c4 <= 26){
          H++;
          if(c4 == 14) HCP += 4;if(c4 ==24) HCP += 1;if(c4 ==25) HCP += 2;if(c4 ==26) HCP += 3;
  
          if(c4 == 14 || c4 == 25 || c4 == 26) HAKQ ++;
      }
      //5
      if(c5 >= 14 && c5 <= 26){
          H++;
          if(c5 == 14) HCP += 4;if(c5 ==24) HCP += 1;if(c5 ==25) HCP += 2;if(c5 ==26) HCP += 3;
  
          if(c5 == 14 || c5 == 25 || c5 == 26) HAKQ ++;
      }
      //6
      if(c6 >= 14 && c6 <= 26){
          H++;
          if(c6 == 14) HCP += 4;if(c6 ==24) HCP += 1;if(c6 ==25) HCP += 2;if(c6 ==26) HCP += 3;
  
          if(c6 == 14 || c6 == 25 || c6 == 26) HAKQ ++;
      }
      //7
      if(c7 >= 14 && c7 <= 26){
         H++;
         if(c7 == 14) HCP += 4;if(c7 ==24) HCP += 1;if(c7 ==25) HCP += 2;if(c7 ==26) HCP += 3;
 
         if(c7 == 14 || c7 == 25 || c7 == 26) HAKQ ++;
     }
     //8
     if(c8 >= 14 && c8 <= 26){
         H++;
         if(c8 == 14) HCP += 4;if(c8 ==24) HCP += 1;if(c8 ==25) HCP += 2;if(c8 ==26) HCP += 3; 
       
		 if(c8 == 14 || c8 == 25 || c8 == 26) HAKQ ++;
     }
     //9
     if(c9 >= 14 && c9 <= 26){
         H++;
         if(c9 == 14) HCP += 4;if(c9 ==24) HCP += 1;if(c9 ==25) HCP += 2;if(c9 ==26) HCP += 3;
 
         if(c9 == 14 || c9 == 25 || c9 == 26) HAKQ ++;
     }
     //10
     if(c10 >= 14 && c10 <= 26){
         H++;
         if(c10 == 14) HCP += 4;if(c10 ==24) HCP += 1;if(c10 ==25) HCP += 2;if(c10 ==26) HCP += 3;
 
         if(c10 == 14 || c10 == 25 || c10 == 26) HAKQ ++;
     }
     //11
     if(c11 >= 14 && c11 <= 26){
         H++;
         if(c11 == 14) HCP += 4;if(c11 ==24) HCP += 1;if(c11 ==25) HCP += 2;if(c11 ==26) HCP += 3;
 
         if(c11 == 14 || c11 == 25 || c11 == 26) HAKQ ++;
     }
     //12
     if(c12 >= 14 && c12 <= 26){
         H++;
         if(c12 == 14) HCP += 4;if(c12 ==24) HCP += 1;if(c12 ==25) HCP += 2;if(c12 ==26) HCP += 3;
 
         if(c12 == 14 || c12 == 25 || c12 == 26) HAKQ ++;
     }
     //13
     if(c13 >= 14 && c13 <= 26){
         H++;
         if(c13 == 14) HCP += 4;if(c13 ==24) HCP += 1;if(c13 ==25) HCP += 2;if(c13 ==26) HCP += 3;
 
         if(c13 == 14 || c13 == 25 || c13 == 26) HAKQ ++;
     }

	//D
	//1
     if(c1 >= 27 && c1 <= 39){
         D++;
         if(c1 == 27) HCP += 4;
		 if(c1 == 37) HCP += 1;
         if(c1 == 38) HCP += 2;
         if(c1 == 39) HCP += 3;

    
     }
     //2
     if(c2 >= 27 && c2 <= 39){
         D++;
         if(c2 == 27) HCP += 4;if(c2 ==37) HCP += 1;if(c2 ==38) HCP += 2;if(c2 ==39) HCP += 3;

 
     }
     //3
     if(c3 >= 27 && c3 <= 39){
         D++;
         if(c3 == 27) HCP += 4;if(c3 ==37) HCP += 1;if(c3 ==38) HCP += 2;if(c3 ==39) HCP += 3;

 
     }
     //4
     if(c4 >= 27 && c4 <= 39){
         D++;
         if(c4 == 27) HCP += 4;if(c4 ==37) HCP += 1;if(c4 ==38) HCP += 2;if(c4 ==39) HCP += 3;


     }
     //5
     if(c5 >= 27 && c5 <= 39){
         D++;
         if(c5 == 27) HCP += 4;if(c5 ==37) HCP += 1;if(c5 ==38) HCP += 2;if(c5 ==39) HCP += 3;


     }
     //6
     if(c6 >= 27 && c6 <= 39){
         D++;
         if(c6 == 27) HCP += 4;if(c6 ==37) HCP += 1;if(c6 ==38) HCP += 2;if(c6 ==39) HCP += 3;
  
     }
     //7
     if(c7 >= 27 && c7 <= 39){
         D++;
         if(c7 == 27) HCP += 4;if(c7 ==37) HCP += 1;if(c7 ==38) HCP += 2;if(c7 ==39) HCP += 3;

  
     }
     //8
     if(c8 >= 27 && c8 <= 39){
         D++;
         if(c8 == 27) HCP += 4;if(c8 ==37) HCP += 1;if(c8 ==38) HCP += 2;if(c8 ==39) HCP += 3;

  
     }
     //9
     if(c9 >= 27 && c9 <= 39){
         D++;
         if(c9 == 27) HCP += 4;if(c9 ==37) HCP += 1;if(c9 ==38) HCP += 2;if(c9 ==39) HCP += 3;

   
     }
     //10
     if(c10 >= 27 && c10 <= 39) {
  	 	 D++;
         if(c10 == 27) HCP += 4;if(c10 ==37) HCP += 1;if(c10 ==38) HCP += 2;if(c10 ==39) HCP += 3;

   
     }
     //11
     if(c11 >= 27 && c11 <= 39){
         D++;
         if(c11 == 27) HCP += 4;if(c11 ==37) HCP += 1;if(c11 ==38) HCP += 2;if(c11 ==39) HCP += 3;

   
     }
     //12
     if(c12 >= 27 && c12 <= 39){
         D++;
         if(c12 == 27) HCP += 4;if(c12 ==37) HCP += 1;if(c12 ==38) HCP += 2;if(c12 ==39) HCP += 3;

    
     }
     //13
     if(c13 >= 27 && c13 <= 39){
         D++;
         if(c13 == 27) HCP += 4;if(c13 ==37) HCP += 1;if(c13 ==38) HCP += 2;if(c13 ==39) HCP += 3;

    
     }

	//C
	//1
     if(c1 >= 40 && c1 <= 52){
         C++;
         if(c1 == 40) HCP += 4;
         if(c1 ==50) HCP += 1;
         if(c1 ==51) HCP += 2;
         if(c1 ==52) HCP += 3;


     }
     //2
     if(c2 >= 40 && c2 <= 52){
         C++;
         if(c2 == 40) HCP += 4;if(c2 ==50) HCP += 1;if(c2 ==51) HCP += 2;if(c2 ==52) HCP += 3;


     }
     //3
	 if(c3 >= 40 && c3 <= 52){
        C++;
        if(c3 == 40) HCP += 4;if(c3 ==50) HCP += 1;if(c3 ==51) HCP += 2;if(c3 ==52) HCP += 3;

     }
     //4
     if(c4 >= 40 && c4 <= 52){
		 C++;
         if(c4 == 40) HCP += 4;if(c4 ==50) HCP += 1;if(c4 ==51) HCP += 2;if(c4 ==52) HCP += 3;


     }
     //5
     if(c5 >= 40 && c5 <= 52){
         C++;
         if(c5 == 40) HCP += 4;if(c5 ==50) HCP += 1;if(c5 ==51) HCP += 2;if(c5 ==52) HCP += 3;


     }
     //6
     if(c6 >= 40 && c6 <= 52){
         C++;
         if(c6 == 40) HCP += 4;if(c6 ==50) HCP += 1;if(c6 ==51) HCP += 2;if(c6 ==52) HCP += 3;


     }
     //7
     if(c7 >= 40 && c7 <= 52){
         C++;
         if(c7 == 40) HCP += 4;if(c7 ==50) HCP += 1;if(c7 ==51) HCP += 2;if(c7 ==52) HCP += 3;


     }
     //8
     if(c8 >= 40 && c8 <= 52){
         C++;
         if(c8 == 40) HCP += 4;if(c8 ==50) HCP += 1;if(c8 ==51) HCP += 2;if(c8 ==52) HCP += 3;


     }
     //9
     if(c9 >= 40 && c9 <= 52){
         C++;
         if(c9 == 40) HCP += 4;if(c9 ==50) HCP += 1;if(c9 ==51) HCP += 2;if(c9 ==52) HCP += 3;

     }
     //10
	 if(c10 >= 40 && c10 <= 52){
         C++;
         if(c10 == 40) HCP += 4;if(c10 ==50) HCP += 1;if(c10 ==51) HCP += 2;if(c10 ==52) HCP += 3;


     }
     //11
     if(c11 >= 40 && c11 <= 52){
         C++;
         if(c11 == 40) HCP += 4;if(c11 ==50) HCP += 1;if(c11 ==51) HCP += 2;if(c11 ==52) HCP += 3;


     }
     //12
     if(c12 >= 40 && c12 <= 52){
         C++;
         if(c12 == 40) HCP += 4;if(c12 ==50) HCP += 1;if(c12 ==51) HCP += 2;if(c12 ==52) HCP += 3;


     }
     //13
     if(c13 >= 40 && c13 <= 52){
         C++;
         if(c13 == 40) HCP += 4;if(c13 ==50) HCP += 1;if(c13 ==51) HCP += 2;if(c13 ==52) HCP += 3;


     }
	printf("---\n");
	printf("HCP: %d pts\n", HCP);
	printf("Suit: %d-%d-%d-%d\n", S, H, D, C);

	//BIDDING RULE
	
	if(HCP >= 22 && HCP <= 24 && ((S == 4 && H == 3 && D == 3 && C == 3)||(S == 3 && H == 4 && D == 3 && C == 3)||(S == 3 &&H == 3 && D == 4 && C == 3)||(S == 3 && H == 3 && D == 3 && C == 4))){
		printf("The bidding choice: 2NT\n");
	}
	else if(HCP >= 16 ){
		printf("The bidding choice: 1C\n");
	}
    else if(HCP >= 11 && HCP <= 15 && (S >=5 || H >= 5)){
         if(S >= H){
		 printf("The bidding choice: 1S\n");
         }else{
             printf("The bidding choice: 1H\n");
         }
     }

	else if(HCP >= 13 && HCP <= 15 && ((S == 4 && H == 3 && D == 3 && C == 3)||(S == 3 && H == 4 && D == 3 && C == 3)||(S == 3 &&    H == 3 && D == 4 && C == 3)||(S == 3 && H == 3 && D == 3 && C == 4))){
		printf("The bidding choice: 1NT\n");
	}
	else if(HCP >= 11 && HCP <= 15 && D >= 4){
		printf("The bidding choice: 1D\n");
	}
	else if(HCP >= 11 && HCP <= 15 && C >=6){
		printf("The bidding choice: 2C\n");
	}
	else if(HCP >= 11 && HCP <= 15 && C >=5 && D == 0){
		printf("The bidding choice: 2D\n");
	}
	else if(HCP >= 8 && HCP <= 11 && (S == 7 || H == 7 || D == 7 || C == 7)){
		if(C == 7){
			printf("The bidding choice: 3C\n");
		}
		if(D == 7){
			printf("The bidding choice: 3D\n");
		}
		if(H == 7){
			printf("The bidding choice: 3H\n");
		}
		if(S == 7){
			printf("The bidding choice: 3S\n");
		}
	}
	else if(HCP >= 8 && HCP <= 10 && ( S == 6 || H == 6)){
		if(S >= H){
			printf("The bidding choice: 2S\n");
		}
		else{
			printf("The bidding choice: 2H\n");
		}
	}
	else if(HCP <= 16 && ((S == 7 && SAKQ == 3) || (H == 7 && HAKQ == 3))){
		printf("The bidding choice: 3NT\n");
	}
	else{
		printf("The bidding choice: Pass\n");
	}

	return 0;
}

```
:::

Use Function(在這題可以合法使用):
:::spoiler Code (Credit: 41171212H 廖Ｏ晨)
```c=
#include <stdio.h>
#include <stdint.h>

int32_t spadesAKQ = 0, heartsAKQ = 0;

// HCP值
int calculateHCP(int32_t rank) {
    if (rank == 1) return 4; // Aces

    else if (rank == 13) return 3; // Kings

    else if (rank == 12) return 2; // Queens

    else if (rank == 11) return 1; // Jacks
    return 0; // Other cards
}

// st nd rd
void printCardNumber(int32_t cardNumber) {
    printf("%d", cardNumber);
    if (cardNumber == 11 || cardNumber == 12 || cardNumber == 13) {
        printf("th");
    } else {
        if (cardNumber % 10 == 1) printf("st"); 		
        else if (cardNumber % 10 == 2) printf("nd"); 		
        else if (cardNumber % 10 == 3) printf("rd"); 		
        else printf("th");

    }
}

// 輸入輸出
void inputCard(int32_t cardNumber, int32_t hcp, int32_t spadesCount, int32_t heartsCount, int32_t diamondsCount, int32_t clubsCount) {
    int32_t card;
    printCardNumber(cardNumber);
    printf(" card: ");
    scanf("%d", &card);

    // 檢查是否合法
    if (card < 1 || card > 52) {
        printf("Invalid card input. Please enter a number between 1 and 52.\n");
        inputCard(cardNumber, hcp, spadesCount, heartsCount, diamondsCount, clubsCount);
        return;
    }

    // 花色
    int32_t suit = (card - 1) / 13;

    // HCP
    int32_t rank = (card - 1) % 13 + 1;
    hcp += calculateHCP(rank);

    if (suit == 0) spadesCount++;
    else if (suit == 1) heartsCount++;
    else if (suit == 2) diamondsCount++;
    else if (suit == 3) clubsCount++;

    if (card == 1 || card == 12 || card == 13) spadesAKQ++;
    if (card ==14  || card == 25 || card == 26) heartsAKQ++;

    if (cardNumber == 13) {
        printf("---\n");
        printf("HCP: %d pts\n", hcp);
        printf("Suit: %d-%d-%d-%d\n", spadesCount, heartsCount, diamondsCount, clubsCount);

        // 開牌選擇
        if (hcp >= 22 && hcp <= 24 && ((spadesCount == 4 && heartsCount == 3 && diamondsCount == 3 && clubsCount == 3)||(spadesCount == 3 && heartsCount == 4 && diamondsCount == 3 && clubsCount == 3)||(spadesCount == 3 && heartsCount == 3 && diamondsCount == 4 && clubsCount == 3)||(spadesCount == 3 && heartsCount == 3 && diamondsCount == 3 && clubsCount == 4))) printf("The bidding choice : 2NT\n");
        else if (hcp >= 16) printf("The bidding choice : 1C\n");
        else if (15 >= hcp && hcp >= 11 && spadesCount>= 5 && spadesCount > heartsCount) printf("The bidding choice : 1S\n");
        else if (15 >= hcp && hcp >= 11 && heartsCount>= 5 && spadesCount < heartsCount) printf("The bidding choice : 1H\n");
        else if (15 >= hcp && hcp >= 11 && heartsCount>= 5 && spadesCount == heartsCount) printf("The bidding choice : 1S\n");
        else if (hcp >= 13 && hcp <= 15 && ((spadesCount == 4 && heartsCount == 3 && diamondsCount == 3 && clubsCount == 3)||(spadesCount == 3 && heartsCount == 4 && diamondsCount == 3 && clubsCount == 3)||(spadesCount == 3 && heartsCount == 3 && diamondsCount == 4 && clubsCount == 3)||(spadesCount == 3 && heartsCount == 3 && diamondsCount == 3 && clubsCount == 4))) printf("The bidding choice : 1NT\n");
        else if (15 >= hcp && hcp >= 11 && diamondsCount>= 4 ) printf("The bidding choice : 1D\n");
        else if (15 >= hcp && hcp >= 11 && clubsCount>= 6 ) printf("The bidding choice : 2C\n");
        else if (15 >= hcp && hcp >= 11 && clubsCount== 5 && diamondsCount == 0) printf("The bidding choice : 2D\n");
        else if (10 >= hcp && hcp >= 8 && heartsCount== 6 && spadesCount!=6) printf("The bidding choice : 2H\n");
        else if (10 >= hcp && hcp >= 8 && spadesCount==6 && heartsCount!=6) printf("The bidding choice : 2S\n");
        else if (10 >= hcp && hcp >= 8 && heartsCount== 6&&spadesCount==6) printf("The bidding choice : 2S\n");
        else if (11 >= hcp && hcp >= 8 && heartsCount== 7) printf("The bidding choice : 3H\n");
        else if (11 >= hcp && hcp >= 8 && spadesCount== 7) printf("The bidding choice : 3S\n");
        else if (11 >= hcp && hcp >= 8 && diamondsCount== 7) printf("The bidding choice : 3D\n");
        else if (11 >= hcp && hcp >= 8 && clubsCount== 7) printf("The bidding choice : 3C\n");
        else if (16 >= hcp && spadesCount== 7 && spadesAKQ==3) printf("The bidding choice : 2NT\n");
        else if (16 >= hcp && heartsCount== 7 && heartsAKQ==3) printf("The bidding choice : 2NT\n");
        else printf("The bidding choice : Pass\n");

    }
    else {
        inputCard(cardNumber + 1, hcp, spadesCount, heartsCount, diamondsCount, clubsCount);
    }
}

int main() {
    int32_t hcp = 0, spadesCount = 0, heartsCount = 0, diamondsCount = 0, clubsCount = 0;
    inputCard(1, hcp, spadesCount, heartsCount, diamondsCount, clubsCount); 
    return 0;
}
```
:::
## 1.5 How to Write an Email to TA?

:::spoiler Code
```c=
#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

#define READED_INIT_VALUE -1
#define NO_ERROR 0
#define LOCAL_PART_INVALID 1
#define DOMAIN_NOT_AUTHORIZED 2
#define NO_CATEGORY 3
#define EMPTY_TITLE 4
#define OUT_OF_RANGE 5
#define TOO_LOW 6

int main() {
    uint64_t content_score = 0;
    int32_t  A = 0, B = 0, C = 0;
    int32_t  readed = READED_INIT_VALUE;
    int32_t  error = 0;
    int32_t  title_length = 0;
    char     tmp;

    // 允許的域名：
    // ntnu.edu.tw
    // gapps.ntnu.edu.tw
    // csie.ntnu.edu.tw
    // csie.cool
    printf("Please enter the sender address      > ");
    scanf("%*[a-zA-Z0-9]@%n", &readed);
    if (readed == READED_INIT_VALUE) {
        error = error ? error : LOCAL_PART_INVALID;
        scanf("%*[^@]@");
    }

    // 確認 ntnu.edu.tw
    readed = READED_INIT_VALUE;
    scanf("ntnu.edu.tw%n", &readed);

    // 確認 gapps.ntnu.edu.tw
    if (readed == READED_INIT_VALUE) {
        scanf("gapps.ntnu.edu.tw%n", &readed);
    }

    // 確認 csie.ntnu.edu.tw 或 csie.cool
    if (readed == READED_INIT_VALUE) {
        // 先處理共同前綴 csie.
        scanf("csie.%n", &readed);
        if (readed != READED_INIT_VALUE) {
            readed = READED_INIT_VALUE;
            scanf("ntnu.edu.tw%n", &readed);
            if (readed == READED_INIT_VALUE) {
                scanf("cool%n", &readed);
            }
        }
    }

    if (readed == READED_INIT_VALUE) {
        error = error ? error : DOMAIN_NOT_AUTHORIZED;
        scanf("%*[^\n]");
    }

    // 確認是否有多餘的字元
    scanf("%c", &tmp);
    if (tmp != '\n') {
        error = error ? error : DOMAIN_NOT_AUTHORIZED;
    }

    // 允許的主旨：
    // [general] <title>
    // [hw<A>][p<B>] <title>, A 與 B 為小於 10 之正整數
    readed = READED_INIT_VALUE;
    printf("Please enter the email subject       > ");
    scanf("[");
    scanf("hw%d][p%d] %n", &A, &B, &readed);
    if (readed == READED_INIT_VALUE) {
        scanf("general] %n", &readed);
        if (readed == READED_INIT_VALUE) {
            error = error ? error : NO_CATEGORY;
        }
    }
    readed = READED_INIT_VALUE;
    scanf("%*[^\n]%n", &readed);
    if (readed == READED_INIT_VALUE) {
        error = error ? error : EMPTY_TITLE;
    }
    title_length = readed;

    // 允許的內容分數：
    // 非負整數
    printf("Please enter the email content score > ");
    scanf("%" SCNu64, &content_score);
    if (content_score > (uint64_t)1e19) {
        error = error ? error : OUT_OF_RANGE;
    }
    else if (content_score < title_length * (uint64_t)1e10) {
        error = error ? error : TOO_LOW;
    }

    printf("================================================================================\n");
    printf("Sender Address Test : ");
    if (error == LOCAL_PART_INVALID) {
        printf("Failed, local-part is invalid\n");
    }
    else if (error == DOMAIN_NOT_AUTHORIZED) {
        printf("Failed, domain is not authorized\n");
    }
    else {
        printf("Passed\n");
    }

    printf("Email Subject Test  : ");
    if (error && error < NO_CATEGORY) {
        printf("-\n");
    }
    else if (error == NO_CATEGORY) {
        printf("Failed, no category\n");
    }
    else if (error == EMPTY_TITLE) {
        printf("Failed, title is empty\n");
    }
    else {
        printf("Passed\n");
    }

    printf("Email Content Test  : ");
    if (error && error < OUT_OF_RANGE) {
        printf("-\n");
    }
    else if (error == OUT_OF_RANGE) {
        printf("Failed, out of range\n");
    }
    else if (error == TOO_LOW) {
        printf("Failed, too low\n");
    }
    else {
        printf("Passed\n");
    }

    printf("--------------------------------------------------------------------------------\n");
    if (error) {
        printf("Rejected\n");
    }
    else {
        C = (A != 0 && B != 0) ? ((A * B) % 5) : 5;
        switch (C) {
        case 0:
            printf("Assigned to Kaname Madoka\n");
            break;
        case 1:
            printf("Assigned to Akemi Homura\n");
            break;
        case 2:
            printf("Assigned to Miki Sayaka\n");
            break;
        case 3:
            printf("Assigned to Tomoe Mami\n");
            break;
        case 4:
            printf("Assigned to Sakura Kyok\n");
            break;
        default:
            printf("Assigned to QB\n");
            break;
        }
    }

    return error ? EXIT_FAILURE : EXIT_SUCCESS;
}

```
:::

:::spoiler Code (Credit: 41247014S 李Ｏ達)
```c=
#include<stdio.h>
#include<stdint.h>

int main()
{
	int32_t local=0, domain=0, domain_after=0, category1=0, category2=0, hw=0, p=0, title=0, trash=0;
	unsigned long long score=0;
	printf("Please enter the sender address      >");
	scanf("%*[a-zA-Z0-9]@%n", &local);
	scanf("csie.cool%n", &domain);
	scanf("ntnu.edu.tw%n", &domain);
	scanf("csie.ntnu.edu.tw%n", &domain);
	scanf("gapps.ntnu.edu.tw%n", &domain);
	scanf("%*[^\n]%n", &domain_after);
	printf("Please enter the email subject       >");
	scanf(" [%n", &category1);
	if (category1==2)
	{
		scanf("general]%n", &category2);
		scanf("hw%d][p%d]", &hw, &p);
	}
	scanf("%*[^\n]%n", &title);
	scanf("[\n]%n", &trash);
	printf("Please enter the email content score >");
	scanf("%llu", &score);
		printf("================================================================================\n");
	if (local==0)
	{
		printf("\nSender Address Test : Failed, local-part is invalid\n");
		printf("Email Subject Test  : -\n");
		printf("Email Content Test  : -\n");
			printf("--------------------------------------------------------------------------------\n");
		printf("\nRejected\n");
		return 0;
	}
	if (domain==0 || domain_after!=0)
	{
		printf("\nSender Address Test : Failed, domain is not authorized\n");
		printf("Email Subject Test  : -\n");
		printf("Email Content Test  : -\n");
			printf("--------------------------------------------------------------------------------\n");
		printf("\nRejected\n");
		return 0;
	}
	if (category1!=2 || category2!=8 && ((hw!=1 && hw!=2 && hw!=3 && hw!=4 && hw!=5 && hw!=6 && hw!=7 && hw!=8 && hw!=9) || (p!=1 && p!=2 && p!=3 && p!=4 && p!=5 && p!=6 && p!=7 && p!=8 && p!=9)))
	{
		printf("\nSender Address Test : Passed\n");
		printf("Email Subject Test  : Failed, no category\n");
		printf("Email Content Test  : -\n");
			printf("--------------------------------------------------------------------------------\n");
		printf("\nRejected\n");
		return 0;
	}
	if (title==0)
	{
		printf("\nSender Address Test : Passed\n");
		printf("Email Subject Test  : Failed, title is empty\n");
		printf("Email Content Test  : -\n");
			printf("--------------------------------------------------------------------------------\n");
		printf("\nRejected\n");
		return 0;
	}
	if (score>10000000000000000000LLU)
	{
		printf("\nSender Address Test : Passed\n");
		printf("Email Subject Test  : Passed\n");
		printf("Email Content Test  : Failed, out of range\n");
			printf("--------------------------------------------------------------------------------\n");
		printf("\nRejected\n");
		return 0;
	}
	if (score<title*10000000000)
	{
		printf("\nSender Address Test : Passed\n");
		printf("Email Subject Test  : Passed\n");
		printf("Email Content Test  : Failed, too low\n");
			printf("--------------------------------------------------------------------------------\n");
		printf("\nRejected\n");
		return 0;
	}
	printf("\nSender Address Test : Passed\n");
	printf("Email Subject Test  : Passed\n");
	printf("Email Content Test  : Passed\n");
		printf("--------------------------------------------------------------------------------\n");
	if (category2==8)
		printf("\nAssigned to TA QB\n");
	else if (((hw*p)%5)==0)
		printf("\nAssigned to Kaname Madoka\n");
	else if (((hw*p)%5)==1)
		printf("\nAssigned to Akemi Homura\n");
	else if (((hw*p)%5)==2)
		printf("\nAssigned to Miki Sayaka\n");
	else if (((hw*p)%5)==3)
		printf("\nAssigned to Tomoe Mami\n");
	else if (((hw*p)%5)==4)
		printf("\nAssigned to Sakura Kyoko\n");

	return 0;
}

```
:::


## 1.6 Bonus: Makefile for Multiple files
```make=
-gcc a.c -o a
gcc b.c -o b
```