高级语言期末2018级B卷(计算机学院)

1.编bool型函数,判断给定的三位数是否armstrong数。所谓armstrong数是指其值等于它本身每位数字立方和的数。

#include <stdio.h>
#include <stdbool.h>

bool armstr(int n) {
	int sum=0;
	int flag=n;
	while(n>0) {
		sum+=pow(n%10,3);
		n/=10;
	}
	if(flag==sum)
		return true;
	return false;
}
int main() {
	printf("%d",armstr(152));
}

2.编程序,输入正整数N,计算N的各个数字的阶乘之和r1!+r2!+...+rn!并输出。N=r1r2...rn

#include <stdio.h>
 
int main() {
	int a[100];
	int count = 0;
	int dat;
	int sum=0;
	scanf("%d",&dat);
	int flag=dat;
	while(dat>0) {
		a[count++]=dat%10;
		dat/=10;
	}
 
	for(int i=count-1; i>=0; i--)
			sum+=fac(a[i]);
	printf("%d",sum);
	return 0;
}
 
int fac(int n){
	int sum = 1;
	for(int i=1;i<=n;i++){
		sum*=i;
	}
	return sum;
}

3.编写递归函数,将一个给定的以“.”结束的字符串逆序输出

#include <stdio.h>

void reverse(char *str){
	int size=0;
	while(str[size]!='.')
		size++;
	int last=size-1;
	for(int j=0;j<i/2;j++,last--){
		char temp = str[j];
		str[j]=str[last];
		str[last]=temp;
	}
}

int main(){
	char str[100]="aaaaabbbbbccccc.";
	reverse(str);
	printf("%s",str);
}

4.编函数找出给定int型二维数组a[M][N]中最大值

#include <stdio.h>
#define M 5
#define N 5
 
void fun(int arr[][N], int rows, int cols, int *max) {
    *max = arr[0][0];
    int i,j;
    for (i = 0; i < rows; i++)
        for (j = 0; j < cols; j++) 
            if (arr[i][j] > *max)
                *max = arr[i][j];
}
 
int main() {
    int arr[M][N];
    int max = 0;
    int i,j;
    for (i = 0; i < M; i++) {
        for (j = 0; j < N; j++) {
            scanf("%d", &arr[i][j]);
            printf("%d ", arr[i][j]);
        }
        printf("\n");
    }
    fun(arr, M, N, &max);
    printf("max = %d\n", max);
    return 0;
}

5.求给定整数链表各项中整数之和,要求:

设计用于存储结点的数据类型

编函数求各项中整数之和

#include <stdio.h>

typedef struct node {
	int key;
	struct node *next;
} node;

int getsum(struct node* head) {
	struct node *p=head;
	int sum=0;
	while(p!=NULL) {
		sum+=p->key;
		p=p->next;
	}
	return sum;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值