美文网首页C语言学习杭电oj
第九章-指针:C语言世界中所向披靡的“金箍棒”

第九章-指针:C语言世界中所向披靡的“金箍棒”

作者: 小小Henry | 来源:发表于2019-10-16 21:37 被阅读0次

    练兵区:

    1、二分法求根

    #include <stdio.h>
    #include <math.h>
    
    void GetValue(float* value, float left, float right);
    
    int main()
    {
        float left;//左区间
        float right;//右区间
        float mid;
    
        scanf("%f,%f", &left, &right);
        GetValue(&mid, left, right);
        printf("x=%6.2f\n", mid);
        return 0;
    }
    
    void GetValue(float* value, float left, float right)
    {
        float temp;
        do
        {
            *value = (left + right) / 2;
            temp = pow(*value, 3) - *value - 1;
            if (temp < 0)
            {
                left = *value;
            }
            else
                right = *value;
    
        } while (fabs(temp) > 1e-6);
    }
    

    2、 矩阵转置

    #include <stdio.h>
    
    #define N 5
    
    void GetArray(int a[][N], int n);
    
    int main()
    {
        int n;
        int array[10][5] = {0};
        scanf("%d", &n);
        GetArray(array, n);
        printf("The original matrix is:\n");
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                printf("%3d", array[i][j]);
            }
            printf("\n");
        }
        printf("The changed matrix is:\n");
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                printf("%3d", array[i+5][j]);
            }
            printf("\n");
        }
    }
    
    void GetArray(int a[][N],int n)
    {
        int i, j;
        int temp;
        for (i = 0; i < n; i++)
            for (j = 0; j<n; j++)
            {
                a[i][j] = i * n + j + 1;
                a[j + 5][i] = a[i][j];
            }
    
    }
    

    3、程序改错

    #include  <stdio.h>
    #define ARR_SIZE 10
    
    void MaxMinExchang(int *a, int n);
    
    int main()
    {
        int a[ARR_SIZE], i, n;
        printf("Input n(n<=10):\n");
        scanf("%d", &n);
        printf("Input %d Numbers:\n", n);
        for (i = 0; i < n; i++)
        {
            scanf("%d", &a[i]);
        }
        MaxMinExchang(a, n);
        printf("After MaxMinExchange:\n");
        for (i = 0; i < n; i++)
        {
            printf("%d ", a[i]);
        }
        printf("\n");
        return 0;
    }
    void  MaxMinExchang(int *a, int n)
    {
        int  maxValue = a[0], minValue = a[0], maxPos ,minPos ;
        int  i, temp;
        maxPos = minPos = 0;
        for (i = 0; i < n; i++)
        {
            if (a[i] > maxValue)
            {
                maxValue = a[i];
                maxPos = i;
            }
            if (a[i] < minValue)
            {
                minValue = a[i];
                minPos = i;
            }
        }
        temp = a[maxPos];
        a[maxPos] = a[minPos];
        a[minPos] = temp;
    }
    

    4蛇形矩阵

    这一题你会发现行列之后为偶数,走向为右上;行列之后为奇数,走向为左下;然后碰到边界会变直线,于是考虑一下边界情况题目就很简单了。

    # include <stdio.h>
    
    #define N 100
    
    void ZigzagMatrix(int a[][N], int n);
    
    int main()
    {
        int n;
        int a[N][N] = { 0 };
        printf("Input n:\n");
        if (scanf("%d", &n) != 1)
            printf("Input error!\n");
        else
        {
            if (n<=0 || n>=100)
                printf("Input error!\n");
            else
            {
                ZigzagMatrix(a, n);
                for (int i = 0; i < n; i++)
                {
                    for (int j = 0; j < n; j++)
                        printf("%4d", a[i][j]);
                    printf("\n");
                }
            }
        }
            
    
        return 0;
    }
    
    void ZigzagMatrix(int a[][N], int n)
    {
        int counter = 1;
        int i = 0, j = 0;
        while (1)
        {
            a[i][j] = counter;
            counter++;
            //i+j都为偶数,移动方向为右上,遇到边界有一个条件不变化
            if ((i+j)%2 == 0)
            {
                if (i == n - 1 && j == n - 1)
                    break;
                else if (i == 0 && j != n - 1)
                {
                    j++;
                }
                else if (j == n - 1)
                    i++;
                else
                {
                    i--; j++;
                }
            }
            //i+j为奇数,移动方向为左下,同样受限于边界
            else
            {
                if (j == 0 && i != n - 1)
                    i++;
                else if (i == n - 1)
                    j++;
                else
                {
                    i++; j--;
                }
            }
        }
    }
    

    5 亲密数_1

    ···

    include <stdio.h>

    include <assert.h>

    void GetDivisors(int *tempa, int a);

    int main()
    {
    int a, b;
    int tempa, tempb;
    printf("Input m, n:\n");
    if (scanf("%d,%d", &a, &b) != 2)
    assert(1);
    else
    {
    GetDivisors(&tempa, a);
    GetDivisors(&tempb, b);
    if (tempa == b && tempb == a)
    printf("Yes!\n");
    else
    printf("No!\n");
    }

    return 0;
    

    }

    void GetDivisors(int* tempa, int a)
    {
    int small, large;
    int i, j;
    *tempa = 1;
    for (small = 2, large = a; small < large; small++)
    {
    if (a % small == 0)
    {
    large = a / small;
    *tempa += small + large;
    }
    }
    if (a / small == small)
    *tempa += small;

    }
    ···

    6 亲密数_2

    # include <stdio.h>
    #include <assert.h>
    
    void GetDivisors(int *tempa, int a);
    
    int main()
    {
        int n;
        int i, j, tempi ,tempj;
        int a[100];
        int count = 0;
        printf("Input n:\n");
        scanf("%d", &n);
        for (i = 2; i <= n; i++)
        {
            GetDivisors(&tempi, i);
            GetDivisors(&tempj, tempi);
            if (tempj == i && tempi != i)
            {
                for (j = 0; j < count; j++)
                    if (a[j] == i)
                        goto A;
                a[count] = i;
                a[count + 1] = tempi;
                printf("(%d,%d)\n", a[count], a[count + 1]);
                count += 2;
    
            A:;
            }
        }
    
        return 0;
    }
    
    void GetDivisors(int* tempa, int a)
    {
        int small, large;
        int i, j;
        *tempa = 1;
        for (small = 2, large = a; small < large; small++)
        {
            if (a % small == 0)
            {
                large = a / small;
                *tempa += small + large;
            }
        }
        if (a / small == small)
            *tempa += small;
    
    }
    

    7 完全数

    ···

    include <stdio.h>

    include <assert.h>

    int GetDivisors(int m, int a[]);

    int main()
    {
    int m;
    int i, j;
    int a[100];
    int count;
    printf("Input m:\n");
    scanf("%d", &m);
    if (count = GetDivisors(m, a))
    {
    printf("Yes!\n");
    for (i = 0; i < count; i++)
    {
    if (i == count-1)
    printf("%d", a[i]);
    else
    printf("%d,", a[i]);

        }
            
    }
    else
        printf("No!\n");
    
    return 0;
    

    }

    int GetDivisors(int m, int a[])
    {
    int i, j;
    int count = 0, sum = 0;
    for (i=1; i<m; i++)
    {
    if (m % i == 0)
    {
    a[count] = i;
    count++;
    sum += i;
    }
    }
    if (m == sum)
    return count;
    else
    return 0;
    }
    ···

    8 回文素数

    ···

    include <stdio.h>

    include <math.h>

    int IsPrime(int n);
    int IsPalindromic(int n);

    int main()
    {
    int n;
    int i;
    int count = 0;
    printf("Input n:\n");
    scanf("%d", &n);
    for (i = 10; i <= n; i++)
    {
    if (IsPrime(i) && IsPalindromic(i))
    {
    printf("%4d", i);
    count++;
    }

    }
    printf("\ncount=%d\n", count);
    
    return 0;
    

    }

    int IsPalindromic(int n)
    {
    if (n/100==n%10 || n / 10 == n % 10)
    return 1;
    else
    return 0;
    }

    int IsPrime(int n)
    {
    int i;
    for (i = 2; i <= sqrt(n); i++)
    {
    if (n % i == 0)
    return 0;
    }
    return 1;
    }
    ···

    9 梅森尼数

    ···

    include <stdio.h>

    include <math.h>

    int IsPrime(double n);

    int main()
    {
    int n;
    int i, count=0;
    double temp=1;
    printf("Input n:\n");
    scanf("%d", &n);
    for (i = 1; i < n; i++)
    {
    temp *= 2;
    if (IsPrime(temp - 1))
    {
    printf("2^%d-1=%.0lf\n", i, temp - 1);
    count++;
    }
    }
    printf("count=%d\n", count);
    return 0;
    }

    int IsPrime(double n)
    {
    double i;
    if (n == 1)
    return 0;
    for (i = 2; i <= sqrt(n); i++)
    {
    if (n / i == (int)(n/i))
    return 0;
    }
    return 1;
    }
    ···

    10工资统计

    ···

    include<stdio.h>

    void Input(float wage[], int n);

    float Compute(float wage[], int n, float* pmaxwage, float* pminwage);

    int main()

    {

    float wage[50], maxwage, minwage, avewage;
    
    int n;
    
    
    
    printf("Please input n:\n");
    
    scanf("%d", &n);
    
    
    
    Input(wage, n);
    
    avewage = Compute(wage, n, &maxwage, &minwage);
    
    
    
    printf("maxwage=%.2f, minwage=%.2f, avewage=%.2f\n", maxwage, minwage, avewage);
    
    
    
    return 0;
    

    }

    void Input(float wage[], int n)

    {
    for (int i = 0; i < n; i++)
    {
    scanf("%f", &wage[i]);
    }
    }

    float Compute(float wage[], int n, float* pmaxwage, float* pminwage)

    {
    float averwage=0;
    int i;
    *pmaxwage = *pminwage = wage[0];
    for (i = 0; i < n; i++)
    {
    if (wage[i] > * pmaxwage)
    *pmaxwage = wage[i];
    if (wage[i] < *pminwage)
    *pminwage = wage[i];
    averwage += wage[i] / n;
    }
    return averwage;

    }
    ···

    相关文章

      网友评论

        本文标题:第九章-指针:C语言世界中所向披靡的“金箍棒”

        本文链接:https://www.haomeiwen.com/subject/tclpmctx.html