题解 | #时间转换#
小乐乐改数字
http://www.nowcoder.com/practice/fcd30aac9c4f4028b23919a0c649824d
本次讲解两次方法,分别为 数组 和 递归
// 1.转换为数组输入输出
#include<iostream>
#include<math.h>using namespace std;
int main()
{
unsigned long a, sum = 0, arr[20] = { 0 };
int i;
cin >> a ;
for (i = 0; i < 20; i++)
{
if (a != 0)
{
arr[i] = a % 2;
a /= 10;
}
else
break;
}
for (i--; i >= 0; i--)
{
sum += arr[i]*pow(10,i);
}
cout << sum << endl;
system("pause");
return 0;
}
// 2.通过递归函数实现
#include<iostream>
using namespace std;
unsigned long set(unsigned long a, unsigned long sum = 0)
{
if (a != 0)
{
sum = a % 2;
sum += set(a / 10) * 10;
}
return sum;
}
int main()
{
unsigned long a, sum;
cin >> a;
sum = set(a);
cout << sum << endl;
system("pause");
return 0;
}