zoukankan      html  css  js  c++  java
  • XX面试题

    是一题笔试题,也没有什么难点,当时写这道题的时候太唐突了,
    今天回来再机子上重新完善完善了

    题面:请打印以下图形

       *
      ***
     *****
    *******
     *****
      ***
       *
     
    class Program
        {
            
    private const string flag = "*";
            
    private const char snull = ' ';
            
    private const int length = 7;

            
    /// <summary>
            
    /// 主函数
            
    /// </summary>
            
    /// <param name="args"></param>
            static void Main(string[] args)
            {
                
    //----------正向打印
                for (int i = 0; i <= length; i++)
                    
    if (i % 2 != 0)
                        PrintFlag(i);

                
    //-----------反向打印
                for (int i = length - 2; i > 0; i--)
                    
    if (i % 2 != 0)
                        PrintFlag(i);

                Console.Read();
            }

            
    /// <summary>
            
    /// 打印标记
            
    /// </summary>
            
    /// <param name="currentIndex">当前索引</param>
            static void PrintFlag(int currentIndex)
            {
                
    string t = string.Empty, s = string.Empty;
                
    for (int i = 0; i < currentIndex; i++)
                {
                    t 
    += flag;
                }
                s 
    = GetLocation(currentIndex, t);//获取字符串及所在位置
                Console.WriteLine(s);
            }

            
    /// <summary>
            
    /// 获取“标记”位置
            
    /// </summary>
            
    /// <param name="currentIndex">当前索引</param>
            
    /// <param name="flag">标记</param>
            
    /// <returns></returns>
            static string GetLocation(int currentIndex, string flag)
            {
                
    int totalcount = length;
                
    if (currentIndex == totalcount)
                    
    return flag;

                
    int startlocation = (totalcount - currentIndex) / 2;//中间位置
                string temp = flag.PadLeft(startlocation + currentIndex, snull);//前置空白
                temp = temp.PadRight(totalcount, snull);//后置空白
                return temp;
            }
        }




    如果有更好的方法,希望大家多补充哈
  • 相关阅读:
    适配器模式
    显示实现接口
    Mysql表引擎的切换
    Mysql事务隔离级别
    按照指定的格式解析字节数组
    委托和事件的简单实用
    C#压缩和解压缩字节(GZip)
    Mysql数据库批量添加数据
    常用的分页类
    保证依赖的服务已全部启动
  • 原文地址:https://www.cnblogs.com/wfcfan/p/1579758.html
Copyright © 2011-2022 走看看