zoukankan      html  css  js  c++  java
  • 字母图形

    题目如下:

    解法一:

     1 import java.util.Scanner;
     2 class test
     3 {
     4     public static void main(String[] args) 
     5     {
     6         int n,m;
     7         int i,h;
     8         int num=0;
     9         Scanner In=new Scanner(System.in);
    10         
    11         System.out.printf("输入n=");
    12         n=In.nextInt();
    13         System.out.printf("输入m=");
    14         m=In.nextInt();
    15 
    16         for(i=0;i<n;i++)  //输出n行,以65为界分前后半部分输出
    17         {
    18            for(h=i,num=0;h>0 && num<m;h--,num++)//前部分输出,其中num控制行数小于m,针对的是n<m
    19            {                                    
    20                System.out.printf("%c",65+h);
    21            }
    22 
    23            for(h=0;h<m-i;h++)                  //后部分输出,当h<=m时,后半部分必有输出
    24                System.out.printf("%c",65+h);
    25            System.out.printf("
    ");
    26         }
    27     }
    28 }

    解法二:

     1 import java.util.Scanner;
     2 class test
     3 {
     4     public static void main(String[] args) 
     5     {
     6         int i,j,h;
     7         int n,m;
     8         char ch='A';
     9         char[][] str=new char[26][26];//把全部情况存放在二维数组,然后输出相应的行和列
    10         
    11         Scanner In=new Scanner(System.in);
    12         System.out.printf("输入n=");
    13         n=In.nextInt();
    14         System.out.printf("输入m=");
    15         m=In.nextInt();
    16 
    17         for(i=0;i<26;i++,ch='A')
    18         {   
    19            str[i][i]=ch;   //A总是在对角线的位置,以A的位置向数组两边递增
    20            for(j=i+1,h=i-1;j<26 || h>=0;j++,h--)          
    21            {   
    22                ch++;
    23                if(j<26)
    24                    str[i][j]=ch;
    25                if(h>=0)
    26                {         
    27                    str[i][h]=ch;
    28                }
    29            }
    30         }
    31         for(i=0;i<n;i++)  //打印结果
    32         {
    33             for(j=0;j<m;j++)
    34             {
    35                System.out.printf("%c",str[i][j]);
    36             }
    37             System.out.printf("
    ");
    38         }
    39     }
    40 }

    解法三:

     1 import java.util.Scanner;
     2 class test 
     3 {
     4     public static void main(String[] args) 
     5     {
     6         char[][]str=new char[26][26];
     7         int n,m;
     8         int i,j,h;
     9         char ch='A';
    10         
    11         Scanner In=new Scanner(System.in);
    12         System.out.printf("输入n=");
    13         n=In.nextInt();
    14         System.out.printf("输入m=");
    15         m=In.nextInt();
    16 
    17         for(i=0;i<n;i++)       //由于A所在的位置恰好可以代表行数i
    18         {                     //而每一行某位置的字符与A所在位置的差值有关
    19            for(j=0;j<m;j++)
    20            {
    21               h=j-i;
    22               if(h<0)
    23                   h=-h;
    24               str[i][j]=(char)(ch+h);
    25            }
    26         }
    27 
    28         for(i=0;i<n;i++)  //打印结果
    29         {
    30             for(j=0;j<m;j++)
    31             {
    32                System.out.printf("%c",str[i][j]);
    33             }
    34             System.out.printf("
    ");
    35         }
    36    }
    37 }

    说明一下,解法一是按自己的想法来写的,自己写完之后觉得过于一般且可读性不强,解法二和解法三是参照了网上的一些方法,然后自己稍稍优化了一下。SO,相比之下,自己写得太low了!!!!

  • 相关阅读:
    Mysql Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column To disable safe mode
    vs2012+ winform+.net4.0发布如何在xp上运行
    ubuntu下手动配置apache2.4.12
    mysql连接错误解决(ERROR 2049 (HY000): Connection using old (pre-4.1.1) authentication protocol ref used (client option 'secure_auth' enabled))
    位运算取绝对值
    位运算两数交换
    java mysql prepareStatement模糊查询like使用注意
    idea14远程调试linux下的tomcat
    web视频播放插件:Video For Everybody
    cmd杀死进程
  • 原文地址:https://www.cnblogs.com/ttpn2981916/p/6392240.html
Copyright © 2011-2022 走看看