Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม C

สตริงทั้งหมดที่สามารถใช้หมุนหมายเลขใน C/C++ ได้หรือไม่


ในส่วนที่เกี่ยวกับหมายเลขที่กำหนด ให้แสดงหรือพิมพ์ชุดค่าผสมที่เป็นไปได้ทั้งหมดที่สามารถใช้เพื่อหมุนหมายเลขที่ระบุในโทรศัพท์ได้โดยใช้ข้อกำหนดต่อไปนี้

  • ในโทรศัพท์ที่กำหนด เราสามารถโทรออกได้

  • 2 การนำ A หรือ B หรือ C ไปใช้

  • 3 การนำ D หรือ E หรือ F ไปใช้

  • ……………….

  • 8 การนำ T หรือ U หรือ V

  • 9 ใช้ W หรือ X หรือ Y หรือ Z

  • 1 ดำเนินการเพียง 1

  • 0 กำลังดำเนินการ 0.

ตัวอย่างเช่น ถ้า 89 เป็นหมายเลขโทรศัพท์ที่กำหนด โปรแกรมควรพิมพ์

TW, TX, TY, TZ, UW, UX, UY, UZ, VW, VX, VY, VZ

#include <stdio.h>
#include <string.h>
// TableHash[i] stores all characters that correspond to digit i in phone const char TableHash[10][5] =
{"", "", "WXYZ", "TUV", "PQRS", "MNO", "GHI", "GHI", "DEF", "ABC"};
// A recursive function to display or print all possible words that can be obtained by implementing input number1[] of size n1.
The output words are one by one stored in output1[] void UtilWordsPrint(int number1[], int curr_digit1, char output1[], int n1) {
         // In the Base case, if current output word is prepared int i; if (curr_digit1 == n1) {
            printf("%s ",  
            output1); return ;
       }
   // We try all 3 possible characters for current digit in number1[]
   // and recur for remaining digits
   for (i=0; i<strlen(TableHash[number1[curr_digit1]]); i++) {
      output1[curr_digit1] =
      TableHash[number1[curr_digit1]][i];
      UtilWordsPrint(number1, curr_digit1+1, output1, n1);
      if (number1[curr_digit1] == 0 || number1[curr_digit1] == 1)
      return;
   }
}
// A wrapper over UtilWordsPrint(). It is able to create an output1 array and calls UtilWordsPrint() void printWords(int number1[], int n1) {
   char result1[n1+1];
   result1[n1] ='\0';
   UtilWordsPrint(number1, 0, result1, n1);
}
//Driver program int main(void) {
   int number1[] = {2, 3, 4};
   int n1 = sizeof(number1)/sizeof(number1[0]);
    printWords(number1, n1); return 0;
 }

ผลลัพธ์

WTP WTQ WTR WTS WUP WUQ WUR WUS WVP WVQ WVR WVS XTP XTQ XTR XTS XUP XUQ XUR XUS XVP XVQ XVR XVS YTP YTQ YTR YTS YUP YUQ YUR YUS YVP YVQ YVR YVS ZTP ZTQ ZTR ZTS ZUP ZUQ ZUR ZUS ZVP ZVQ ZVR ZVS

ความซับซ้อนของเวลา:ความซับซ้อนของเวลาของโค้ดด้านบน beO(4n) โดยที่ n ถือเป็นตัวเลขในตัวเลขอินพุต