1. 程式人生 > 其它 >試題 基礎練習 字母圖形

試題 基礎練習 字母圖形

技術標籤:java

試題 基礎練習 字母圖形

問題描述
利用字母可以組成一些美麗的圖形,下面給出了一個例子:

ABCDEFG

BABCDEF

CBABCDE

DCBABCD

EDCBABC

這是一個5行7列的圖形,請找出這個圖形的規律,並輸出一個n行m列的圖形。

輸入格式
輸入一行,包含兩個整數n和m,分別表示你要輸出的圖形的行數的列數。
輸出格式
輸出n行,每個m個字元,為你的圖形。
樣例輸入
5 7
樣例輸出
ABCDEFG
BABCDEF
CBABCDE
DCBABCD
EDCBABC
資料規模與約定
1 <= n, m <= 26。

import java.util.
Scanner; public class jichu3 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt();//行數 int m = sc.nextInt();//列數 char[][] pa=new char[26][26]; char str; int i,j; for(i = 0 ; i < n; i++) { str = 'A'; for(j = i ;j < m; j++) { pa[
i][j] = str++; } str = 'B'; for(j = i - 1 ;j >= 0; j--) { pa[i][j] = str++; } } for(i = 0;i < n;i++) { for(j = 0;j < m;j++) { System.out.print(pa[i][j]); } System.out.println(); } } }

第二種方法

	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++) { char c = (char)(Math.abs(i - j) + 'A'); System.out.print(c+" "); } System.out.println(" "); } }

第三種

public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc=new Scanner(System.in);
		int m=sc.nextInt();
		int n=sc.nextInt();
		int[][]A=new int[26][26];
	int i,l;
	for(i=0;i<m;i++) {
		for(l=0;l<n;l++) {
			if(i==l) {
				A[i][l]=65;
				}
			else if(i<l) {
				A[i][l]=A[i][l-1]+1;
			}
			else {
				A[i][l]=A[i-1][l]+1;
			}
			System.out.print((char)A[i][l]+" ");
		}
		
		System.out.print("\n");
	}
	
}