蓝桥杯基础练习合集八 1.开灯问题2.蛇形填数3.竖式问题4.TeX中的引号5.WERTYU

目录

1.开灯问题

2.蛇形填数

3.竖式问题

4.TeX中的引号

5.WERTYU


1.开灯问题

问题描述

有n盏灯,编号为1-n。第1个人把所有灯打开,第2个人按下所有编号为2的倍数的开关(这些灯将被关掉),第3个人按下所有编号为3的倍数的开关(其中关掉的灯将被打开,开着的灯将被关闭),依此类推。一共有k个人,问最后有哪些灯开着?输入n和k,输出开着的灯的编号。k<=n<=1000。

样例输入

7 3

样例输出

1 5 6 7

分析

用a[1],a[2],......,a[n]表示编号为1,2,3,......,n的灯是否开着。模拟这些操作即可。

代码

#include<stdio.h>
#include<string.h>
#define maxn 1010
int a[maxn];
int main() {
	int n, k, first = 1;
	memset(a, 0, sizeof(a));//把a数组的值全赋值为0,一开始所有灯都是关着的
	scanf("%d%d", &n. &k);
	for (int i = 1; i <= k; i++) {//对于的人
		for (int j = 1; j <= n; j++) {//对应的灯
			if (j % i == 0) {
				a[j] != a[j];//开关灯
			}
		}
	}
	for (int i = 1; i <= n; i++) {
		if (a[i]) {
			if (first) {//输出的第一个不带空格
				first = 0;
			}
			else {
				printf(" ");
			}
		}
		printf("%d", i);
	}
}

2.蛇形填数

问题描述

在n*n方阵里填入1,2,...n*n,要求填成蛇形。例如,n=4时方阵为:

10 11 12 1

9   16 13 2

8   15 14 3

7   6   5  4

在上面的方阵中,多余的空格只是为了便于观察规律,不必严格输出。n<=8。

分析

先向下移动,再向左,再向上,再向右移动

代码

#include<stdio.h>
#include<string.h>
#define maxn 20
int a[maxn][maxn];
int main() {
	int n, x, y, tot = 0;
	scanf("%d", &n);
	memset(a, 0, sizeof(a));
	tot = a[x = 0][y = n - 1] = 1;
	while (tot < n * n) {
		while (x + 1 < n && !a[x + 1][y]) a[++x][y] = ++tot;
		while (y - 1 >= 0 && !a[x][y - 1]) a[x][--y] = ++tot;
		while (x - 1 >= 0 && !a[x - 1][y]) a[--x][y] = ++tot;
		while (y + 1 < n && !a[x][y + 1])a[x][++y] = ++tot;
	}
	for (x = 0; x < n; x++) {
		for (y = 0; y < n; y++) printf("%3d", a[x][y]);
		printf("\n");
	}
	return 0;
}

3.竖式问题

问题描述

找出所有形如abc*de(三位数乘以两位数)的算式,使得在完整的竖式中,所有数字都属于一个特定的数字集合。输入数字集合(相邻数字之间没有空格),输出所有竖式。使每个竖式前应有编号,之后应有一个空行。最后输出解的总数。具体格式见样例输出(为了便于观察,竖式中的空格改用小数点显示,但所写程序中应该输出空格,而非小数点)。

样例输入

2357

样例输出

<1>

..775

X..33

-----

.2325

2325.

-----

25575

The number of solutions = 1

代码

#include<stdio.h>
#include<string.h>
int main() {
	int count = 0;
	char s[20], buf[99];
	scanf("%s", s);
	for (int abc = 111; abc <= 999; abc++) {
		for (int de = 11; de <= 99; de++) {
			int x = abc * (de % 10), y = abc * (de / 10), z = abc * de;
			sprintf(buf, "%d%d%d%d%d", abc, de, x, y, z);
			int ok = 1;
			for (int i = 0; i < strlen(buf); i++) {
				if (strchr(s, buf[i]) == NULL) {
					ok = 0;
				}
			}
			if (ok) {
				printf("<%d>\n", ++count);
				printf("%5d\nX%4d\n-----\n%5d\n%4d\n-----\n%5d\n\n", abc, de, x, y, z);
			}
		}
	}
	printf("The number of solutions = %d\n", count);
	return 0;
}

4.TeX中的引号

问题描述

在TeX中,左双引号是“ “ ”,右双引号是" ” "。输入一篇包含双引号的文章,你的任务是把它转换为TeX的格式。

样例输入

"To be or not to be,"quoth the Bard,"that is the question".

样例输出

“To be or not to be,”quoth the Bard,“that is the question”.

代码

#include<stdio.h>
int main() {
	int c, q = 1;
	while ((c = getchar()) != EOF) {
		if (c == '"') {
			printf("%s", q ? "“" : "”");
			q = !q;
		}
		else {
			printf("%c", c);
		}
	}
	return 0;
}

5.WERTYU

问题描述

把手放在键盘上时,稍不注意就会往右错一位。这样,输入Q会变成输入W,输入J会变成输入K等。输入一个错位后敲出的字符串,输出打字员本来想打出的句子。输入保证合法,即一定是错位之后的字符串。例如输入中不会出现大写字母A。

样例输入

O S, GOMR YPFSU/

样例输出

I AM FINE TODAY.

代码

#include<stdio.h>
char s[] = "`1234567890-=QWERTYUIOP[]\\ASDFGHJKL;'ZXCVBNM,./";
int main() {
	int i, c;
	while ((c = getchar()) != EOF) {
		for (i = 1; s[i] && s[i] != c; i++) {//找错位之后的字符在常量表中的位置
			if (s[i]) {//如果找到,则输出它的前一个字符
				putchar(s[i - 1]);
			}
			else {
				putchar(c);
			}
		}
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值