สำหรับหมายเลขที่ให้มา ให้แสดงหรือพิมพ์ all possible combinations of strings ที่สามารถใช้ dial หมายเลขนั้นในโทรศัพท์ได้ ด้วย specifications ดังนี้
ในโทรศัพท์นี้ เราสามารถ dial
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
ผลลัพธ์
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
Time Complexity: Time complexity of above code be O(4^n) where n is number of digits in input number.