DAY 5

作业:

#include 
#include 
#include 

void Demo01(){
    //    杨辉三角
    //       1
    //      1 1
    //     1 2 1
    //    1 3 3 1
    //   1 4 6 4 1
    // 1 5 10 10 5 1
    int line = 0;
    printf("Please set the line of Pascal's triangle:");
    scanf("%d", &line);

    int tri[line][line];
    for(int m = 0; m < line; m++){
        for(int count = line-m; count > 0; count--){
            putchar(' ');
        }
        for(int n = 0; n < m+1; n++){
            if(n == 0 || n == m){
                tri[m][n] = 1;
            } else {
                tri[m][n] = tri[m-1][n-1] + tri[m-1][n];
            }
            printf("%d ", tri[m][n]);
        }
        printf("\n");
    }

}

void Demo02(){
    int len = 1;
    printf("Please set the length of array: ");
    scanf("%d", &len);
    float arr[len];

    for(int i = 0; i < len; i++){
        printf("Please give arr[%d]'s float value: ", i);
        scanf("%f", &arr[i]);
    }

    printf("Swich your wanted sort: \n");
    printf("input 1 means 冒泡升序排序;\n");
    printf("input 2 means 简单选择降序排序;\n");
    printf("input other will end with error.\n");
    int mode = 0;
    printf("now give your wanted mode: ");
    scanf("%d", &mode);

    switch(mode){
        case 1 : 
            for(int i = 0; i < len-1; i++){
            	int count = 0;
                for(int j = 0; j < len-1-i; j++){
                    if(arr[j] > arr[j+1]){
                        arr[j+1] = arr[j] + arr[j+1];
                        arr[j] = arr[j+1] - arr[j];
                        arr[j+1] = arr[j+1] - arr[j];
                        count++;
                    }
                }
                if(count == 0)
                	break;
            }
            printf("The result of MaoPao is: \n");
            for(int i = 0; i < len; i++){
                printf("arr[%d]=%.2f\n", i, arr[i]);
            }
            printf("\n");
            break;
        case 2 :
            // min
            for(int i = 0; i < len; i++){
                int min = i;
                for(int j = i; j < len; j++){
                    if(arr[min] > arr[j])
                        min = j;
                }
                if(min != i){
                    arr[i] = arr[i] + arr[min];
                    arr[min] = arr[i] - arr[min];
                    arr[i] = arr[i] - arr[min];
                }
            }
            printf("The result of JianDanXuanZe is: \n");
            for(int i = 0; i < len; i++){
                printf("arr[%d]=%.2f\n", i, arr[i]);
            }
            printf("\n");
            break;
        default : printf("your input mode num is wrong!\n"); break;
    }
}

int main(int argc, const char *argv[]){
	Demo01();
    Demo02();
    Demo02();
	return 0;
}

DAY 5_第1张图片

 

你可能感兴趣的:(算法,c语言)