Java教程

task4

本文主要是介绍task4,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1.

#include<stdio.h>
const int N = 4;

int main()
{
    int a[N] = { 2,0,2,1 };
    char b[N] = { '2','0','1','1' };
    int i;

    printf("sizeeof(int)=%d\n", sizeof(int));
    printf("sizeeof(char)=%d\n", sizeof(char));
    printf("\n");

    for (i = 0; i < N; i++)
        printf("%x: %d\n", &a[i], a[i]);

    printf("\n");
    for (i = 0; i < N; i++)
        printf("%x: %c\n", &b[i], b[i]);

    return 0;

    
}

 

 (1).是,4个

(2)。是,1个

#include<stdio.h>

int main()
{
    int a[2][3] = { {1,2.3},{4.5,6} };
    char b[2][3] = { {'1','2','3'},{'4','5','6'} };
    int i, j;

    for (i = 0; i < 2; i++)
        for (j = 0; j < 3; j++)
            printf("%x: %d\n", &a[i][j], a[i][j]);
    printf("\n");
    for (i = 0; i < 2; i++)
        for (j = 0; j < 3; j++)
            printf("%x: %c\n", &b[i][j], b[i][j]);
    return 0;

    
}

 

 (1)是,4

(2)是,1

2.

#include<stdio.h>

#define N 1000
int fun(int n, int m, int bb[N])
{

    int i, j, k = 0, flag;
    for (j = n; j <= m; j++)
    {
        flag = j;
        for (i = 2; i < j; i++)
            if (flag % i == 0)
            {
                flag = 0;
                break;
            }
        if (i == j)
        {
            bb[k] = j;
            k++;
        }
    }
            return k;

}
int main()
{
    int n = 0, m = 0, i, k,bb[N];
 
    scanf("%d", &n);
    scanf("%d", &m);

    for (i = 0; i < m - n; i++)
        bb[i] = 0;

    k = fun(n, m, bb); 

    for (i = 0; i < k; i++)
        printf("%4d", bb[i]);
    return 0;
    
}

 

 

 

 3.

#include<stdio.h>

const int N = 5;

int find_max(int x[], int n);
void output(int x[], int n);
void input(int x[], int n);
int main()
{
    int a[N];
    int max;

    input(a, N);
    output(a, N);
    max = find_max(a, N);

    printf("max=%d\n", max);
    return 0;
}
void input(int x[], int n)
{
    int i;

    for (i = 0; i < n; i++)
        scanf("%d", &x[i]);

}
void output(int x[], int n)
{
    int i;

    for (i = 0; i < n; i++)
        printf("%d ", x[i]);
    printf("\n");

    
}
int find_max(int x[], int n)
{
    int max, i;
    max = 0;
    for (i = 0; i < n; i++)
    {
        max = max > x[i] ? max : x[i];
    }
    return max;
}

 

 4.

#include<stdio.h>
void dec2n(int x, int n);

int main()
{
    int x;
    printf("输入一个十进制数:");
    scanf_s("%d", &x);

    dec2n(x, 2);
    dec2n(x, 8);
    dec2n(x, 16);


    return 0;
}
void dec2n(int x, int n)
{
    int i, k;
    int a[20];
    k = 0;
    i = 0;
    while (x / n>=0)
    {
        a[i] = x % n;
        k++;
        i++;
        x = x/n;
        if (x == 0)
            break;
    }

    for (i = k - 1; i >= 0; i--)
    {
        if (a[i] ==10)
            printf("A");
        if (a[i] ==11)
            printf("B");
        if (a[i] == 12)
            printf("C");
        if (a[i] == 13)
            printf("D");
        if (a[i] == 14)
            printf("E");
        if (a[i] == 15)
            printf("F");
        if(a[i]<10)
        printf("%d", a[i]);
    }
    printf("\n");


}

 

 

 5.

#include <stdio.h>
int main()
{
    int a[10][10], i, j, n;
    printf("Enter n:");
    
    while (scanf_s("%d", &n) != EOF)
    {
        for (i = 0; i < n; i++)
        {
            for (j = 0; j < n; j++)
            {
                if (i < j)
                    a[i][j] = i + 1;
                else
                    a[i][j] = j + 1;
            }
        }
    }
    for (i = 0; i <n; i++)
    {
        for (j = 0; j < n; j++)
            printf(" %d ", a[i][j]);
        printf("\n");
    }
   
    return 0;
}

 

 

这篇关于task4的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!