公共区往年真题1
递归
编写一个函数,使用递归算法求满足下述定义的整数序列的第n项。
f( n ) = 1 当 n <= 0 时
f( n ) = n * f( n-1 ) + f( n-2 ) 当 n > 0 时
函数原型如下:
long findf(int n);
参数说明:参数 n:整数序列的第n项。
函数返回值:整数序列的第n项的值。
例如输入:8,输出:137861
送分
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
long long findf(int n) {
if (n <= 0) return 1;
else return n * findf(n - 1) + findf(n - 2);
}
int main () {
int n;
scanf("%d", &n);
printf("%lld\n", findf(n));
return n;
}
图形输出
送分
输入10以内的整数n,输出n行的由数字构成的倒置直角三角形。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main () {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i - 1; j++) printf(" ");
for (int j = 1; j <= n - i + 1; j++) printf("%d", j);
printf("\n");
}
return 0;
}
链表
建立一个链表,每个结点存储一个学生的姓名和成绩,将a,b,c三个结点按照分数由高到低链接起来,然后输出,编写链表结点连接函数connect。
排序较复杂,这里直接分类讨论了,关于链表实现这点还是很简单的
struct Node * connect(struct Node *x, struct Node *y, struct Node *z) {
int a = x -> score, b = y -> score, c = z -> score;
if (a >= b && b >= c) {
x -> next = y; y -> next = z; z -> next = NULL;
return x;}
else if (a >= c && c >= b ) {
x -> next = z; z -> next = y; y -> next = NULL;
return x;}
else if (b >= a && a >= c ) {
y -> next = x; x -> next = z; z -> next = NULL;
return y;}
else if (b >= c && c >= a ) {
y -> next = z; z -> next = x; x -> next = NULL;
return y;}
else if (c >= a && a >= b ) {
z -> next = x; x -> next = y; y -> next = NULL;
return z;}
else if (c >= b && b >= a ) {
z -> next = y; y -> next = x; x -> next = NULL;
return z;}
}
英文单词输出
输入一个英文句子(20单词以内,不含标点符号或者数字特殊字符等),将其中各个单词按照字典顺序排序输出,各单词之间以空格分隔。
用二维数组遍历字符串读取各个单词,进行字符串之间的冒泡排序即可。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char ch[100] = {0};
char s[100][100] = {0};
int main () {
int cnt1 = 0, cnt2 = 0;
gets(ch);
for (int i = 0; i < strlen(ch); i++) {
if (ch[i] >= 'a' && ch[i] <= 'z' || ch[i] >= 'A' && ch[i] <= 'Z' ) {
if (cnt2 == 0) cnt1 ++;
s[cnt1][cnt2++] = ch[i];}
else cnt2 = 0;
}
for (int i = 1; i <= cnt1; i++) {
for (int j = 1; j < cnt1; j++) {
if (strcmp(s[j], s[j + 1]) > 0) {
char t[100] = {0};
strcpy(t, s[j]);
strcpy(s[j], s[j + 1]);
strcpy(s[j + 1], t);
}
}
}
for (int i = 1; i <= cnt1; i++) {
if (i != cnt1) printf("%s ", s[i]);
else printf("%s\n", s[i]);}
return 0;
}
总结:
总体来说送分卷,可以在10min内写完。
本文作者:misasteria
本文链接:https://www.cnblogs.com/misasteria/p/16332077.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步