实验四

任务一

源代码:

#include<stdio.h>
#define N 4
#define M 2

void test1(){
    int x[N]={1, 9, 8, 4};
    int i;
    printf("sizeof(x)=%d\n",sizeof(x));
    for(i=0;i<N;++i)
        printf("%p:%d\n",&x[i],x[i]);
        printf("x=%p\n",x);
}
void test2(){
    int x[M][N]={{1, 9, 8, 4},{2, 0, 4, 9}};
    int i, j;
    printf("sizeof(x)=%d\n",sizeof(x));
    for(i=0;i<N;++i)
        for(j=0;j<N;++j)
            printf("%p:%d\n",&x[i][j],x[i][j]);
    printf("\n");
    printf("x=%p\n",x);
    printf("x[0]=%p\n",x[0]);
    printf("x[1]=%p\n",x[1]);
    printf("\n");
}
int main(){
    printf("测试1:int型一维数组\n");
    test1(); 
    printf("\n测试2:int型二维数组\n");
    test2(); 
    return 0;
}

运行结果:

问题1:是,一样

问题2:是,不一样,差值是一行元素所占字节数,意义在于反映二维数组在内存中按行存储时相邻两行之间的地址偏移量。

任务二

源代码:

#include<stdio.h>
#define N 100
void input(int x[],int n);
double compute(int  x[],int n);
int main(){
    int x[N];
    int n, i;
    double ans;
    while(printf("Enter n:"),scanf("%d",&n)!=EOF){
        input(x,n);
        ans = compute(x, n);
        printf("ans = %.2f\n\n",ans);
    }
    return 0;
}
void input(int x[N],int n){
    int i;
    for(i=0;i<n;++i)
    scanf("%d",&x[i]);
}
double compute(int x[],int n){
    int i, high, low;
    double ans;
    high=low=x[0];
    ans=0;
    
    for(i=0;i<n;++i){
        ans+=x[i];
        if(x[i]>high)
            high=x[i];
        else if(x[i]<low)
            low=x[i];
    }
    ans = (ans-high-low)/(n-2);
    return ans;
    
}

运行结果:

问题:input是从键盘录入n个数,compute是求数组删去最大值和最小值后的平均数

任务三

源代码:

#include <stdio.h>
#define N 100

void output(int x[][N], int n);
void init(int x[][N], int n, int value);

int main(){
    int x[N][N];
    int n, value;

    while(printf("Enter n and value: "), scanf("%d%d", &n, &value) != EOF) {
        init(x, n, value);  
        output(x, n);      
        printf("\n");
    }

    return 0;
}
void output(int x[][N], int n){
    int i, j;

    for(i = 0; i < n; ++i) {
        for(j = 0; j < n; ++j)
            printf("%d ", x[i][j]);
        printf("\n");
    }
}
void init(int x[][N], int n, int value){
    int i, j;

    for(i = 0; i < n; ++i)
        for(j = 0; j < n; ++j)
            x[i][j] = value;
}

运行结果:

问题1:第二维不可省略

问题2:output是定义一个n阶方阵,init是将value输入方阵中

任务四

源代码:

#include<stdio.h>
#define N 100

void input(int x[],int n);
float median(int x[],int n);



int main(){
    int x[N];
    int n;
    double ans; 
    while(printf("Enter n:"),scanf("%d",&n)!=EOF){
        input(x, n);
        ans = median(x, n);
        printf("ans = %g\n\n", ans);
    }
    return 0;
}


void input(int x[], int n){
    int i;
    for (int i = 0;i < n; i++){
        scanf("%d",&x[i]);
    }
}
float median(int x[],int n){
    int i,j;
    for(i=0;i<n-1;++i){
        for(j=0;j<n-i-1;j++){
            if(x[j]>x[j+1]){
            int temp = x[j];
            x[j]=x[j+1];
            x[j+1]=temp;
        }
        }
    }
    if(n%2!=0){
        return x[n/2];
    }
    else{
        return (x[n/2-1]+x[n/2])/2.0;
    }
}

运行结果:

任务五

源代码:

#include <stdio.h>
#define N 100

void input(int x[][N], int n);
void output(int x[][N],int n);
void rotate_to_right(int x[][N],int n);


int main(){
    int x[N][N];
    int n;
    
    printf("Enter n: ");
    scanf("%d",&n);
    input(x, n);
    printf("原始矩阵:\n");
    output(x, n);
    
    rotate_to_right(x, n);
    printf("变换后矩阵:\n");
    output(x, n);
    
    return 0;
}

