输出图形

来源:互联网 发布:微信数据突然没有了 编辑:程序博客网 时间:2024/05/22 07:48

排版题:

第一类:规律性墙,自上而下,自左到右输出即可。

第二类:规律性无,先完成排版再输出!


第一类:


/*排版题:第一类:规律性墙,自上而下,自左到右输出即可。第二类:规律性无,先完成排版再输出!题目:输入一个高度h,输出一个高为h,上底边为h的梯形。输入:一个整数h(1<=h<=1000)。输出:h所对应的梯形。样例输入:4样例输出:      ****    ******  *******************/#include <stdio.h>int main(){int h;while (scanf("%d", &h) != EOF){int lenBottom, cns, cnb;lenBottom = h + (h-1) * 2;cns = h;for( int i = 0; i < h; i++){cnb = lenBottom - cns; //空白符数for (int j=1; i <= cnb; j++)printf(" ");for (int j=1; i <= cns; j++)printf("*");cns += 2; // 下一行星的书目}}return 0;}

第二类:

题目:http://ac.jobdu.com/problem.php?pid=1432
思想很重要:排版,输出!怎么排?怎么简单怎么排呗!
/*排版题:第一类:规律性墙,自上而下,自左到右输出即可。第二类:规律性无,先完成排版再输出!本题目为第二类!但是我还是不小心找到了规律,并且AC!WA:1、没有考虑每组之间有空格2、n = 1,是特例3、方案二中没有计算下一圈的参照坐标*/int max(int x, int y){return x > y ? x : y;}int Abs(int x){return x > 0 ? x : -x;}// 是否是边角,是用空格int isSpace(int x, int y, int n){return (x == 0 && y == 0) || (x == 0 && y == n-1) ||(x == n-1 && y == 0) ||(x == n-1 && y == n-1);}#include <stdio.h>int main (){#ifdef ONLINE_JUDGE#elsefreopen("E:\\in.txt","r",stdin);#endifint n; // n 为奇数char a, b;// a :中心图案char map[80][80]; bool firstCase = true;while (scanf("%d %c %c", &n, &a, &b) != EOF){if (firstCase == true){firstCase = false;}else{printf("\n"); //多组数据之间的空格}if (n == 1){printf("a\n");continue;} // 特例int c = n / 2;// (c,c)为中心坐标for (int i = 0; i < n; i++){for (int j = 0; j < n; j ++){if(isSpace(i,j,n))printf(" ");else{int level = max(Abs(i-c), Abs(j-c)) % 2;if (level == 0)printf("%c", a);// 同中心elseprintf("%c", b);// 不同} // if-else}// for :内printf("\n");//一行输出完毕}// for:外}// while : zureturn 0;}//--------------------  华丽丽的分割线  -------------------------------------// 排版的方案:从内而外,左上角的坐标为参照//#include <stdio.h>int main(){#ifdef ONLINE_JUDGE#elsefreopen("E:\\in.txt", "r", stdin);#endifint map[82][82];//char 也是可以的。左上角坐标为(1,1).右下角为(n,n)int n;char a, b;bool firstCase = true;while (scanf ("%d %c %c", &n, &a, &b) != EOF){if (firstCase == true){firstCase = false;}else{printf("\n"); //多组数据之间的空格}int o = n/2 + 1; // (o,o)为中心坐标int x, y; //每圈左上角的坐标x = o; // x向下增y = o; // y向右增for (int len = 1, j = 1; len <= n; len +=2, j++){ // j代表圈号,最内为1,len代表圈的边长,以2递增char c = (j%2) == 1 ? a : b; //本圈的图案for (int k=0; k < len; k++){map[x][y+k] = c; //upmap[x+len-1][y+k] = c; //bottommap[x+k][y]= c; // leftmap[x+k][y+len-1]= c; // right} // 填充本圈if (n != 1){map[1][1] = ' ';map[1][n] = ' ';map[n][1] = ' ';map[n][n] = ' ';}// n != 1, 四角为空x--;y--; //下一圈的参照坐标}// for: 每圈处理for (int i = 1; i <= n; i++){for(int j =1; j <= n; j++)printf("%c", map[i][j]);printf("\n");}} // whilereturn 0;}





















0 0
原创粉丝点击