zoukankan      html  css  js  c++  java
  • 算法01 C语言设计

    8.21

    #include <stdio.h>
    
    void bubbleSort(int **p, int n);
    
    int main(void){
    
    	int a[100];
    	int *b[100];
    	int n, i, **p;
    
    	printf("Input your n(n < 100)
    ");
    	scanf("%d", &n);
    	printf("Input your a[n]
    ");
    	for(i = 0; i < n; i++)
    		scanf("%d", a + i);
    	for(i = 0; i < n; i++)
    		b[i] = a + i;
    	p = b;
    	bubbleSort(p, n);
    	for(i = 0; i < n; i++)
    		printf("%d ", *b[i]);
    	printf("
    ");
    	return 0;
    }
    
    void bubbleSort(int **p, int n){
    	int i, j, flag = 1;
    	int *temp;
    	for(i = 0; flag; i++){
    		flag = 0;
    		for(j = n - 1; j > i; j--){
    			flag = 1;
    			if(**(p + i) > **(p + j)){
    				temp = *(p + i);
    				*(p + i) = *(p + j);
    				*(p + j) = temp;
    			}
    		}
    
    
    	}
    }
    

     8.20

    #include <stdio.h>
    #include <string.h>
    
    void selectionSort(char **p);
    
    int main(void)
    {
    	char a[5][30], *b[5], **p;
    	int i;
    
    	for(i = 0; i < 5; i++)
    		b[i] = a[i];
    	for(i = 0; i < 5; i++)
    		scanf("%s", b[i]);
    	p = b;
    	selectionSort(p);
    	printf("
    ");
    	for(i = 0; i < 5; i++)
    		printf("%s
    ", b[i]);
    	return 0;
    }
    
    void selectionSort(char **p)
    {
    	int i, j, minj, n = 5;
    	char *temp;
    	
    	for(i = 0; i < n - 1; i++)
    	{
    		minj = i;
    		for(j = i; j < n; j++)
    		{
    			if(strcmp(*(p + j), *(p + minj)) < 0)
    				minj = j;
    		}
    		temp = *(p + i);
    		*(p + i) = *(p + minj);
    		*(p + minj) = temp;
    	}
    
    }
    

     8.16

    #include <stdio.h>
    
    int getnum(char str[], int a[]);
    
    int main(void)
    {
        char str[100] = {}, *pstr;
        int a[100] = {}, total, *p, i;
        p = a;
        pstr = str;
        printf("Input your string
    ");
        gets(pstr);
        total = getnum(str, a);
        for(i = 0; a[i] != ''; i++)
            printf("%d ", a[i]);
        printf("total = %d
    ", total);
        return 0;
    }
    
    int getnum(char str[], int a[])
    {
        int count = 0, temp = 0;
        int i = 0, j = 0, k = 0, m = 0, e10;
        char *p;
        int *pa;
        p = str;
        pa = a;
        while(*(p + i) != '')
        {
            if((*(p + i) >= '0') && (*(p + i) <= '9'))
            {
                j++;
            }
            else
            {
                if(j > 0)
                {
                    temp = *(p + i -1) - '0';
                    k = 1;
                    while(k < j)
                    {
                        e10 = 1;
                        for(m = 1; m <= k; m++)
                            e10 = e10 * 10;
                        temp = temp + (*(p + i - 1 - k) - '0') * e10;
                        k++;
                        printf("2th while e10 temp =  %d
    ", temp);
                    } //while
                    *pa = temp;
                    printf("pa++ = %d
    ", *pa);
                    pa++;
                    j = 0;
                    count++;
                } //if
            } //else
            i++;
        } // while
        
        if(j > 0)
        {
            temp = *(p + i -1) - '0';
            k = 1;
            while(k < j)
            {
                e10 = 1;
                for(m = 1; m <= k; m++)
                    e10 = e10 * 10;
                temp = temp + (*(p + i - 1 - k) - '0') * e10;
                k++;
                printf("2th while e10 temp =  %d
    ", temp);
            } //while
            *pa = temp;
            printf("pa++ = %d
    ", *pa);
            j = 0;
            pa++;
            count++;
        } //if
        *pa = '';
        return count;
    }
  • 相关阅读:
    windows_MySQL安装详解
    nginx 基本安全优化
    pip._vendor.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out.
    浏览器缓存
    JavaScript迭代
    js模块开发
    关于逻辑删除标识字段value的设定
    c#单例(Singleton)模式实现
    css兼容小问题
    IIS网站不能访问
  • 原文地址:https://www.cnblogs.com/paprikatree/p/10425655.html
Copyright © 2011-2022 走看看