C 語言簡單演算法回顧總結

ZY_FlyWay發表於2018-01-08
//
//  main.c
//  EsayAlgorithm
//
//  Created by apple on 2018/1/8.
//  Copyright © 2018年 ZY. All rights reserved.
//

#include <stdio.h>

void prime(int n);
void  call_bubble(int a[]);
int *  bubble_sort(int * a,int n);
void Fibonacci(int n);
void yanghuiSingle(int n);

int main(int argc, const char * argv[]) {
    
    prime(16);
    
    int a[5] = {4,2,40,22,3};
    call_bubble(a);
    
    Fibonacci(5);
    
    yanghuiSingle(6);
    
    return 0;
    
}


//素數的判斷
void prime(int n)
{
    double d = (double)n;
    for (int i=2; i<=sqrt(d); i++) {
        if (n%i==0) {
            printf("%d不是素數\n",n);
            return;
        }else{
            printf("%d是素數\n",n);
        }
    }
    
}

#pragma mark  排序

//氣泡排序呼叫

void  call_bubble(int a[]){
    int * p;
    p = a;
    p=bubble_sort(p, 5);
    for (int i=0; i<5; i++) {
        printf("%d",*(p+i));
    }
    printf("\n");
}


//氣泡排序
int *  bubble_sort(int * a,int n){
    
    for (int i=0; i<n; i++) {
        for (int j=i+1; j<n; j++) {
            if (a[i]>a[j]) {
                a[i] = a[i]^a[j];
                a[j] = a[j]^a[i];
                a[i] = a[i]^a[j];
            }
        }
    }
    
    return a;
}



//斐波那契數列   兔子生小兔子問題

void Fibonacci(int n){
    long  f1,f2;
    f1=f2=1;
    for (int i=0; i<n; i++) {
        printf("%ld,%ld\n",f1,f2);
        f1 = f1 + f2;
        f2 = f1 + f2;
    }
    
}


//楊輝三角
void yanghuiSingle(int n){
    
    int a[n][n];
    
    for (int i=0; i<n; i++) {
        for (int j=0; j<=i; j++) {
            if (j==0||j==i) {
                a[i][j]=1;
            }else{
                a[i][j] = a[i-1][j-1]+a[i-1][j];
            }
        }
    }
    
    for (int i=0; i<n; i++) {
        
        for (int j=0;j<2*n-i;j++) {
            printf(" ");
        }
        for (int j=0; j<i+1; j++) {
            printf("%2d",a[i][j]);
        }

        printf("\n");
    }
}


列印結果:

16不是素數

2342240

1,1

2,3

5,8

13,21

34,55

             1

            1 1

           1 2 1

          1 3 3 1

         1 4 6 4 1

        1 51010 5 1

Program ended with exit code: 0


原始碼地址:https://github.com/RainManGO/EsayAlgorithm

相關文章