蓝桥杯历年试题 java c组 等腰三角形

标题:等腰三角形

本题目要求你在控制台输出一个由数字组成的等腰三角形。
具体的步骤是:

  1. 先用1,2,3,…的自然数拼一个足够长的串

  2. 用这个串填充三角形的三条边。从上方顶点开始,逆时针填充。
    比如,当三角形高度是8时:

    1
    

    2 1
    3 8
    4 1
    5 7
    6 1
    7 6
    891011121314151

显示不正确时,参看:p1.png

输入,一个正整数n(3<n<300),表示三角形的高度
输出,用数字填充的等腰三角形。

为了便于测评,我们要求空格一律用"."代替。

例如:
输入:
5

程序应该输出:
…1
…2.1
…3…2
.4…1
567891011

再例如:
输入:
10

程序应该输出:
…1
…2.2
…3…2
…4…2
…5…1
…6…2
…7…0
…8…2
.9…9
1011121314151617181

再例如:
输入:
15

程序应该输出:

…1
…2.3
…3…2
…4…3
…5…1
…6…3
…7…0
…8…3
…9…9
…1…2
…0…8
…1…2
…1…7
.1…2
21314151617181920212223242526

资源约定:
峰值内存消耗(含虚拟机) < 256M
CPU消耗 < 1000ms

请严格按要求输出,不要画蛇添足地打印类似:“请您输入…” 的多余内容。

所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。
不要使用package语句。不要使用jdk1.7及以上版本的特性。
主类的名字必须是:Main,否则按无效代码处理。

import java.util.Scanner;

public class 等腰三角形2 {

public static void main(String[] args) {
	// TODO Auto-generated method stub

	Scanner ci=new Scanner(System.in);
	int h=ci.nextInt();
	ci.close();
	int t=4*(h-1);
	int [] a=new int[500];
	String b="";
	for(int i=0;i<500;i++) {
		a[i]=i+1;
		b+=String.valueOf(a[i]);
	}
	//System.out.println(b);
	char[] c=b.toCharArray();
	char[] d=new char[t];
	for(int i=0;i<d.length;i++) {
		d[i]=c[i];
		//System.out.print(d[i]);
	}
	//System.out.println();
	int count=0;
	char[][] z=new char[h][2*h-1];
	for(int i=0;i<h;i++) {
		for(int j=0;j<2*h-1;j++) {
			//if(j<h-i-1&&i<h-1) {
				//z[i][j]='*';
				//System.out.print(z[i][j]);
			//}
			if(j==h-i-1&&i<h-1) {
				z[i][j]=d[count];
				count++;
				//System.out.print(z[i][j]);
			}
			if(i==h-1) {
				z[i][j]=d[count];
				count++;
				//System.out.print(z[i][j]);
			}
		}
		//System.out.println();
	}
	for(int i=h-1;i>0;i--) {
		for(int j=0;j<2*h-1;j++) {
			if(j==h+i-1&&i>0&&i<h-1) {
				z[i][j]=d[count];
				count++;
				//System.out.print(z[i][j]);
			}
		}
		//System.out.println();
	}
	
	for(int i=0;i<h;i++) {
		for(int j=0;j<2*h-1;j++) {
			System.out.print(z[i][j]);
		}
		System.out.println();
	}
}

}

猜你喜欢

转载自blog.csdn.net/weixin_43557514/article/details/88733634