前言
本文是鹏哥C语言刷题训练营第7节课内容的配套笔记,主要是一些简单基础性质的打印图形找规律性的题目。
题目链接(如果有需要请自行复制粘贴)
- 翻转直⻆三⻆形 题号:BC101 链接:https://www.nowcoder.com/pr
actice/2cdea429fa414fbba26e6c821724ca06?tpId=290&tqId=39889&ru=/exam/oj - 带空格的直⻆三⻆形 题号:BC102 链接:https://www.nowcoder.co
m/practice/192d1039c3d44155bb868073f5482670?tpId=290&tqId=39890&ru=/exam/oj - 翻转⾦字塔图案 题号:BC104 链接:https://www.nowcoder.com/p
ractice/c4775776e4464537bfb6a5ba37c596c6?tpId=290&tqId=39892&ru=/exam/oj - 菱形图案 题号:BC105 链接:https://www.nowcoder.com/practice/8
b935f8ffe99445dbd42494febd13e45?tpId=290&tqId=39893&ru=/exam/oj - K形图案 题号:BC106 链接:https://www.nowcoder.com/practice/
753cc2662d9949fe826a85f815db0ca4?tpId=290&tqId=39894&ru=/exam/oj - 箭形图案 题号:BC107 链接:https://www.nowcoder.com/practic
e/a6d1081e0c9a42f19e42ed6cd91556c1?tpId=290&tqId=39895&ru=/exam/oj - 反斜线形图案 题号:BC108 链接:https://www.nowcoder.com/pra
ctice/7b066963c4eb4d188becd18c37dfe22d?tpId=290&tqId=39896&ru=/exam/oj - 正斜线图案 题号:BC109 链接:https://www.nowcoder.com/practi
ce/61ef68d129534dfbb04b232e1244e447?tpId=290&tqId=39897&ru=/exam/oj - X形图案 题号:BC110 链接:https://www.nowcoder.com/practice/
83d6afe3018e44539c51265165806ee4?tpId=290&tqId=39898&ru=/exam/oj - 空⼼正⽅形图案 题号:BC111 链接:https://www.nowcoder.com/
practice/72347ee949dc47399186ee183632f303?tpId=290&tqId=39899&ru=/exam/oj - 空⼼三⻆形图案 题号:BC112 链接:https://www.nowcoder.com/p
ractice/2ccc5fca423e47f0b622fe6f151cfab4?tpId=290&tqId=39900&ru=/exam/oj
T1:
这个题目的话就是找规律,结合循环嵌套就可以做出来了
void D7_T1_way1(void) { int n = 0; while (scanf("%d", &n) != EOF) { //打印多少行 int i = 0; for (i = 0; i < n; i++) { int j = 0; //对每行打印什么进行控制 for (j = 0; j < n - i; j++) { printf("* "); } printf("\n"); } } }
T2
题目解析:这道题目型号前面是有空格的,注意一下就好了。
void D7_T2_way1(void) { int n = 0; while (~scanf("%d", &n)) { //打印多少行 int i = 0; for (i = 0; i < n; i++) { //对列进行控制 int j = 0; //对每行中的空格进行处理 for (j = 0; j < n - 1 - i; j++) { printf(" "); } //对每行的星号进行处理 for (j = 0; j < i + 1; j++) { printf("* "); } printf("\n"); } } }
T3:
都是类似的思路,直接看代码吧:
void D7_T3_way1(void) { int n = 0; while (~scanf("%d", &n)) { int i = 0; for (i = 0; i < n; i++) { int j = 0; for (j = 0; j < i ; j++) { printf(" "); } for (j = 0; j < n - i; j++) { printf("* "); } printf("\n"); } } } int main() { //D7_T1_way1(); //D7_T2_way1(); D7_T3_way1(); return 0; }
T4:
这个题目的话我们要分上下两部分来看,上半部分是一个n行的正三角形,下面是一个n+1行的倒三角形。
void D7_T4_way1(void) { int n = 0; while (~scanf("%d", &n)) { int i = 0; int j = 0; //上 for (i = 0; i < n; i++) { //上,空格 for (j = 0; j < n - i; j++) { printf(" "); } //上,* for (j = 0; j < i + 1; j++) { printf("* "); } printf("\n"); } //下 for (i = 0; i < n + 1; i++) { //下,空格 for (j = 0; j < i; j++) { printf(" "); } //下,* for (j = 0; j < n + 1 - i; j++) { printf("* "); } printf("\n"); } } }
T5:
void D7_T5_way1(void) { int n = 0; while (~scanf("%d", &n)) { int i = 0; int j = 0; for (i = 0; i < n; i++) { for (j = 0; j < n+1-i; j++) { printf("* "); } printf("\n"); } for (i = 0; i < n + 1; i++) { for (j = 0; j < i + 1; j++) { printf("* "); } printf("\n"); } } }
T6:
void D7_T6_way1(void) { int n = 0; while (~scanf("%d", &n)) { int i = 0; int j = 0; for (i = 0; i < n; i++) { for (j = 0; j < n - i; j++) { printf(" "); } for (j = 0; j < i + 1; j++) { printf("*"); } printf("\n"); } for (i = 0; i < n + 1; i++) { for (j = 0; j < i; j++) { printf(" "); } for (j = 0; j < n + 1 - i; j++) { printf("*"); } printf("\n"); } } }
T7:
void D7_T7_way1(void) { int n = 0; while (~scanf("%d", &n)) { int i = 0; int j = 0; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { if (i == j) printf("*"); else printf(" "); } printf("\n"); } } }
T8:
void D7_T8_way1(void) { int n = 0; while (~scanf("%d", &n)) { int i = 0; int j = 0; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { if (j + i == n - 1) { printf("*"); } else printf(" "); } printf("\n"); } } }
T9:
void D7_T9_way1(void) { int n = 0; while (~scanf("%d", &n)) { int i = 0; int j = 0; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { if (i == j || i + j == n - 1) printf("*"); else printf(" "); } printf("\n"); } } }
T10:
void D7_T10_way1(void) { int n = 0; while (~scanf("%d", &n)) { int i = 0; int j = 0; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { if (i == 0 || i == n-1) printf("* "); else if (j == 0 || j == n - 1) printf("* "); else printf(" "); } printf("\n"); } } }
T11:
void D7_T11_way1(void) { int n = 0; while (~scanf("%d", &n)) { int i = 0; int j = 0; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { if (j == 0 || i == n - 1 || i == j) printf("* "); else printf(" "); } printf("\n"); } } }
全篇代码展示:
#define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h> void D7_T1_way1(void) { int n = 0; while (scanf("%d", &n) != EOF) { //打印多少行 int i = 0; for (i = 0; i < n; i++) { int j = 0; //对每行打印什么进行控制 for (j = 0; j < n - i; j++) { printf("* "); } printf("\n"); } } } void D7_T2_way1(void) { int n = 0; while (~scanf("%d", &n)) { //打印多少行 int i = 0; for (i = 0; i < n; i++) { //对列进行控制 int j = 0; //对每行中的空格进行处理 for (j = 0; j < n - 1 - i; j++) { printf(" "); } //对每行的星号进行处理 for (j = 0; j < i + 1; j++) { printf("* "); } printf("\n"); } } } void D7_T3_way1(void) { int n = 0; while (~scanf("%d", &n)) { int i = 0; for (i = 0; i < n; i++) { int j = 0; for (j = 0; j < i ; j++) { printf(" "); } for (j = 0; j < n - i; j++) { printf("* "); } printf("\n"); } } } void D7_T4_way1(void) { int n = 0; while (~scanf("%d", &n)) { int i = 0; int j = 0; //上 for (i = 0; i < n; i++) { //上,空格 for (j = 0; j < n - i; j++) { printf(" "); } //上,* for (j = 0; j < i + 1; j++) { printf("* "); } printf("\n"); } //下 for (i = 0; i < n + 1; i++) { //下,空格 for (j = 0; j < i; j++) { printf(" "); } //下,* for (j = 0; j < n + 1 - i; j++) { printf("* "); } printf("\n"); } } } void D6_T5_way1(void) { int n = 0; while (~scanf("%d", &n)) { int i = 0; int j = 0; for (i = 0; i < n; i++) { for (j = 0; j < n - i; j++) { printf(" "); } for (j = 0; j < i + 1; j++) { printf("*"); } printf("\n"); } for (i = 0; i < n + 1; i++) { for (j = 0; j < i; j++) { printf(" "); } for (j = 0; j < n + 1 - i; j++) { printf("*"); } printf("\n"); } } } void D7_T5_way1(void) { int n = 0; while (~scanf("%d", &n)) { int i = 0; int j = 0; for (i = 0; i < n; i++) { for (j = 0; j < n+1-i; j++) { printf("* "); } printf("\n"); } for (i = 0; i < n + 1; i++) { for (j = 0; j < i + 1; j++) { printf("* "); } printf("\n"); } } } void D7_T7_way1(void) { int n = 0; while (~scanf("%d", &n)) { int i = 0; int j = 0; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { if (i == j) printf("*"); else printf(" "); } printf("\n"); } } } void D7_T8_way1(void) { int n = 0; while (~scanf("%d", &n)) { int i = 0; int j = 0; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { if (j + i == n - 1) { printf("*"); } else printf(" "); } printf("\n"); } } } void D7_T9_way1(void) { int n = 0; while (~scanf("%d", &n)) { int i = 0; int j = 0; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { if (i == j || i + j == n - 1) printf("*"); else printf(" "); } printf("\n"); } } } void D7_T10_way1(void) { int n = 0; while (~scanf("%d", &n)) { int i = 0; int j = 0; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { if (i == 0 || i == n-1) printf("* "); else if (j == 0 || j == n - 1) printf("* "); else printf(" "); } printf("\n"); } } } void D7_T11_way1(void) { int n = 0; while (~scanf("%d", &n)) { int i = 0; int j = 0; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { if (j == 0 || i == n - 1 || i == j) printf("* "); else printf(" "); } printf("\n"); } } } int main() { //D7_T1_way1(); //D7_T2_way1(); //D7_T3_way1(); //D7_T4_way1(); //D7_T5_way1(); //D7_T6_way1(); //D7_T7_way1(); //D7_T8_way1(); //D7_T9_way1(); //D7_T10_way1(); D7_T11_way1(); return 0; }
结语:
以上就是对鹏哥刷题训练营第七节课所有的代码笔记了~希望同学们还是自己动手敲一下。