深入攻克c语言--day01

  1. 打印九九乘法表
#include<stdio.h>
#include<stdlib.h>

int main()
{
    int i = 1, j = 1;
    for (i = 1; i <= 9; i++)
    {
        for (j = 1; j <= 9; j++)
        {
            printf("%d * %d = %d \n", i, j, i * j);
        }
            
    }
    system("pause");
    return 0;
}<br />

2.打印如下图形

(1)

深入攻克c语言--day01
#include<stdio.h>
#include<stdlib.h>

int main()
{
    int i = 1, j = 1, k = 5, m = 5;
    for (i = 1; i <= 9; i++)
    {
        for (j = 1; j <= 9; j++)
        {
            if (j < 5)
            {
                if (j / k == 0)
                    printf(" ");
                else printf("*");
            }
            if (j == 5)printf("*");
            if (j >= 5)
            {
                if (j / m == 0)
                    printf("*");
                else printf(" ");
            }
            
        }
        if (i < 5 && k > 0)
        {
            k--;
            m++;
        }
        if (i >= 5)
        {
            k++;
            m--;
        }
        printf("\n");
    }


    system("pause");
    return 0;
}

(2)

深入攻克c语言--day01
#include<stdio.h>
#include<stdlib.h>

int main()
{
    int i = 1, j = 1, k = 5, m = 5;
    for (i = 1; i <= 9; i++)
    {
        for (j = 1; j <= 9; j++)
        {
            if (j < 5)
            {
                if (j ==k)
                    printf("*");
                else printf(" ");
            }
            //if (j == 5)printf("*");
            if (j >= 5)
            {
                if (j ==m)
                    printf("*");
                else printf(" ");
            }

        }
        if (i < 5 && k > 0)
        {
            k--;
            m++;
        }
        if (i >= 5)
        {
            k++;
            m--;
        }
        printf("\n");
    }
    system("pause");
    return 0;
}

(3)

深入攻克c语言--day01
#include<stdio.h>
#include<stdlib.h>

int main()
{
    int i = 1, j = 1, tmp1 = 4, tmp2 = 5;

    for (i = 0; i < 5; i++)
    {
        if (i == 0)
                printf("\n");

        else
        {
            for (j = 5; j > i; j--)
            {
                printf(" ");
            }
            for (int m = 0; m <= j; m++)
            {
                printf("* ");
            }
            for (int k = 4; k >i; k--)
            {
                printf("  ");
            }
            for (int m = 0; m <= j; m++)
            {
                printf("* ");
            }
            printf("\n");
        }
        
    }
    
    for (int i = 0; i<11; i++)
    {
        for (int m = 0; m < i; m++)
        {
            printf(" ");//先打印空格
        }
        for (int j = i; j<11; j++)
        {
            
            printf("* ");
        }
        printf("\n");

    }
    
    system("pause");
    return 0;
}

3

1求两个有序数组的公共元素

#include<stdio.h>
#include<stdlib.h>
//求两个有序数组的公共元素

int main()
{
    int a[5] = { 1,2,3,4,5 }, b[5] = { 1,3,5,7,9 };
    int ret[5],cnt=0;
    for (int i = 0; i < 5; i++)
    {    
        for (int j = 0; j < 5; j++)
        {    
            if (a[i] == b[j])ret[cnt ++ ] = b[j];
            else continue;
        }
    }
    for (int i = 0; i < cnt; i++)printf("%d ", ret[i]);
    printf("\n");
    system("pause");
    return 0;
}

(2)求三个有序数组的公共元素。

#include<stdio.h>
#include<stdlib.h>
//求三个有序数组的公共元素
#define A 5
#define B 5
#define C 5
int main()
{
    int a[A] = { 1,2,3,4,5 }, b[B] = { 1,3,5,7,9 }, c[C] = { 1,2,5,4,7 };
    int ret[5], cnt = 0;
    for (int i = 0; i < 5; i++)
    {
        for (int j = 0; j < 5; j++)
        {
            for (int k = 0; k < 5; k++)
            {
                if (a[i] == b[j] && b[j] == c[k])
                {
                    ret[cnt++] = c[k];
                }
                else continue;
            }

        }
    }
    for (int i = 0; i < cnt; i++)printf("%d  ", ret[i]);
    printf("\n");
    system("pause");
    return 0;
}

