📜  Project Euler 问题 13 - C# 代码示例

📅  最后修改于: 2022-03-11 14:48:50.311000             🧑  作者: Mango

代码示例1
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
public class Solution 
{
    static void Main(String[] args) 
    {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution */
        var n = Convert.ToInt32(Console.ReadLine());
        
        var str = string.Empty;
        // taking input 
        for(int i=0; i< n; i++)
        {
            str = str + Console.ReadLine() +" ";
        }
        var tenDigit = Function(str);
        Array.Reverse(tenDigit);
        var number =0;
        var flag =true;
        
        // displaying value
        //start 
        foreach( var z in tenDigit)
        {
            if(flag)
            {
                if(z!=0)
                {
                    flag = false;
                    Console.Write(z);
                }
            }
            else
            {
                 if(number< 9)
                 {
                    Console.Write(z);
                    number++;
                 }
                
            }
        }
        // end
            
    }
    static int[] strInt = new int[100];
    static int[] Function (string str)
    {
        var strArr = str.Split(' ');
        
        foreach(var i in strArr)
        {
            // converting string to integer array 
            // start
            var y = new int[i.Length];
            var m=i.Length-1;
            foreach( var sr in i)
                y[m--]=Convert.ToInt32(sr.ToString());
            // end
            
            
            for(var j=0; j < y.Length; j++)
            {
                strInt[j]+=y[j];
                if(strInt[j] > 9)
                  AddReminder(j);
            }
        }
        return strInt;   
    }
    static void AddReminder(int i)
    {
        // if less than 9 then its fine
        if(strInt[i] < = 9 )
            return;
        else if( strInt[i] > 9)
        {
            strInt[i]-=10;  // setting once place by substracting 
            strInt[i+1]+=1; // setting reminder to next position
        }
        AddReminder(i+1);
    }
}