n a method for changing to while

Source: Internet
Author: User

The problem is as follows: The number keys on the phone correspond to several characters, for example, 2 corresponds to A,b,c. The problem is that when you enter a number, find out all possible combinations of characters, (you can imagine the situation when texting, whenever a few number keys, give the possible Hanyu Pinyin, of course, this requirement is higher, the topic only asked to give all possible combinations).
For example, after entering the 4,2 key, a ga,gb,gc,ha,hb,hc,ia,ib,ic combination is given.

Of course, for most people, it's just a few layers of loops, each of which traverses, inside out. If you have a 2-digit phone number, you can solve the problem with two for loops.

The code is as follows:

#include <iostream>using namespace std; #defineTELLENGTH 2//number of digits char c[10][10]={"", "", "ABC", "DEF", "GHI", " JKL "," MNO "," PQRS "," TUV "," WXYZ ",};int total[10]={0,0,3,3,3,3,3,4,3,4};//the number of letters per single digit void  telephone_number_ Word (int answer[],int number[]), void  telephone_number_word1 (int answer[],int number[]), void Main () {int answer[ tellength]={0};//the position in the character set corresponding to the phone number is an int number[tellength]={4,2};//two-digit telephone number, and the first bit is 4. The second place is 2telephone_number_word (answer, number);} void  Telephone_number_word (int answer[],int number[]) {for (answer[0]=0;answer[0]<total[number[0]];answer[0 ]++) {for (answer[1]=0;answer[1]<total[number[1]];answer[1]++) {for (int i=0;i<tellength;i++) {cout<<c[ number[i]][answer[i]]<< "";} Cout<<endl;}}}
But the number of phone numbers in different regions is different, and when the number of phone numbers has been upgraded, we need to modify the source code to add several for loops, which is a painful thing. Here we modify the above code to replace N for loop with while.

void  telephone_number_word1 (int answer[],int number[]) {while (1) {for (int i=0;i<tellength;i++) {cout<<c [Number[i]] [Answer[i];} Cout<<endl;int K=tellength-1;while (k>=0) {if (answer[k]<total[number[k]]-1)//Update the second number represents the different character position {answer[ K]++;break;} else{answer[k]=0;k--;}} if (k<0) {break;}}}



n a method for changing to while

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.