Project Euler #172: Investigating numbers with few repeated digits

  • + 1 comment

    my code work perfectly if k below 6... if the k is 7 and above, my C# code will not showing any result...i think is the max size of the data type problem...

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    
    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 */
            
            int[] m_t = Array.ConvertAll(System.Console.ReadLine().Split(' '), arTemp => Convert.ToInt32(arTemp));
            int numDigitRepeated = m_t[0];
            // numDigitRepeated--;
            int numInput = m_t[1];
            
            // System.Console.WriteLine(numDigitRepeated+" "+numInput);
            
            
            int numDigit = -1;
            for(int i=0;i<numInput;i++){
                numDigit = Convert.ToInt32(Console.ReadLine());
                // System.Console.WriteLine(numDigit);
                //numDigit++;
                int finalNumOfDigit = getCountNumOfDigit(numDigit);
                
                int temp = getCountNumOfDigit(numDigit-1);
                
                finalNumOfDigit = finalNumOfDigit - temp;//eg if 3 digit, range(100-999) = range(1-999) - range(1-10)
                //System.Console.WriteLine("finalNumOfDigit:"+finalNumOfDigit);
                
                if(numDigit > numDigitRepeated){
                    //3 > 1
                    updateRepeatedCountNumOfDigit(ref finalNumOfDigit, numDigitRepeated, numDigit);
                }
                System.Console.WriteLine("finalNumOfDigit:"+finalNumOfDigit);
            }
            
            
            
        }
        public static int getCountNumOfDigit(int n){
            String initDigit="";
            for(int w=0;w<n;w++) initDigit += "0";
    
            initDigit="1"+initDigit;
            int finalNumOfDigit = Int32.Parse(initDigit) - 1;
            
            return finalNumOfDigit;
        }
        
        public static void updateRepeatedCountNumOfDigit(ref int finalNumOfDigit, int digitRepeat, int totalNumDigit){
            digitRepeat++;
            
            
           // System.Console.WriteLine("digitRepeat:"+digitRepeat+",totalNumDigit:"+totalNumDigit+",aaaaaaaaa");
            int allNumOfDigit = getCountNumOfDigit(totalNumDigit);
    
    
            String initDigit="";
            for(int w=1;w<digitRepeat;w++) initDigit += "0";
            initDigit="1"+initDigit;
            int startDigit = Int32.Parse(initDigit);
    
            //System.Console.WriteLine("startDigit:"+startDigit+",allNumOfDigit:"+allNumOfDigit);
            for(int q=startDigit;q<=allNumOfDigit;q++){
    
                //if( Math.Floor(Math.Log10(q) + 1) >= digitRepeat ){
    
                    if(checkRepeatedNumber(q, digitRepeat)){
    
                        finalNumOfDigit--;
                    }
                //}
    
            }
                
            
            
        }
        public static bool checkRepeatedNumber(int q, int digitRepeat){
            
            String sss = q.ToString();
            // if(q==1000 ||q==2000|| q==3000 || q==4000|| q==5000|| q==6000|| q==7000|| q==8000 || q==9000)
            //     System.Console.WriteLine("sss:"+sss);
            bool isRepeated = false;
            
            //int[] arr1 = Enumerable.Range(1, 9).ToArray();
            
            
            char[] qqq={'1','2','3','4','5','6','7','8','9'};
            foreach(char qq in qqq){
                int countR = countFrom(sss,qq,0);//Convert.ToChar(1)
                //if(q==1111 ||q==2000|| q==2111) System.Console.WriteLine("countR:"+countR+",digitRepeat:"+digitRepeat);
                if(countR >= digitRepeat){
                    //System.Console.WriteLine("adddddddddd"+sss);
                    isRepeated = true;
                }
            }
            
            return isRepeated;
        }
        
        public static int countFrom(String str, char ch, int start) {
            int count = 0;
            for (int i = start; i < str.Length; i++) {
                int tt = str.IndexOf(ch,i,1);
                if(tt!=-1){
                    count++;
                }
            }
            return count;
        }
    }