开始以为好难,会无限循环。后来发现题目中有一定会得到1这句话,那么就轻易AC了。好开心呐!~
CODE:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
const int maxn = 300001;
__int64 odd[maxn];
int tot;
void print(int n)
{
tot = 0;
if(n == 0)
{
printf("No number can be output !\n");
return ;
}
while(n!=1)
{
if(n&1)
{
odd[tot++] = n;
n = 3*n+1;
}
else
{
n = n/2;
}
}
if(tot == 0)
{
printf("No number can be output !\n");
}
else
{
for(int i = 0; i < tot ; i++)
{
printf(i!=tot-1?"%d ":"%d\n", odd[i]);
}
}
}
int main()
{
int T;
int n;
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
print(n);
}
return 0;
#include <stdlib.h>
#include <string.h>
using namespace std;
const int maxn = 300001;
__int64 odd[maxn];
int tot;
void print(int n)
{
tot = 0;
if(n == 0)
{
printf("No number can be output !\n");
return ;
}
while(n!=1)
{
if(n&1)
{
odd[tot++] = n;
n = 3*n+1;
}
else
{
n = n/2;
}
}
if(tot == 0)
{
printf("No number can be output !\n");
}
else
{
for(int i = 0; i < tot ; i++)
{
printf(i!=tot-1?"%d ":"%d\n", odd[i]);
}
}
}
int main()
{
int T;
int n;
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
print(n);
}
return 0;
}