(2)求n个有序数组的公共元素

#include<stdio.h>
#include<stdlib.h>
//求N个有序数组的公共元素
#define A 5
#define N 3
int main()
{
    int a[N][A] = { 1,2,3,4,5, 1,3,5,7,9 , 1,2,5,4,7 };
    int ret[5], cnt = 0;
    for (int i = 0; i < 5; i++)
    {
        for (int j = 0; j < 5; j++)
        {
            for (int k = 0; k < 5; k++)
            {
                if (a[0][i] == a[1][j] && a[1][j] == a[2][k])
                {
                    ret[cnt++] = a[2][k];
                }
                else continue;
            }

        }
    }
    for (int i = 0; i < cnt; i++)printf("%d  ", ret[i]);
    printf("\n");
    system("pause");
    return 0;
}

4.求数组的最大值和次大值。

#include<stdio.h>
#include<stdlib.h>
//求数组的最大值和次大值
#define A 11
#define max(a, b) (a) > (b)? (a) : (b) 
int *fun(int *a)
{
    int ret[2],j=0;
    for (int i = 0; i < a; i++)
    {
            ret[j%2] = max(ret[j % 2], a[i]);
            j++;
    }
    printf("%d %d", ret[0], ret[1]);
    return ret;
}
int main()
{
    int a[A] = { 1,2,3,4,10,5,9,7,6,11,0 };
    fun(a);
    printf("\n");
    system("pause");
    return 0;
}

5.给定一个n个整型元素的数组a,其中有一个元素出现次数超过n / 2,求这个元素。

#include<stdio.h>
#include<stdlib.h>
#define n 10
 
int fun(int *a,int cnt)
{
    int curValue = a[0];

    for (int i = 1; i < n; i++)
    {
        if (a[i] == curValue)cnt++;
        else
        {
            cnt--;
            if (cnt < 0)
            {
                curValue = a[i];
                cnt = 1;
            }
        }
    }

    return curValue;
}
int main()
{
    int a[n] = { 3,9,9,9,9,9,2,3,9,2 };
    int i = 0,cnt=1;
    int ret = {0};
    ret = fun(a,cnt);
    //while (ret[i] == ret[i+1])cnt++;
    printf("这个数是%d", ret);
    printf("\n");
    system("pause");
    return 0;
}

6.给定一个含有n个元素的整型数组,找出数组中的两个元素xy使得abs(x - y)值最小

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define n 10

int main()
{
    int a[n] = { 3,-9,9,19,6,7,3,4,11,2 };
    int i = 0, j = 0, ret[2] = { a[0]-a[1],0 }, loc[2];
    for (i = 0; i < n; i++)
    {
        for (j = i+1; j < n; j++)
        {
            ret[1] = abs(a[i] - a[j]);
            printf("%d\n", ret[1]);
            if (ret[1] == 0)
            {
                loc[0] = a[i];
                loc[1] = a[j];
                i = n;
                j = n;
                break;
            }
            if (ret[0] > ret[1])
            {
                ret[0] = ret[1];
                loc[0] = a[i];
                loc[1] = a[j];
            }
            else
            {
                continue;
            }
        }
        
    }
    printf("%d %d", loc[0], loc[1]);
    printf("\n");
    system("pause");
    return 0;
}

8. 给定含有1001个元素的数组,其中存放了1-1000之内的整数,只有一个整数是重复的,请找出这个数

A1+ … + A1001 – (1 + …+ 1000)

#include<stdio.h>
#include<stdlib.h>
#define N 1001
int main()
{
    int a[N],sum=0,suma=0 ,ret=0;
    for (int i =0; i < 1000; i++)
    {
        a[i] = i+1;
    }
    a[1000] = 33;
    for (int i = 1; i < 1001; i++)
    {
        sum += i; 
        
    }
    for (int i = 0; i < 1001; i++)
    {
        suma += a[i];

    }
    ret = suma - sum;
    printf("%d", ret);
    printf("\n");
    system("pause");
    return 0;
}

相关推荐