PAT C#“非零返回”

PAT C#“非零返回”问题求解

using System;
class A
{
     
    public int setXin(int x)
    {
     
        int sum = 0;
        for (int i = 1; x-sum>4*i-2; i++)
        {
     
            sum = 2 * i * i - 1;
        }
        return sum;
    }
    public void writeXin(int x,char y)
    {
     
        double num1 = Math.Sqrt((x + 1) / 2);
        for (int i = 0; i < num1-1; i++)
        {
     
            for (int j = 0; j<i; j++)
            {
     
                Console.Write(" ");
            }
            for (int j = 0; j < 2*num1-2*i-1; j++)
            {
     
                Console.Write(y);
            }
            Console.Write('\n');
        }
        for (int i = 0; i < num1; i++)
        {
     
            for (int j = 0; j < num1-1-i; j++)
            {
     
                Console.Write(" ");
            }
            for (int j = 0; j < 2*i+1; j++)
            {
     
                Console.Write(y);
            }
            Console.Write('\n');
        }
    }
}
class Program 
{
     
    static void Main(string[] args)
    {
     
        A lou = new A();
        int a = int.Parse(Console.ReadLine());
        char b = char.Parse(Console.ReadLine());
        lou.writeXin(lou.setXin(a),b);
        Console.WriteLine(a - lou.setXin(a));
    }
}

#求解各位大佬

你可能感兴趣的:(PAT C#“非零返回”)