文章目录
021 字符按ASCII码降序排列
【题目】请编写函数fun,对长度为7个字符的字符串,除首尾字符外,将其余5个字符按ASCII码降序排列。
【代码】
#include <stdio.h>
void fun(char *s, int num) {
char t;
int i, j;
for (i = 1; i < num - 2; i++) {
for (j = i + 1; j < num - 1; j++) {
if (s[i] - s[j] < '0') {
t = s[i];
s[i] = s[j];
s[j] = t;
}
}
}
}
int main() {
char s[7] = "HelloCC";
fun(&s, 7);
int i;
for (i = 0; i < 7; i++) {
printf("%c ", s[i]);
}
return 0;
}
022 带头节点的链表结构求最高分
【题目】N名学生的成绩已在主函数中放入一个带头节点的链表结构中,h指向链表的头节点。请编写函数fun,它的功能是:找出学生的最高分,由函数值返回。
【代码】
#include <stdio.h>
#include <stdlib.h>
typedef struct student {
struct student *next;
double score;
} Student;
double fun(Student *h) {
double max;
Student *q = h;
max = h->score;
do {
if (q->score > max) {
max = q->score;
}
q = q->next;
} while (q);
return max;
}
Student *Create() {
double score;
scanf("%lf", &score);
Student *student;
if (score == -1) {
return NULL;
} else {
student = (Student *)malloc(sizeof(Student));
student->score = score;
student->next = Create();
}
return student;
}
int main() {
Student *h;
h = Create();
double max = fun(&h);
printf("%lf", max);
return 0;
}
023 判断字符串是否为回文
【题目】请编写函数fun,该函数的功能是:判断字符串是否为回文?若是则函数返回1,主函数中输出YES,否则返回0,主函数中输出NO。回文是指顺读和倒读都是一样的字符串。
【代码】
#include <stdio.h>
int fun(char *str) {
int i, n = 0, flag = 1;
char *p = str;
while (*p) {
n++;
p++;
}
for (i = 0; i < n / 2; i++) {
if (str[i] != str[n - 1 - i]) {
flag = 0;
break;
}
}
return flag;
}
int main() {
char str[100];
gets(str);
if (fun(&str) == 1) {
printf("true");
} else {
printf("false");
}
return 0;
}
024 将一个字符串转换为一个整数
【题目】请编写一个函数fun,它的功能是:将一个字符串转换为一个整数(不得调用C语言提供的将字符串转换为整数的函数)。
【代码】
#include <stdio.h>
#include <string.h>
long fun(char *p) {
long s = 0, t;
int i = 0, j, n = strlen(p), k, s1;
if (p[0] == '-') {
i++;
}
for (j = i; j < n; j++) {
t = p[j] - '0';
for (k = j; k < n - 1; k++) {
t *= 10;
}
s += t;
}
if (p[0] == '-') {
return -s;
}
return s;
}
int main() {
char a[100];
gets(a);
printf("%ld", fun(&a));
return 0;
}
025 函数返回较长的字符串
【题目】请编写一个函数fun,它的功能是:比较两个字符串的长度,(不得调用C语言提供的求字符串长度的函数),函数返回较长的字符串。若两个字符串长度相同,则返回第一个字符串。
【代码】
#include <stdio.h>
char *fun(char *s, char *t) {
char *p, *s1 = s, *t1 = t;
int n = 0, m = 0;
while (*s1) {
n++;
s1++;
}
while (*t1) {
m++;
t1++;
}
if (n >= m) {
p = s;
} else {
p = t;
}
//puts(p);
return p;
}
int main() {
char a[100];
gets(a);
char b[100];
gets(b);
char *c;
c = fun(a, b);
puts(c);
return 0;
}
026 根据公式求值
【题目】请编写一个函数fun,它的功能是:根据以下公式求X的值(要求满足精度0.0005,即某项小于0.0005时停止迭代):
X/2=1+1/3+1×2/3×5+1×2×3/3×5×7+1×2×3×4/3×5×7×9+…+1×2×3×…×n/3×5×7×(2n+1)
程序运行后,如果输入精度0.0005,则程序输出为3.14…。
【代码】
#include <stdio.h>
double fun(double eps) {
double s = 1.0, n = 1.0, x = 0;
while (fabs(s) >= eps) {
x += s;
s *= n / (2 * n + 1);
n++;
}
x = x * 2;
return x;
}
int main() {
printf("%lf", fun(0.0005));
return 0;
}
027 整数放在数组a中,返回数的个数
【题目】请编写一个函数fun,它的功能是:求出1到m之内(含m)能被7或11整除的所有整数放在数组a中,通过n返回这些数的个数。
【代码】
#include <stdio.h>
void fun(int m, int *a, int *n) {
int i, j = 0;
*n = 0;
for (i = 1; i <= m; i++) {
if (i % 7 == 0 || i % 11 == 0) {
a[j] = i;
j++;
}
}
*n = j;
}
int main() {
int a[100];
int m = 12;
int n;
fun(m, a, &n);
printf("%d", n);
return 0;
}
028 找出一维整型数组元素中最大的值和它所在的下标
【题目】请编写一个函数fun,它的功能是:找出一维整型数组元素中最大的值和它所在的下标,最大的值和它所在的下标通过形参传回。数组元素中的值已在主函数中赋予。主函数中x是数组名,n是x中的数据个数,max存放最大值,index存放最大值所在元素的下标。
【代码】
#include <stdio.h>
void fun(int a[], int n, int *max, int *index) {
int i;
*max = a[0];
*index = 0;
for (i = 0; i < n; i++) {
if (a[i] > *max) {
*max = a[i];
*index = i;
}
}
}
int main() {
int a[] = {1,2,5,4};
int max, index;
fun(a, 4, &max, &index);
printf("a[%d] = %d", index, max);
return 0;
}
029 字符串换字母
【题目】请编写一个函数fun,它的功能是:将ss所指字符串中所有下标为奇数位置上的字母转换为大写(若该位置上不是字母,则不转换)。
【代码】
#include <stdio.h>
#include <string.h>
void fun(char *ss) {
int i, n = strlen(ss);
for (i = 0; i < n; i++) {
if (i % 2 != 0) {
if (ss[i] >= 'a' && ss[i] <= 'z') {
ss[i] -= 32;
}
}
}
}
int main() {
char a[100];
gets(a);
fun(a);
puts(a);
return 0;
}
030 求二维数组中最大元素
【题目】请编写一个函数fun,它的功能是:求出一个2×M整型二维数组中最大元素的值,并将此值返回调用函数。
【代码】
#include <stdio.h>
#define M 2
int fun(int a[][M]) {
int i, j, max = a[0][0];
for (i = 0; i < 2; i++) {
for (j = 0; j < M; j++) {
if (a[i][j] >max) {
max = a[i][j];
}
}
}
return max;
}
int main() {
int a[2][M];
int i, j;
for (i = 0; i < 2; i++) {
for (j = 0; j < M; j++) {
scanf("%d", &a[i][j]);
}
}
int max = fun(a);
printf("%d", max);
return 0;
}