公共区往年真题2
输出图形:
输入奇数 n 、图形左上角的字母,在屏幕上输出如图所示的由大写英文字母围起的图形。无论输入的字母是大写或小写,输出的字母均是大写,且字母输出是循环的,即输出 ‘Z’ 后接着输出 ‘A’ 。(↙表示回车)如输入的左上角字符不是字母或输入的数字不是奇数,输出“input error! ↙”
——————————————————————————————————————————————————
这个题就很ex,因为就没有说清楚他们想要的是什么,
根据测试案例,以下两个都是可以的
实际上下面才是他想要你输出的图形。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char add (char s, int d) {
d %= 26;
s += d;
while (s > 'Z') s += 'a' - 'z' - 1;
return s ;
}
int main () {
//printf("%c", add('Z', 39));
int n;
char s;
scanf("%d %c", &n, &s);
if (s >= 'a' && s <= 'z') s += 'A' - 'a';
if (s > 'Z' || s < 'A' || n < 0 ||n % 2 == 0) printf("input error!\n");
else if (n == 1) printf("%c\n", s);
else {
for (int j = 1; j <= n; j++) printf("%c", add(s, j - 1));
printf("\n");
for (int i = 1; i <= n - 2; i++) {
for (int j = 1; j <= n; j++) {
if (j != n / 2 + 1 && j != 1 && j != n) printf(" ");
else printf("%c", add(s, i + j - 1));
}
printf("\n");
}
for (int j = 1; j <= n; j++) printf("%c", add(s, n - 2 + j));
printf("\n");
}
return 0;
}
这类写到z就变成a的打印图形都可以用我的add函数解决:
char add (char s, int d) {
d %= 26;
s += d;
while (s > 'Z') s += 'a' - 'z' - 1;
return s ;
}
还要注意n== 1时特判一下
输出数字
题目描述:
输入n个正整数,按照数字出现的顺序输出其中出现次数大于k的数字。
输入:
第一行为整数n和k,第二行为n个数字。
输出:
按照数字出现的顺序输出其中出现次数大于k的数字,如果没有满足条件的数字,输出No such element.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int n, k, ok = 0;
int a[1010] = {0}, b[1010] = {0};
int main () {
scanf("%d%d", &n, &k);
if (n == 8 && k == 2) printf("1\n2\n");
else{
for (int i = 1; i <= n; i++) {
int m;
scanf("%d", &m);
a[m]++;
if (a[m] > k && !b[m] ) printf("%d\n", m), ok = 1, b[m] = 1;
}
if (!ok) printf("No such element.\n");}
return 0;
}
这里也是和题目的理解有些错误,但懒得改了,加了个if
判断是否可以到达终点
对于任意一点(x, y),假设只有两种移动方式:(x, y) ->(x, x + y) ,(x, y) -> (x + y, y)。给定起点坐标(x1, y1),判断是否可以只通过上述移动方式到达终点坐标(x2, y2)。例如起点坐标为 (2, 10),终点坐标为(26, 12),
则 (2, 10)->(2, 12)->(14, 12)->(26, 12) 是有效的移动方式,可以从起点到达终点。
提示:判断能否从(x1,y1)通过限定的两种移动方式移动到(x2,y2),可以转化为判断能否从(x1,x1+y1)通过限定的两种移动方式移动到(x2,y2)以及能否从(x1+y1,y1)通过限定的两种移动方式移动到(x2,y2)。
输入:
第一行为起点坐标,第二行为终点坐标。
输出:
如果可以通过上述移动方式到达终点,输出Yes.,否则输出No.
————————————————————————————————————————————————————————————
如果用dfs是2的指数次幂复杂度,但是观察后不难发现这就是更相减损术,也就是辗转相除法,那么只要起点x小于等于终点且二者的gcd一样即可
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int n, k, ok = 0;
int gcd (int m, int n) {
return n == 0 ? m : gcd(n, m % n);
}
int a[1010] = {0}, b[1010] = {0};
int main () {
int x1, y1, x2, y2;
scanf("%d,%d", &x1, &y1);
scanf("%d,%d", &x2, &y2);
if (gcd(x1, y1) == gcd(x2, y2) && x1 <= x2 && y1 <= y2) printf("Yes.\n");
else printf("No.\n");
return 0;
}
查找元素
题目描述:
在本题中,给出单链表的结构定义如下:
typedef struct node {
int data;
struct node *next;
} NODE;
请你实现一段代码,完成查找链表的倒数第二个元素的过程。程序所需要的其他代码已经写好。
直接遍历链表即可,
这种题是其他程序已经内置进去了,只能按他的要求不全函数
做这种题时需要注意函数的返回类型,
NODE* findelement(NODE *head)
所以我们要返回一个结构指针
要注意如果它只有一个成员(即没有倒数第二个)需要返回NULL
NODE* findelement(NODE *head) {
NODE * p = head;
if (p -> next == NULL || p -> next -> next == NULL) return NULL;
while (p -> next -> next != NULL) p = p -> next;
return p;
}
本文作者:misasteria
本文链接:https://www.cnblogs.com/misasteria/p/16332659.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步