代碼如下:列印了很多變數,所以有點亂
#include<iostream>
#include<string.h>
using namespace std;
char *f ( int n )
{
char *ans=NULL;
char anw[100] = {NULL};
if ( n == 0 )//遞回出口
{
*anw = { '1' };
ans = anw;
return ans;
}
else
{
char *tem = f ( n - 1 );//調出上一個數列,即本次需要“數數”的數列
int len = strlen(tem);
/*cout << len;*/
char book=0;
int sum = 0;
char SUM;
for ( int i = 0; i < len; i++ )
{
if ( i == 0 )
book = tem[0];
if ( book == tem[i] )
{
sum++;
}
if ( book != tem[i] )
{
SUM = sum + '0';
char middle[3] = { SUM,book,'\0' };
strcat ( anw, middle );
/* cout << *anw << endl;*/
book = tem[i];
sum = 1;
}
}
SUM = sum + '0';
char middle[3] = { SUM,book,'\0' };
//cout << sum << " " << SUM << " " << book << endl;
//cout << *middle << endl;
//cout << *anw << endl;//檢查出此處是空
strcat ( anw, middle );
/*cout << anw[0] << anw[1];*///--11
/*puts ( middle );----->圖片輸出的自定義函式結果為這個
puts ( anw );*/
ans = anw;
return ans;
}
}
int main ()
{
int t, n;
cin >> t;
while ( t-- )
{
cin >> n;
char *ans = f ( n );
int i = 0;
puts ( ans );
}
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/230470.html
標籤:C語言
