Computer >> 컴퓨터 >  >> 프로그램 작성 >> C++

C++의 4키 키보드

<시간/>

키보드를 사용하여 문자 'A'를 쓰려고 한다고 가정해 보겠습니다. 우리의 목표는 4개의 키만 사용하고 텍스트 필드에 최대 'A'를 쓰는 것입니다. 키는 'A', 'C', 'V', 'Ctrl'입니다.

최대 개수의 A를 작성하려면 Ctrl + A를 사용하여 모두를 선택하고 Ctrl + C를 사용하여 복사하고 Ctrl + V를 사용하여 붙여넣습니다.

따라서 입력이 키 입력 횟수가 7과 같으면 A를 세 번 누를 때 출력이 9가 됩니다.

그런 다음 Ctrl+A, Ctrl+C, Ctrl+V, Ctrl+V

이 문제를 해결하기 위해 다음 단계를 따릅니다. −

  • keyStrokes <=6이면

    • 리턴 키스트로크

  • n :=1에서 6에 대해 수행

    • 결과[n-1] :=n

  • n :=7의 경우 keyStrokes, 수행

    • 결과[n-1] :=0

    • 중단점의 경우:=n-3에서 1까지, 수행

      • curr :=(n – 중단점 - 1)*결과[중단점 - 1]

      • curr> result[n-1]이면

        • 결과[n - 1] :=curr

  • 결과[키스트로크 - 1]

예시

더 나은 이해를 위해 다음 구현을 살펴보겠습니다. −

#include<iostream>
using namespace std;
int keyNumbers(int keystrokes){ //find number of 'A's using 4 types of keys
   if (keystrokes <= 6) //if keystrokes are less than 7
      return keystrokes;
      int result[keystrokes]; //store intermediate results
      for (int n=1; n<=6; n++) //upto 6 keystrokes, we need that number of keystrokes for max
         result[n-1] = n;
      for (int n=7; n<=keystrokes; n++){ //for 7th to higher result[n-1] = 0; //initially store 0 as result
      for (int breakPoint=n-3; breakPoint>=1; breakPoint--){ //find breakpoint to select, copy and paste
         int curr = (n-breakPoint-1)*result[breakPoint-1];
         if (curr > result[n-1])
            result[n-1] = curr;
      }
   }
   return result[keystrokes-1];
}
int main(){
   int keystrokes;
   cout << "Enter Number of keystrokes: "; cin >> keystrokes;
   cout << "Maximum Number of A's with "<<keystrokes << " keystrokes
   is: "<< keyNumbers(keystrokes)<<endl;
}

입력

7

출력

Enter Number of keystrokes: Maximum Number of A's with 0 keystrokes is: 0