前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【蓝桥杯省赛】冲刺练习题【绘图】倒计时【10】天

【蓝桥杯省赛】冲刺练习题【绘图】倒计时【10】天

作者头像
红目香薰
发布2022-11-29 21:10:47
3330
发布2022-11-29 21:10:47
举报
文章被收录于专栏:CSDNToQQCode

目录

1、打印空心三角形

2、输出正反三角形

3、打印大X

4、字母图形

5、打印图形

6、打印十字图

1、打印空心三角形

输入一个数控制三角形的行数

代码语言:javascript
复制
         *
        * *
       *   *
      *     *
     *       *
    *         *
   *           *
  *             *
 *               *
*******************
代码语言:javascript
复制
package test;

import java.util.Scanner;

public class demo {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		double tmp = sc.nextInt(); // 如果是其他类型的数据,则改成相应的方法即可,如float类型,那么是nextFloat();
		int num = (int) tmp;
		for (int i = 1; i <= num; i++) { // 行

			for (int j = num - i; j > 0; j--) {// 列的空格和*
				System.out.print(" ");
			}
			for (int k = 1; k <= 2 * i - 1; k++) {
				if (k == 1 || k == 2 * i - 1 || i == num) {
					System.out.print("*");
				} else {
					System.out.print(" ");
				}

			}
			System.out.print("\n");
		}
	}
}

2、输出正反三角形

使用循环结构打印下述图形,打印行数n由用户输入。图中每行事实上包括两部分,中间间隔空格字符数m也由用户输入。 注意:两行之间没有空行。

样例输入

5 4

样例输出

代码语言:javascript
复制
    *    *********
   ***    *******
  *****    *****
 *******    ***
*********    *
代码语言:javascript
复制
package test;

import java.util.Scanner;

public class demo {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int m = sc.nextInt();
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++)
				System.out.printf(" ");
			for (int j = n - i; j > 1; j--)
				System.out.printf(" ");
			for (int k = 0; k <= i * 2; k++)
				System.out.printf("*");
			for (int j = 0; j < m; j++)
				System.out.printf(" ");
			for (int k = 1; k < (n - i) * 2; k++)
				System.out.printf("*");
			System.out.printf("\n");
		}
	}

}

3、打印大X

小明希望用星号拼凑,打印出一个大X,他要求能够控制笔画的宽度和整个字的高度。 为了便于比对空格,所有的空白位置都以句点符来代替。 

要求输入两个整数m n,表示笔的宽度,X的高度。用空格分开(0<m<n, 3<n<1000, 保证n是奇数)要求输出一个大X 

例如,用户输入:

代码语言:javascript
复制
3 9

程序应该输出:

代码语言:javascript
复制
***********
 *********
***********

用户输入

代码语言:javascript
复制
15 3

程序应该输出:

代码语言:javascript
复制
***           ***
 ***         ***
  ***       ***
   ***     ***
    ***   ***
     *** ***
      *****
       ***
      *****
     *** ***
    ***   ***
   ***     ***
  ***       ***
 ***         ***
***           ***
代码语言:javascript
复制
package test;

import java.util.Scanner;

public class demo {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int m = sc.nextInt();
		int n = sc.nextInt();
		sc.close();
		f(m, n);
	}

	static void f(int h, int w) {
		System.out.println(String.format("高度=%d, 笔宽=%d", h, w));
		int a1 = 0;
		int a2 = h - 1;

		for (int k = 0; k < h; k++) {
			int p = Math.min(a1, a2);
			int q = Math.max(a1 + w, a2 + w);

			for (int i = 0; i < p; i++)
				System.out.print(" ");

			if (q - p < w * 2) {
				for (int i = 0; i < q - p; i++)
					System.out.print("*");
				; // 填空
			} else {
				for (int i = 0; i < w; i++)
					System.out.print("*");
				for (int i = 0; i < q - p - w * 2; i++)
					System.out.print(" ");
				for (int i = 0; i < w; i++)
					System.out.print("*");
			}
			System.out.println();
			a1++;
			a2--;
		}
	}
}

4、字母图形

问题描述

利用字母可以组成一些美丽的图形,下面给出了一个例子:

ABCDEFG

BABCDEF

CBABCDE

DCBABCD

EDCBABC

这是一个5行7列的图形,请找出这个图形的规律,并输出一个n行m列的图形。

输入格式

输入一行,包含两个整数n和m,分别表示你要输出的图形的行数的列数。

输出格式

输出n行,每个m个字符,为你的图形。

样例输入

代码语言:javascript
复制
5 7

样例输出

代码语言:javascript
复制
ABCDEFG

BABCDEF

CBABCDE

DCBABCD

EDCBABC

数据规模与约定

1 <= n, m <= 26。

代码语言:javascript
复制
package test;

import java.util.Scanner;

public class demo {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();// 行数
		int m = sc.nextInt();// 列数
		char[][] pattern = new char[26][26];
		char str;
		int i, j;
		// 向pattern数组中添加字母元素
		for (i = 0; i < n; i++) {
			str = 'A';
			for (j = i; j < m; j++) {
				pattern[i][j] = str++;
			}
			str = 'A';
			for (j = i - 1; j >= 0; j--) {
				pattern[i][j] = ++str;
			}
		}
		// 输出字符数组元素
		for (i = 0; i < n; i++) {
			for (j = 0; j < m; j++) {
				System.out.print(pattern[i][j]);
			}
			System.out.println();
		}
	}

}

5、打印图形

如下的程序会在控制台绘制分形图(就是整体与局部自相似的图形)

当 n=1,2,3 的时候,输出如下:

请仔细分析程序,并填写划线部分缺少的代码

n=1 时:

代码语言:javascript
复制
 o 
