逐至 2021-04-12 11:58 采纳率: 100%
浏览 69
已结题

另一个c#编程题,请进链接查看文本,会的兄弟留下代码,输入输出得一致,感谢

链接: https://pan.baidu.com/s/14y0xTlugL3R9ubj-h7zMvg

提取码: qjvg 

  • 写回答

3条回答 默认 最新

  • soar3033 2021-04-12 14:16
    关注
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApp4
    {
        class Program
        {
            static void Main(string[] args)
            {
                auto.start();
                Console.ReadKey();
            }
        }
    
        class auto
        {
            public static void start()
            {
                char i1;
                int gens,count;
                count = 0;
                int[] start;
                int[] rules = new int[8];
                i1=(char)int.Parse(Console.ReadLine());
                gens = int.Parse(Console.ReadLine());
                string str = Console.ReadLine();
                start = new int[str.Length];
                for (int i = 0; i < str.Length; i++)
                {
                    if (str[i]=='-')
                    {
                        start[i]=0;
                    }
                    else
                    {
                        start[i]=1;
                    }
                }
                for (int i = 0; i < 8; i++)
                {
                    if ((i1&0x80)==0x80)
                    {
                        rules[i] = 1;
                    }
                    else
                    {
                        rules[i] = 0;
                    }
                    i1 <<= 1;
                }
                int[] gen = new int[start.Length];
                for (int i = 0; i < gens; i++)
                {
                    count++;
                    for (int j = 0; j < start.Length; j++)
                    {
                        int tmp = 0;
                        if (j==0)
                        {
                            tmp = start[start.Length - 1];
                        }
                        else
                        {
                            tmp = start[j - 1];
                        }
                        tmp <<= 1;
                        tmp |= start[j];
                        tmp <<= 1;
                        if (j==(start.Length-1))
                        {
                            tmp |= start[0];
                        }
                        else
                        {
                            tmp |= start[j + 1];
                        }
    
                        switch (tmp)
                        {
                            case 7:
                                gen[j] = rules[0];
                                break;
                            case 6:
                                gen[j] = rules[1];
                                break;
                            case 5:
                                gen[j] = rules[2];
                                break;
                            case 4:
                                gen[j] = rules[3];
                                break;
                            case 3:
                                gen[j] = rules[4];
                                break;
                            case 2:
                                gen[j] = rules[5];
                                break;
                            case 1:
                                gen[j] = rules[6];
                                break;
                            case 0:
                                gen[j] = rules[7];
                                break;
                        }
                    }
    
                    for (int k = 0; k < start.Length; k++)
                    {
                        start[k] = gen[k];
                    }
                    if ((count<=20)||(count>gens-20))
                    {
                        for (int k = 0; k < start.Length; k++)
                        {
                            if (start[k]==1)
                            {
                                Console.Write("x");
                            }
                            else
                            {
                                Console.Write("-");
                            }  
                        }
                        Console.Write('\n');
                    }
                    else if (gens>40 && count==21)
                    {
                        Console.WriteLine("...");
                    }
                }
            }
        }
    }
    

    如果不需要main函数,则复制下面的

    using System;
    class Program
    {
        public static void start()
        {
            char i1;
            int gens, count;
            count = 0;
            int[] start;
            int[] rules = new int[8];
            i1 = (char)int.Parse(Console.ReadLine());
            gens = int.Parse(Console.ReadLine());
            string str = Console.ReadLine();
            start = new int[str.Length];
            for (int i = 0; i < str.Length; i++)
            {
                if (str[i] == '-')
                {
                    start[i] = 0;
                }
                else
                {
                    start[i] = 1;
                }
            }
            for (int i = 0; i < 8; i++)
            {
                if ((i1 & 0x80) == 0x80)
                {
                    rules[i] = 1;
                }
                else
                {
                    rules[i] = 0;
                }
                i1 <<= 1;
            }
            int[] gen = new int[start.Length];
            for (int i = 0; i < gens; i++)
            {
                count++;
                for (int j = 0; j < start.Length; j++)
                {
                    int tmp = 0;
                    if (j == 0)
                    {
                        tmp = start[start.Length - 1];
                    }
                    else
                    {
                        tmp = start[j - 1];
                    }
                    tmp <<= 1;
                    tmp |= start[j];
                    tmp <<= 1;
                    if (j == (start.Length - 1))
                    {
                        tmp |= start[0];
                    }
                    else
                    {
                        tmp |= start[j + 1];
                    }
    
                    switch (tmp)
                    {
                        case 7:
                            gen[j] = rules[0];
                            break;
                        case 6:
                            gen[j] = rules[1];
                            break;
                        case 5:
                            gen[j] = rules[2];
                            break;
                        case 4:
                            gen[j] = rules[3];
                            break;
                        case 3:
                            gen[j] = rules[4];
                            break;
                        case 2:
                            gen[j] = rules[5];
                            break;
                        case 1:
                            gen[j] = rules[6];
                            break;
                        case 0:
                            gen[j] = rules[7];
                            break;
                    }
                }
    
                for (int k = 0; k < start.Length; k++)
                {
                    start[k] = gen[k];
                }
                if ((count <= 20) || (count > gens - 20))
                {
                    for (int k = 0; k < start.Length; k++)
                    {
                        if (start[k] == 1)
                        {
                            Console.Write("x");
                        }
                        else
                        {
                            Console.Write("-");
                        }
                    }
                    Console.Write('\n');
                }
                else if (gens > 40 && count == 21)
                {
                    Console.WriteLine("...");
                }
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 我下载图形界面重启完就变成这样了,打字也打不了,动也动不了,该怎么解决(操作系统-centos)
  • ¥15 VBA中在窗体中遍历所有checkbox控件,提取出被选中的checkbox的caption值
  • ¥15 在Ubuntu上有什么命令,或者是系统文件能告诉我链接nvme ssd的pcie槽位是不是支持热插拔功能?
  • ¥15 ansys license许可证问题
  • ¥20 QQ号和密码都能正常登录微信 QQ号和密码登录微信显示密码错误
  • ¥15 qiankun主应用注册子应用提示跨域
  • ¥15 单片机RTOS Kernel与应用分离开发,Kernel如何调起应用?
  • ¥15 快手小店商家版APP怎么第三方APP跳转到指定用户聊天界面
  • ¥15 为什么AVL fire软件仿真时的步长改变了,仿真结果也会改变
  • ¥100 如何帮我写一个java小游戏