题解 | #X形图案#
X形图案
https://www.nowcoder.com/practice/83d6afe3018e44539c51265165806ee4
#include <stdio.h> int main() { int n; while (scanf("%d", &n) != EOF) { int num; if (n % 2 == 0) { num = (n - 2) / 2; for (int i = 1; i <= num; i++) { for (int j = 1; j <= i - 1; j++) //输出首部i-1个空格 { printf(" "); } printf("*"); for (int k = 1; k <=n - 2-2*(i-1);k++) //输出中间n-2个空格 { printf(" "); } printf("*"); for (int j = 1; j <= i - 1; j++) //输出尾部i-1个空格 { printf(" "); } printf("\n"); } for(int count =1;count<=2;count++) { for(int l=1;l<=num;l++) { printf(" "); } printf("**"); for(int l=1;l<=num;l++) { printf(" "); } printf("\n"); } for (int i = 1; i <= num; i++) { for (int j = 1; j <= num - i; j++) //第i行首部输出num-i个空格 { printf(" "); } printf("*"); for (int k = 1; k <= n-2-2*(num-i);k++) //输出中间n-2-2*(num-i)个空格 { printf(" "); } printf("*"); for (int j = 1; j <= num - i; j++) //第i行尾部输出num-i个空格 { printf(" "); } printf("\n"); } } else { num = (n - 1) / 2; for (int i = 1; i <= num; i++) { for (int j = 1; j <= i - 1; j++) { //输出首部i-1个空格 printf(" "); } printf("*"); for (int k = 1; k <= n - 2-2*(i-1);k++) { //输出中间n-2*i个空格 printf(" "); } printf("*"); for (int j = 1; j <= i - 1; j++) { //输出尾部i-1个空格 printf(" "); } printf("\n"); } for(int l=1;l<=num;l++) { printf(" "); } printf("*"); for(int l=1;l<=num;l++) { printf(" "); } printf("\n"); for (int i = 1; i <= num; i++) { for (int j = 1; j <= num - i; j++) { //第i行输出首部num-i个空格 printf(" "); } printf("*"); for (int k = 1; k <= n-2-2*(num-i);k++) { //输出中间2*i个空格 printf(" "); } printf("*"); for (int j = 1; j <= num - i; j++) { //第i行输出尾部num-i个空格 printf(" "); } printf("\n"); } } } }
前100道题中最长的一段代码,修改的时间和次数都创下新高,主要是关系找不正确,其次是方法比较暴力,你就说A没A吧