ooo
 o 

n=2 时:

代码语言:javascript
复制
    o    
   ooo   
    o    
 o  o  o 
ooooooooo
 o  o  o 
    o    
   ooo   
    o    

n=3 时:

代码语言:javascript
复制
             o             
            ooo            
             o             
          o  o  o          
         ooooooooo         
          o  o  o          
             o             
            ooo            
             o             
    o        o        o    
   ooo      ooo      ooo   
    o        o        o    
 o  o  o  o  o  o  o  o  o 
ooooooooooooooooooooooooooo
 o  o  o  o  o  o  o  o  o 
    o        o        o    
   ooo      ooo      ooo   
    o        o        o    
             o             
            ooo            
             o             
          o  o  o          
         ooooooooo         
          o  o  o          
             o             
            ooo            
             o             

填空:

代码语言:javascript
复制
public class Main {

    static void show(byte[][] buf) {
        for (int i = 0; i < buf.length; i++) {
            for (int j = 0; j < buf[i].length; j++) {
                System.out.print(buf[i][j] == 0 ? ' ' : 'o');
            }
            System.out.println();
        }
    }

    static void draw(byte[][] buf, int x, int y, int size) {
        if (size == 1) {
            buf[y][x] = 1;
            return;
        }

        int n = ____________; // 填空
        draw(buf, x, y, n);
        draw(buf, x - n, y, n);
        draw(buf, x + n, y, n);
        draw(buf, x, y - n, n);
        draw(buf, x, y + n, n);
    }

    public static void main(String[] args) {
        final int N = 3;
        int t = 1;
        for (int i = 0; i < N; i++)
            t *= 3;

        byte[][] buf = new byte[t][t];
        draw(buf, t / 2, t / 2, t);
        show(buf);
    }
}

题解:

代码语言:javascript
复制
package test;

public class demo {

	static void show(byte[][] buf) {
		for (int i = 0; i < buf.length; i++) {
			for (int j = 0; j < buf[i].length; j++) {
				System.out.print(buf[i][j] == 0 ? ' ' : 'o');
			}
			System.out.println();
		}
	}

	static void draw(byte[][] buf, int x, int y, int size) {
		if (size == 1) {
			buf[y][x] = 1;
			return;
		}

		int n = size / 3; // 填空
		draw(buf, x, y, n);
		draw(buf, x - n, y, n);
		draw(buf, x + n, y, n);
		draw(buf, x, y - n, n);
		draw(buf, x, y + n, n);
	}

	public static void main(String[] args) {
		final int N = 3;
		int t = 1;
		for (int i = 0; i < N; i++)
			t *= 3;

		byte[][] buf = new byte[t][t];
		draw(buf, t / 2, t / 2, t);
		show(buf);
	}
}

完成: 

代码语言:javascript
复制
             o             
            ooo            
             o             
          o  o  o          
         ooooooooo         
          o  o  o          
             o             
            ooo            
             o             
    o        o        o    
   ooo      ooo      ooo   
    o        o        o    
 o  o  o  o  o  o  o  o  o 
ooooooooooooooooooooooooooo
 o  o  o  o  o  o  o  o  o 
    o        o        o    
   ooo      ooo      ooo   
    o        o        o    
             o             
            ooo            
             o             
          o  o  o          
         ooooooooo         
          o  o  o          
             o             
            ooo            
             o             

6、打印十字图

资源限制 时间限制:1.0s 内存限制:256.0MB

问题描述 小明为某机构设计了一个十字型的徽标(并非红十字会啊),如下所示:

对方同时也需要在电脑dos窗口中以字符的形式输出该标志,并能任意控制层数。

输入格式 一个正整数 n (n<30) 表示要求打印图形的层数。

输出格式 对应包围层数的该标志。

样例输入 1

样例输出

样例输入 3

样例输出

代码语言:javascript
复制
package test;

import java.util.Scanner;

public class demo {
	static char[][] arr;
	static int s;

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while (sc.hasNext()) {
			int n = sc.nextInt();
			s = 5 + n * 4;// 观察可得,输出的均是5+n*4的矩阵
			arr = new char[s][s];
			for (int i = 0; i < s; i++) {
				for (int j = 0; j < s; j++) {
					arr[i][j] = '.';// 初始化矩阵
				}
			}
			tian(0, s, 0);
			for (int i = 0; i < s; i++) {
				for (int j = 0; j < s; j++) {
					System.out.print(arr[i][j]);
				}
				System.out.println();
			}
		}
		sc.close();
	}
	// count是每次递归开始的横坐标,len是每次递归瘦两圈前的最大长度,bu是步数
	private static void tian(int count, int len, int bu) {
		if (bu > s / 4) { // 递归次数大于s/4就退出
			arr[s / 2][s / 2] = '$'; // 填补最中心的点
			return;
		}
		if (count != 0) {// 不是第一圈就填上四个角
			arr[count][count] = arr[count][len - 1] = arr[len - 1][count] = arr[len - 1][len - 1] = '$';
		}
		for (int i = count + 2; i < len - 2; i++) {
			// 每次递归改变的第一行
			arr[count][i] = arr[i][count] = arr[len - 1][i] = arr[i][len - 1] = '$';
			// 每次递归改变的第二行
			if (i == count + 2 || i == len - 3) {
				arr[count + 1][i] = arr[i][count + 1] = arr[len - 2][i] = arr[i][len - 2] = '$';
			}
		}
		// count+2,len-2起到瘦两圈的效果
		tian(count + 2, len - 2, bu + 1);
	}
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-03-30,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1、打印空心三角形
  • 2、输出正反三角形
  • 3、打印大X
  • 4、字母图形
  • 5、打印图形
  • 6、打印十字图
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档