程序有时仅打印垃圾

时间:2018-06-20 18:58:57

标签: string recursion

#include<iostream>
#include<bits/stdc++.h>

using namespace std;

const char A[10][5]={ "", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};``


void rec_print(int arr[],int current,int str_length,char output[]){


    if(current==str_length){
        cout<<output<<" ";
        return;
    }
    int i;
    for(i=0;i<strlen(A[arr[current]]);i++){
        output[current]=A[arr[current]][i];
        rec_print(arr,current+1,str_length,output);
    }

}

void printString(int ar[],int l){

    char op[l];
    rec_print(ar,0,l,op);
    }

int main() {
    //code

    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        int arr[n];
        for(int i=0;i<n;i++){
            cin>>arr[i];
        }
        printString(arr,n);
    }

    return 0;
}

0 个答案:

没有答案