728x90

728x90
728x90

728x90
728x90

문제

- (0,0) 부터 시작해서 아래 or 아래우측으로 이동 가능

- 만나는 수자는 모두 더함

- 더한 숫자가 최대가 되는 경로는? 

- 더한 숫자의 최대 합은?

 

 

728x90
728x90

728x90
728x90

문제

 We * love = CodelQ

 W = 7 e =4 l =3 o = 8 v =0 C =2 d =1 l =9 Q =6

 74*3804=281496

 

다음 식을 만족하는 숫자 대입 방법은 몇 가지 있는지 구해 보세요. (단 최상위 문자에 0은 들어가지 않고 다른 문자에는 다른 숫자가 대입됩니다)

 

READ + WRITE + TALK = SKILL

 

난이도

IQ 80

 

목표시간

20분

 

풀이(c#,VS)

1. 

-

 

문제정답(사용언어:python)

 

1.

  r e a d
w r i t e
  t a l k
s k i l l
w+1=s or w+2=s   e+a=8 or e+a=9 or e+a=10 a+t=8 or a+t=9 or a+t=10

(d+e+k)%10 = l

((a+t+l)*10+d+e+k)%100 = l*11

#풀이1
from itertools import permutations

count =0;
for (r,e,a,d,w,i,t,l,k,s) in permutations(range(10)):
    if r==0 or w==0 or t==0 or s==0:
        continue

    read = r*1000 + e*100 + a*10 +d
    write = w*10000 + r*1000 +i*100 + t*10 + e
    talk = t*1000 + a*100 + l*10 + k
    skill = s*10000 + k*1000 +i*100 + l*10 + l
    if read+write+talk==skill:
        count +=1

print(count)

#풀이2    디버깅이 안된다.. 예제 소스(책 소스랑 좀 다르다) 찾아서 받아서 돌려봤는대도 안된다..
import re
from itertools import permutations
from collections import OrderedDict

count =0

for(e,a,d,t,k,l) in permutations(range(0,10),6):
    if ((a+t==8) or (a+t==9) or (a+t==10)) and\
       ((a+e==8) or (a+e==9) or (a+e==10)) and\
       ((d+e+k)%10==1) and\
       (((a+t+l)*10+d+e+k)%100==l*11):
       
        temp = [item for item in range(0,10) if item not in [k,e,d,l,t,a]]
        for(i,r,s,w) in permutations(temp,4):
            if((r!=0) and (w!=0) and (t!=0)) and\
                ((s==w+1)or(s==w+2)):
                   
                    read = r*1000 + e*100 + a*10 +d
                    write = w*10000 + r*1000 +i*100 + t*10 + e
                    talk = t*1000 + a*100 + l*10 + k
                    skill = s*10000 + k*1000 +i*100 + l*10 + l
                    print("{}+{}+{}={}".format(read,write,talk,skill))
                    if read+write+talk==skill:
                        print("d{}+{}+{}={}".format(read,write,talk,skill))
                        count +=1

print(count)

 

책 제목 그대로... 코딩 퍼즐? 같은 느낌이다.. 쓰기 위한 코드? 알고리즘이라기 보단 다양한 방법으로 끼워 맞추는 놀이같은 문제다.. ㅜㅜ...

 

 

728x90
728x90

문제

 제곱근을 소수로 나타내었을 때 0~9의 모든 숫자가 가장 빨리 나타나는 최소 정수를 구하시오. 단, 여기서는 양의 제곱근만을 대상으로 합니다. 정수 부분을 포함하는 경우와 소수 부분만 취하는 경우 각각에 대해 모두 구하시오.

 

난이도

IQ 85

 

목표시간

20분

 

풀이(c#,VS)

1. 

using System;
using System.Linq;

namespace _12_SquareRoot
{
    class Program
    {
        static void Main(string[] args)
        {

            //정수포함           
            int targetNum = 2;
            while (true)
            {
                string sTargetNum = Math.Sqrt(targetNum).ToString().Replace(".", "");
                Console.Write(targetNum + "/" + Math.Sqrt(targetNum) + "/" + sTargetNum);
                if (StringDuplicationDelete(sTargetNum).Length == 10)
                {
                    Console.WriteLine(targetNum);
                    break;
                }
                targetNum++;
            }

            //소수포함           
            //int targetNum = 2;
            //while (true)
            //{
            //    string sTargetNum = string.Empty;
            //    try
            //    {
            //        sTargetNum = Math.Sqrt(targetNum).ToString().Split('.')[1];
            //        Console.Write(targetNum +"/" + Math.Sqrt(targetNum) + "/"+ sTargetNum);
            //    }
            //    catch
            //    {
            //        //split 에러
            //    }

            //    if (StringDuplicationDelete(sTargetNum).Length == 10)
            //    {
            //        Console.WriteLine(targetNum);
            //        break;
            //    }
            //    targetNum++;
            //}
        }

        public static string StringDuplicationDelete(string target)
        {
            string newString = string.Empty;

            for (int i=0; i< target.Length; i++)
            {
                if (!newString.Contains(target[i]))
                {
                    newString += target[i];
                }
               
            }

            Console.WriteLine(" /  " + newString);
            return newString;
        }
    }
}

 

문제정답(사용언어:python)

 

1.

from math import sqrt

# 정수 부분을 포함하는 경우
i =1
while True:
     i +=1
     # 소수점을 제거하고 왼쪽 10문자 추출
     string = '{:10.10f}'.format(sqrt(i)).replace('.','')[0:10]
     # 중복을 제거해서 10문자라면 종료
     if len(set(string)) == 10 :   
         break

print(i)

# 소수 부분만 계산하는 경우
i =1
while True:
     i +=1
     # 소수점을 분할하여 소수 부분만을 취득
     string = '{:10.10f}'.format(sqrt(i)).split('.')[1]
     # 중복을 제거해서 10문자라면 종료
     if len(set(string)) == 10 :
         break

print(i)

 

-

 

 

728x90
728x90

문제

 피보나치 수열 중 각 자리의 숫자를 더한 수로 나누어 떨어지는 수를 다음 예에 이어 작은 쪽부터 5개 구해보세요.

 예)

   2 > 2/2

   3 > 3/3

   5 > 5/5

   8 > 8/8

   13 > 13/4  x

   21 > 21/3

   144 > 144/9

 

난이도

IQ 85

 

목표시간

20분

 

풀이(c#,VS)

1. 

using System;

namespace _11_Fibonacci
{
    class Program
    {
        static void Main(string[] args)
        {
            long num1 = 1;
            long num2 = 2;
            for(long i =0; i<1000; i++)
            {
                //피보나치 수 구하기
                long pbNum;
                pbNum = num1 + num2;

                num1 = num2;
                num2 = pbNum;

                //자릿수 구하기
                long tempPbNum = pbNum;
                long sumNumLen = 0;
                do
                {
                    sumNumLen+= (long)(tempPbNum % 10);
                    tempPbNum = (long)(tempPbNum / 10);
                } while (tempPbNum > 0);
                   
                //자릿수 합으로 나누었을때 떨어지는 숫자 구하기
                if(pbNum%sumNumLen == 0f)
                {
                    Console.WriteLine(pbNum + "/" + sumNumLen);
                }

                
            }
        }
    }
}

 

문제정답(사용언어:python)

 

1.

a=b=1
count = 0
while count<11:
    c = a+b
    # 1자리씩으로 분할하여 각 자리의 합을 취득
    sum =0
    for e in str(c):
        sum +=int(e)
    if c%sum == 0:
        #나누어 떨어지면 출력
        print(c)
        count += 1
    a,b,=b,c

 

-

재귀를 이용해서 문제를 풀지 않았지만 책에서는 재귀를 이용하면 자칫 stackoverflow에 빠질 수 있다고 한다. 재귀가 끝나는 시점까지 호출 메소드관련 메모리가 스택에 계속 살아 있어야 되기 때문인것 같다. 간단하고 작은 수를 다룰 때는 재귀 나름의 장점이 있지면 역시 재귀는 쓰기전에 고려해볼 점이 많은것 같다.

 

728x90
728x90

문제

 남자 20명, 여자 10명이 도착하였을 때 어디에서 끊더라도 두 그룹 모두 남자와 여자의 수가 달라지게 되는 도착 순서가 몇 가지 있는지 구해 보세요.

 

난이도

IQ 80

 

목표시간

20분

 

풀이(c#,VS)

1. 재귀를 이용하여 풀이

        -       도착  
      -         - 도착
    -         -    
  -         -      
          -        

남성 10, 여성 5명, 남성 가로축, 여성 세로축 이라고 가정 했을때  - 부분을 지나지 않고 도착 경로에 도착할 수 있는 가짓수를 찾으면 된다.

/*
20200204
ksj
*/ 
using System;

namespace _9_MaleFemaleUnBalance
{
    class Program
    {
        static void Main(string[] args)
        {
            int cnt = 0;
            Move(1, 0, 0, ref cnt);
            //처음 위칸으로 이동하면 (여자가 먼저 도착하면) 절대로 타겟 경로로 이동할 수없다.
            //Move(-1, 0, 0, ref cnt);   // 이때 cnt는 0이다.

            Console.WriteLine(cnt);
        }

        static void Move(int dir, int x, int y, ref int cnt)
        {
            //dir  1 오른쪽한칸 x++
            //dir  -1 위쪽한칸 y++
            if (dir == 1)
            {
                x++;
            }
            else
            {
                y++;
            }
                        
            if ((x == y) ||( (x-10) == y) || x > 20 || y > 10)  //범위 벗어나거나 여자 남자 수가 같아지는 지점
            {
                return;
            }
            else if ((x == 19 && y == 10) || (x == 20 && y == 9)) //도착지
            {
                cnt++;               
            }
            else //새로운 이동 (위,아래)(여자,남자)
            {
                Move(1, x, y,ref cnt);
                Move(-1, x, y,ref cnt);
            }
        }
    }
}

 

문제정답(사용언어:python)

1. 최단 경로 문제

4 10 20 35
3 6 10 15
2 3 4 5

죄측 아래에서 우측 위 꼭지점 까지 가는 총 가짓수는 35가지이다.

해당 점까지 이동할때 이전 두점의 가짓수를 더한 값과 같다.

 

2. 2차원 배열을 1차원으로 표현함

boy, girl = 20, 10
boy, girl = boy+1, girl+1

arr = [0] * (boy*girl)
arr[0] = 1

#2차원 배열 형태를 arr[가로현재위치 + 가로최대크기 * 세로현재 위치]와 같이 1차원 형태로 표현 했다.
for g in range(0,girl):
    for b in range(0,boy):
        if(b!=g) and (boy-b != girl - g):
            if b>0:
                arr[b+boy*g] += arr[b-1+boy*g]
            if g>0:
                arr[b+boy*g] += arr[b+boy*(g-1)]

#-2 인덱스는 마지막 인덱스에서 가로로 -2칸이동을 의미
#-boy- 인덱스는 마지막 인덱스에서 가로 한줄 내려가고 가로로 -1칸 이동을 의미
print(arr[-2] + arr[-boy-1])

 

-

 

728x90

+ Recent posts