• 字母图形


    题目如下:

    解法一:

     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了!!!!

  • 相关阅读:
    如何使用wei-ui框架,wei-ui框架有哪些组件
    微信小程序tabbar如何配置
    如何使用promise封装wx.request()、
    微信有哪些小程序内置组件,目录结构
    uni-app生命周期
    vue任意关系组件通信与跨组件监听状态 vue-communication
    vue父子组件状态同步的最佳方式续章(v-model篇)
    webstorm中emmet展开a标签和行内元素不换行的解决办法
    深入理解 vue 中 scoped 样式作用域的规则
    javascript事件环微任务和宏任务队列原理
  • 原文地址:https://www.cnblogs.com/ttpn2981916/p/6392240.html
Copyright © 2020-2023  润新知