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

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

提取码: qjvg 

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("...");
            }
        }
    }
}

 

using System;
using System.Collections;
 
public class Test
{
	public static int Main()
	{
	    // 读取数据
        String wolframStr = Console.ReadLine();
	    String timeStr = Console.ReadLine();
	    String cellStr = Console.ReadLine();
	    char[] cellCharArray = cellStr.ToCharArray();
	    int[] cells = new int[cellCharArray.Length];
	    for(int i=0;i<cellCharArray.Length;i++){
	        if(cellCharArray[i]=='-'){
	            cells[i]=0;
	        }else{
	            cells[i]=1;
	        }
	    }
	    int wolfram = int.Parse(wolframStr);
	    int time = int.Parse(timeStr);
	    
	    autoCell(wolfram,time,cells);
	    return 0;
	}
	
	public static void autoCell(int wolfram, int time, int[] cells) {
        int[] rules = new int[8];
        // 将wolfram转为数组存储
        for (int i = 0; i < 8; i++) {
            rules[i] = wolfram % 2;
            wolfram = wolfram / 2;
        }
        for (int i = 0; i < time; i++) {
            cells = newCellsFactory(cells, rules);
            if(time<40 || (time>40 && i < 20) || (time>40 && i >time-20)){
                for(int j = 0;j<cells.Length;j++){
                    if (cells[j]==0){
                        Console.Write("-");
                    } else{
                        Console.Write("x");
                    }
                }
                Console.WriteLine();
            }
            if(time>40 && i == 20){
                Console.WriteLine("...");
            }
            

        }
    }

    public static int[] newCellsFactory(int[] cells, int[] rules) {
        int[] newCells = new int[cells.Length];
        // 根据三位2进制数计算下标取值
        for (int i = 0; i < cells.Length; i++) {
            int index = 0;
            if (i == 0) {
                index = 4 * cells[cells.Length - 1] + 2 * cells[i] + cells[i + 1];
            } else if (i == cells.Length - 1) {
                index = 4 * cells[i - 1] + 2 * cells[i] + cells[0];
            } else {
                index = 4 * cells[i - 1] + 2 * cells[i] + cells[i + 1];
            }
            newCells[i] = rules[index];
        }
        return newCells;
    }
	
}

 

using System;
using System.Collections;
 
public class Test
{
	public static int Main()
	{
	    // 读取数据
        String wolframStr = Console.ReadLine();
	    String timeStr = Console.ReadLine();
	    String cellStr = Console.ReadLine();
	    char[] cellCharArray = cellStr.ToCharArray();
	    int[] cells = new int[cellCharArray.Length];
	    for(int i=0;i<cellCharArray.Length;i++){
	        if(cellCharArray[i]=='-'){
	            cells[i]=0;
	        }else{
	            cells[i]=1;
	        }
	    }
	    int wolfram = int.Parse(wolframStr);
	    int time = int.Parse(timeStr);
	    
	    autoCell(wolfram,time,cells);
	    return 0;
	}
	
	public static void autoCell(int wolfram, int time, int[] cells) {
        int[] rules = new int[8];
        // 将wolfram转为数组存储
        for (int i = 0; i < 8; i++) {
            rules[i] = wolfram % 2;
            wolfram = wolfram / 2;
        }
        for (int i = 0; i < time; i++) {
            cells = newCellsFactory(cells, rules);
            if(time<40 || (time>40 && i < 20) || (time>40 && i >time-21)){
                for(int j = 0;j<cells.Length;j++){
                    if (cells[j]==0){
                        Console.Write("-");
                    } else{
                        Console.Write("x");
                    }
                }
                Console.WriteLine();
            }
            if(time>40 && i == 20){
                Console.WriteLine("...");
            }
            

        }
    }

    public static int[] newCellsFactory(int[] cells, int[] rules) {
        int[] newCells = new int[cells.Length];
        // 根据三位2进制数计算下标取值
        for (int i = 0; i < cells.Length; i++) {
            int index = 0;
            if (i == 0) {
                index = 4 * cells[cells.Length - 1] + 2 * cells[i] + cells[i + 1];
            } else if (i == cells.Length - 1) {
                index = 4 * cells[i - 1] + 2 * cells[i] + cells[0];
            } else {
                index = 4 * cells[i - 1] + 2 * cells[i] + cells[i + 1];
            }
            newCells[i] = rules[index];
        }
        return newCells;
    }
	
}

一点点问题-20 变成-21就可以了