---
# System prepended metadata

title: '==C 語言==解題研究社-張峻瑞'

---

1. 
# ==C 語言==解題研究社-張峻瑞
## [1加到n計算式](https://onlinegdb.com/2EIMiSipg)
:::warning
```
#include <stdio.h>

int main()
{
    int i , sum = 0, n;
    printf("請輸入數字");
    scanf("%d",&n);
    for ( i = 0 ; i <= n ;  i++){
        sum = sum + i;
    }
    printf("%d", sum);
}
```
:::
## [公因數與公倍數(非輾轉)](https://onlinegdb.com/Ol4CUgXkR)
:::warning

```
#include <stdio.h>

int main()
{
    int num1 = 34, num2 = 10,num2_old,LCM;//num2_old:沒有改變的num2
    num2_old = num2;
    while ( num2 != 1 )//num2會一直減少到1
    {
        if (num1 % num2 == 0 & num2_old % num2 == 0){//num1被num2整除或原始的num2_old被num2整除代表此時的num2為最大公因數
            printf("%d\n",num2);
            LCM = (num1*num2_old)/num2;
            printf("%d",LCM);//最大公因數為LCM
            break;
        }
        num2 = num2 -1;
    }
}
```
:::
## [閏年與平年](https://onlinegdb.com/oCUWg-lVj)
:::warning
```
#include <stdio.h>//閏年的規則,每四年閏一次,但是每一百年不閏,遇到四百年還是要閏年

int main()
{
    int year;
    printf("請輸入任一年份的數字");
    scanf("%d",&year);
    
    if ( year % 400 == 0 ){ //除以400的餘數為0,則為閏年
        printf ("閏年");
    }
    else if ( year % 400 == 0 ){ //在除以100的餘數為0,則為平年
        printf ("平年");
    }
    else if ( year % 4 == 0 ){ //在除以4的餘數為0,則為閏年
        printf ("閏年");
    }
    else {
        printf("平年");//餘為平年
    }
}
```
:::
## [輾轉相除法](https://onlinegdb.com/2PqswJT2J)
:::warning
```
#include <stdio.h>

int main()
{
    int num1 ,  num2 , remainder,product,LCM;
    printf("請輸入兩個個數字\n");
    scanf("%d\n%d",&num1,&num2);//輸入兩個數字以計算
    product = num1 *num2;// num1乘num2= product
    while ( num2 != 0 ){//重複執行到( num2 不等於 0 )
        remainder = num1 % num2;
       num1 = num2;
       num2 = remainder;
    }
    printf("最大公因數%d\n",num1);
    LCM = product /num1;
    printf("最小公倍數%d\n",LCM);
    return 0;
}

```
:::
## [質合數判別](https://onlinegdb.com/ge_xTL7w7e)
:::warning
```
#include <stdio.h>

int main()
{
    int n, i = 2, is_prime = 1;
    scanf("%d" , &n);//提問用
    while ( i < n) {
        if (n % i == 0 ){
            is_prime = 0;
            printf("%d" , i );
        }
        i++;
    }
    if (is_prime == 0){
    printf("是合數");//顯示("是合數")
    }else
    printf("是質數");//顯示("是質數")
    }
    
```
:::
## [while迴圈](https://onlinegdb.com/yIGwlntHbx)
:::warning
```
#include <stdio.h>

int main()
{
    int i = 0, sum = 0;
    while (i <= 10 ){//重複以下動作
        sum = sum + i; 
        i++;
    }
    printf("%d\n" , sum );
}
```
:::
## [兩光算命](https://onlinegdb.com/rzczwW2Nz)
:::warning
```
#include <stdio.h>

int main()
{
    int m = 1 , d =5 ;
    int sum = ( m * 2 + d)%3;//sum = ( m * 2 + d)%3
    if (sum == 0)//假如sum = 0
    {printf("普通");}//顯示普通
    if (sum == 1)
    {printf("吉");}//顯示吉
    if (sum == 2)
    {printf("大吉");}//顯示大吉
}

```
:::
## [一加到十](https://onlinegdb.com/JVd-ADWAz)
:::warning
```

#include <stdio.h>

int main()
{
    int i , sum = 0, n;
    printf("請輸入數字");
    scanf("%d",&n);
    for ( i = 0 ; i <= n ;  i++){
        sum = sum + i;
    }
    printf("%d", sum);
}
```
:::
## [等差數列](https://onlinegdb.com/RceYLB0Ni)
:::warning
```

#include <stdio.h>

int main()
{
    int a [100];
    printf("等差數列的首項是多少");
    scanf("%d" , &a[0]);
    a [0] = 2 ;
    int d = 2 , i , n=4 , sum ;
    printf("等差數列的公差是多少");
    scanf("%d",&d);
    printf("要查第幾項");
    scanf ("%d",&n);
    for (i = 0 ; i < n ; i++){
        a[i+1] = a[i] + d ;
    }
    printf("數列的第%d項是:%d\n", n,a[n-1]);
    for (i = 0 ; i < n; i++){
        sum = sum +a[i];
    }
    printf("數列的第1項加到第%d項的和是%d",n,sum);
}
```
:::
## [泡沫排序法](https://onlinegdb.com/0VsropbSZ)
:::warning
```
#include <stdio.h>

int main()
{
    int n[9] = { 9, 8, 7, 6, 5, 4, 3, 2, 1 };
    int i , j , temp;
    for( j = 0 ; j < 9-1 ; j++){
        for ( i = 0; i < 9-1;i++){
            if ( n[i] > n [i+1]){
                temp = n[i];
                n[i] = n[i+1];
                n[i+1] = temp;
            }
        }
        
    }
    for( i = 0 ; i < )
    printf("%d",n[]);
    
}
```
:::
## [猜數字](https://onlinegdb.com/_nQxnNvyu)
:::warning
```
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
    int answer ;
     
    int max = 100 ,smallest = 1 ,frequency = 0,correctanswer = 50;
    do {
        scanf("%d" ,&answer);
        if( answer % 1 == 0 , max > answer , smallest < answer){
           if (correctanswer < answer){
            printf("太大了\n");
            max=answer;
            }
        
            else if (correctanswer > answer){
            printf("太小了\n");
            smallest=answer;
            }
    
            else if (correctanswer == answer){
            printf("答對了\n");
            }
            else {printf("輸入錯誤,請繼續輸入");
            frequency + 1 ;
            }
            printf ("%d",smallest);
            printf ("-");
            printf ("%d\n",max);
        }
    } while (answer != correctanswer);
   
}
```
:::
## [計算正方形面積](https://onlinegdb.com/njRFRxn94)
:::warning
```

#include <stdio.h>
int square(int x);
int main()
{
    int len ;
square(4);
    printf ("%d",square(len));
}
int square(int x){
    return x * x ;
}


```
## [1A2B](https://onlinegdb.com/qU8ZfuKcU)
:::warning
```
#include <stdio.h>

int main()
{
    int answer = 456, input  , a, b, c, x, y, z, A = 0, B = 0;
    printf("請輸入數字\n");
    while(scanf("%i",&input) != EOF){
    a = answer / 100; b = answer / 10 % 10; c = answer % 10; 
    x = input  / 100; y = input  / 10 % 10; z = input  % 10;
    if(a == x && b == y && c == z){
        printf("猜對了!");
        break;
    }
    if(z == c)
        A++;
    if(y == b)
        A++;
    if(x == a)
        A++;
    if(z == b)
        B++;
    if(z == a)
        B++;
    if(y == c)
        B++;
    if(y == a)
        B++;
    if(x == c)
        B++;
    if(x == b)
        B++;
    printf("%iA%iB\n",A,B);
        
}
}
```
:::
## [APCS(1)](https://onlinegdb.com/Fkq9dRhD0)
:::warning
```
#include <stdio.h>

int f(int n){
    if (n<2){
        return n;
    }
    else{
        return(n+f(n/2));
}
}

int main()
{
printf f(10);
}

```
:::
## [APCS(2)](https://onlinegdb.com/EzYuChQiQ)
:::warning
```
#include <stdio.h>
void f(int x, int y){//void = 無回傳值
    int tem = x;
    x = y;
    y = tem;
}
int main(){
    int x = 2, y = 3;
    f(x,y);
    printf("%d",(x-y)*(x+y)/2);//%d=整數
    return 0;
}
```
:::
## [APCS(3)](https://onlinegdb.com/Xy2eIbrFw)
:::warning
```

#include <stdio.h>
int main()
{
int k = 0;
int m = 9;
for (int i=1;i<=5; i=i+1){
    for (int j=1; j <= k; j = j+1){
            printf (" ");
        }
        for(int j=1; j<=m; j = j+1){
            printf ("*");
        }
    printf("\n");
    k = k + 2;
    m = m - 2;
}
}
```
:::
## [APCS(4)](https://onlinegdb.com/bSrthoaQQ)
:::warning
```

#include <stdio.h>

int main()
{
    int i ,a[100],b[100];
    for (i = 1; i <=100;i=i+1){
        b[i] = i;
    }
    a[1]= 0;
    for (i= 1; i<=100;i=i+1){
        a[i] = b[i]+a[i-1];
    }
printf("%i\n",a[50]-a[30]);
}

```
:::
## [APCS(5)](https://onlinegdb.com/u8i9OzWxr)
:::warning
```

#include <stdio.h>

int f (int n) {
    int sum= 0 ;
    if (n<2) {
        return 0 ;
    }
    for (int i=1; i<=n;i=i+1) {
        sum = sum + i ;
    }
    sum = sum + f(2*n/3);
    return sum;
    }

int main(){
    printf("%i",f(666));
    return 0 ;
}

```
:::
## [APCS(6)](https://onlinegdb.com/yyjONL_gK)
:::warning
```

#include <stdio.h>

    int a (int n , int m){
        if (n < 10) {
            if(m<10) {
                return n + m; 
            }
            else {
                return a(n,m  - 2)+ m;
            }
        }
        else{
            return a(n - 1,m) + n ;
        }
    }
int main(){
    printf("%i",a(13,15));
    return 0;
}
```
:::
## [APCS(7)](https://onlinegdb.com/S9nqF5lMM)
:::warning
```

#include <stdio.h>
int f(int i);
int main(){
    printf("%d",f(10));
    return 0;
}

int f(int i){
    if (i>0)
    if((i/2)%2==0)
    return f(i-2)*i;
    else
    return f (i-2)*(-i);
    else
    return 1;
}
```
:::
## [APCS(8)](https://onlinegdb.com/nc_5Okx1B)
:::warning
```
#include <stdio.h>

void gcd(int num1, int num2) {
    if (num2 == 0)
        printf("%d" ,num1);
    else
        return gcd (num2,num1 % num2);
}
int main()
{
    int num1 = 6;
    int num2 = 30;

    gcd(num1,num2);
    return 0;
}
```
:::
## [APCS(9)](https://onlinegdb.com/nc_5Okx1B)
:::warning
```
#include <stdio.h>

int f() {
    int p = 2 ;
    while (p <2000) {
        p = 2 * p;
    }
    return p ;
}
int main()
{
    printf("%d",f());
    return 0;
}
}
```
:::
## [河內塔](https://onlinegdb.com/FW3F2obPM)
:::warning
```
#include <stdio.h>
void hanoi(int n,int A, int B, int C){
    if ( n == 1 ){
        printf("%d -> %d\n",A,C);
    }
    else{
        hanoi(n-1,A,C,B);
        printf("%d -> %d\n",A,C);
        hanoi(n-1,B,A,C);
    }
}
int main()
{
    int n ;
    int A =1;
    int B =2;
    int C =3;
    printf("請輸入要解的盤數");
    scanf("%d", &n);
    hanoi(n, A, B, C);
}
```
:::
## [河內塔(英)](https://onlinegdb.com/uVbCmDkjC)
:::warning
```

#include <stdio.h>
void hanoi(int n , char A, char B, char C){
    if ( n == 1 ){
        printf("%c -> %c\n",A,C);
    }
    else{
        hanoi(n-1,A,C,B);
        printf("%c -> %c\n",A,C);
        hanoi(n-1,B,A,C);
    }
}
int main()
{
    int n ;
    char A ='A';
    char B ='B';
    char C ='C';
    printf("請輸入要解的盤數");
    scanf("%d", &n);
    hanoi(n, A, B, C);
}
```
:::
## [畫星星](https://onlinegdb.com/PeI6zdZ9y)
:::warning
```

#include <stdio.h>

int main()
{
    int i , j ,k ,d = 0 , e =9;
    for(k = 0 ; k<5 ;k++){
    for(i = 0 ; i<d ;i++){
        printf (" ");
    } 
    for (j = 0; j<e;j++ ){
        printf ("*");
    }
    printf("\n");
    d = d + 1 ;                                                                                                                          
    e = e - 2 ;                                                    
    }                                                    
}                                                    
```                                                    
:::
## [找零機器](https://onlinegdb.com/OEN0mjbA6)
:::warning
```                                                    
#include <stdio.h>                                                    
                                                    
int main()                                                    
{                                                    
    int cash , cash100 , cash50 , cash10 , cash5, cash1 ;
    while(1){                                                    
    scanf("%d" , &cash );                                                    
    while (cash >= 100){                                                    
        cash = cash -100;                                                    
        cash100 ++;                                                    
    }                                                    
    while (cash >= 50){                                                    
        cash = cash -50;                                                    
        cash50 ++;                                                    
    }                                                    
    while (cash >= 10){                                                    
        cash = cash -10;                                                    
        cash10 ++;                                                    
    }                                                    
    while (cash >= 5){                                                    
        cash = cash -5;                                                    
        cash5 ++;                                                    
    }                                                    
    while (cash >= 1){                                                    
        cash = cash -1;                                                    
        cash1 ++;                                                    
    }
    printf("100元%d個\n50元%d個\n10元%d個\n5元%d個\n1元%d個\n",cash100 , cash50 ,cash10 , cash5 , cash1 );
    }                                                    
                                                        
}                                                    
```                                                    
:::
## [亂數](https://onlinegdb.com/llJ39Y4Uw)
:::warning
```                                                    
                                                    
#include <stdio.h>
#include <stdlib.h>/*亂數相關函數*/
#include <time.h>/*時間相關函數*/
int main()                                                    
{                                                               
    // 設定亂數種子                                                    
    srand(time(NULL));                                                    
    // 產生亂數                                                    
    int x = rand();                                                    
    printf ("x = %d\n " , x);                                                    
    return 0 ;                                                    
}                                                     
                                                                            
/*int main()                                                    
{                                                    
    //固定亂數種子                                                    
    srand(5);                                                    
    int x = rand();
    printf ("x = %d\n " , x);
    return 0 ;                                                    
}*/                                                    
/*int main()                                                    
{
    srand(time(NULL));
    int min = 4 ;
    int max = 10 ;
    
    int x = rand ()% (max - min + 1) + min ;
    printf ("x = %d\n" , x);                                                 
    return 0 ;
}*/
```
:::
:::warning
# ==心得==
## ++上完了C語言社團的課程,我覺得這真的非常非常的有趣:>真的非常推薦大家加入:>好東西要分享給大家:>完完全全不會無聊喔:>學弟妹要加入喔:>
## 開個玩笑,說真的...一開始我覺得這社團有些無聊,但自從慢慢地了解C語言的運作方式後,發現可以利用這些普普的小字母組成許多有趣又實用的程式...老實說參加這個社團真的很需要耐心去學習,許多程式都需要長時間的講解才能明白,不過假如你對程式有興趣又有耐心的話非常推薦這個社團喔OvOb                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  \                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   