[小算法] 使用递归将一个整数逆序放入一数组中
2011-08-23 15:19 Kevin Pan 阅读(474) 评论(0) 编辑 收藏 举报使用递归将一个整数逆序放入一数组中
#include "stdafx.h"
#include <iostream>
using namespace std;
void convert(int * result, int n);
int main(int argc, _TCHAR* argv[])
{
int n = 123456789, result[20] = {};
convert(result, n);
cout << "n = " << n << endl;
for(int i = 0; i< 9;i++)
{
cout << result[i];
}
cin >> n;
return 0;
}
void convert(int * result, int n)
{
if(n >= 10)
{
convert(result + 1, n / 10);
}
* result = n % 10;
}
#include <iostream>
using namespace std;
void convert(int * result, int n);
int main(int argc, _TCHAR* argv[])
{
int n = 123456789, result[20] = {};
convert(result, n);
cout << "n = " << n << endl;
for(int i = 0; i< 9;i++)
{
cout << result[i];
}
cin >> n;
return 0;
}
void convert(int * result, int n)
{
if(n >= 10)
{
convert(result + 1, n / 10);
}
* result = n % 10;
}