编程完成以下的要求: 1、求无符号数组BUF中的最大值存放于MAX中。(必做) 2、求有符号数组BUF中的最小值存放于MIN中。(必做) 3、统计数组BUF中正数、负数、0的个数顺序存放在变量NUM中。(必做) 4、从键盘输入一个正整数,判断是否是回文数,是则输出“YES”,
时间: 2024-05-09 10:18:48 浏览: 82
汇编在 BUF 和 BUF+1、BUF+2 单元分别放有一个有符号字节型数,编程序 将其中最小数存入 MIN 单元中。
否则输出“NO”。(选做)
以下是示例代码:
```
#include <stdio.h>
void find_max(unsigned int BUF[], int len, unsigned int *MAX)
{
int i;
*MAX = BUF[0];
for (i = 1; i < len; i++) {
if (BUF[i] > *MAX) {
*MAX = BUF[i];
}
}
}
void find_min(int BUF[], int len, int *MIN)
{
int i;
*MIN = BUF[0];
for (i = 1; i < len; i++) {
if (BUF[i] < *MIN) {
*MIN = BUF[i];
}
}
}
void count_nums(int BUF[], int len, int *num_positive, int *num_negative, int *num_zero)
{
int i;
*num_positive = 0;
*num_negative = 0;
*num_zero = 0;
for (i = 0; i < len; i++) {
if (BUF[i] > 0) {
(*num_positive)++;
} else if (BUF[i] < 0) {
(*num_negative)++;
} else {
(*num_zero)++;
}
}
}
int is_palindrome(int num)
{
int reverse_num = 0;
int temp = num;
while (temp != 0) {
reverse_num = reverse_num * 10 + temp % 10;
temp /= 10;
}
return (num == reverse_num);
}
int main()
{
unsigned int max;
int min, num_positive, num_negative, num_zero;
unsigned int BUF[5] = {10, 20, 30, 40, 50};
int BUF2[5] = {-10, 20, 0, -30, 40};
find_max(BUF, 5, &max);
find_min(BUF2, 5, &min);
count_nums(BUF2, 5, &num_positive, &num_negative, &num_zero);
printf("max: %u\n", max);
printf("min: %d\n", min);
printf("positive: %d, negative: %d, zero: %d\n", num_positive, num_negative, num_zero);
int num;
printf("Enter a positive integer: ");
scanf("%d", &num);
if (is_palindrome(num)) {
printf("YES\n");
} else {
printf("NO\n");
}
return 0;
}
```
阅读全文