void input(int x[][N], int n){
    int i,j;
    
    for(i=0;i<n;++i){
        for (j = 0;j<n;++j)
            scanf("%d",&x[i][j]);
    }
}

void output(int x[][N], int n){
    int i,j;
    
    for(i = 0;i<n;++i){
        for(j=0;j<n;++j)
            printf("%4d",x[i][j]);
        
        printf("\n");
        
    }
}

void rotate_to_right(int x[][N],int n){
    int temp[N];
    int i,j ;
    for (j=0;j<n;j++){
        temp[j]=x[j][n-1];
        for(i=n-1;i>0;i--){
            x[j][i]=x[j][i-1];
        }
        x[j][0]=temp[j];
    }
}

运行结果:

任务六

源代码:

#include<stdio.h>
#define N 100

void dec_to_n(int x,int n);

int main(){
    int x;
    while(printf("输入十进制整数:"),scanf("%d",&x) !=EOF){
        dec_to_n(x, 2);
        dec_to_n(x, 8);
        dec_to_n(x, 16);
        
        printf("\n");
    }
    return 0;
}

void dec_to_n(int x, int n) {
    char result[100];
    int i = 0;
    int j;
    while (x > 0) {
        int remainder = x % n;
        if (n == 16 && remainder >= 10) {
            if (remainder == 10) {
                result[i++] = 'A';
            } else if (remainder == 11) {
                result[i++] = 'B';
            } else if (remainder == 12) {
                result[i++] = 'C';
            } else if (remainder == 13) {
                result[i++] = 'D';
            } else if (remainder == 14) {
                result[i++] = 'E';
            } else if (remainder == 15) {
                result[i++] = 'F';
            }
        } else {
            result[i++] = remainder + '0';
        }
        x /= n;
    }
    result[i] = '\0';
    for (j = i - 1; j >= 0; j--) {
        printf("%c", result[j]);
    }
    printf("\n");
}

 

运行结果:

 

任务七

源代码:

#include<stdio.h>
#define N 100

void input(int x[][N], int n);
void output(int x[][N], int n);
int is_magic(int x[][N], int n);


int main(){
    int x[N][N];
    int n;
    
    while(printf("输入n: "),scanf("%d",&n) !=EOF){
        printf("输入方阵:\n");
        input(x, n);
        
        printf("输出方阵:\n");
        output(x, n);
        
        if(is_magic(x, n))
            printf("是魔方矩阵\n\n");
        else
            printf("不是魔方矩阵\n\n");
    }
    return 0;
}

void input(int x[][N],int n){
    int i,j;
    
    for(i = 0;i<n;++i){
        for(j = 0;j < n;++j)
            scanf("%d",&x[i][j]);
            
    }
}

void output(int x[][N],int n){
    int i,j;
    for(i=0;i<n;++i){
        for(j=0;j<n;++j)
            printf("%4d",x[i][j]);
        printf("\n");
    }
}
int is_magic(int x[][N],int n){
    int sum = 0;
    int m,i,j;
    for (i=0;i<n;i++){
        sum+=x[0][i];
    }
    for (i=0;i<n;i++){
        m=0;
        for (j=0;j<n;j++){
            m+=x[i][j];
        }
        if(m!=sum)
        return 0;
    }
    m = 0;
    for(i=0;i<n;i++){
        m+=x[i][i];
    }
    if(m!=sum){
        return 0;
    }
    m=0;
    for (i=0;i<n;i++){
        m+=x[i][n-1-i];
    }
    if(m!=sum){
        return 0;
    }
    return 1;
}

运行结果:‘

任务八

源代码:

#include <stdio.h>

int checkNumber(int num); 
int main() {
    int i;
    for (i = 1; i < 10000; i++) {
        if (checkNumber(i)) {
            printf("%d\n", i);
            break;
        }
    }
    return 0;
}
int checkNumber(int num) {
    int digitUsed[10] = {0};
    int square = num * num;
    int cube = num * num * num;

    while (square > 0) {
        digitUsed[square % 10] = 1;
        square /= 10;
    }

    while (cube > 0) {
        digitUsed[cube % 10] = 1;
        cube /= 10;
    }
    int i;
    for ( i = 0; i < 10; i++) {
        if (digitUsed[i] == 0) {
            return 0;
        }
    }
    return 1;
}

运行结果:

 

posted @ 2024-11-09 21:26  林子龙  阅读(8)  评论(0编辑  收藏  举报