现在的位置: 首页 > 综合 > 正文

HDU-1279 模拟

2013年08月31日 ⁄ 综合 ⁄ 共 341字 ⁄ 字号 评论关闭

著名的3n+1问题,不能再简单。

/*
 * hdu-1279
 * mike-w
 * 2012-4-14
 */
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main(void)
{
	int N,first,ncase;
	scanf("%d",&ncase);
	while(ncase-->0)
	{
		scanf("%d",&N);
		first=1;
		while(N!=1)
		{
			if(N&0x1)
			{
				if(!first)
					putchar(' ');
				printf("%d",N),N=3*N+1,first=0;
			}
			else
				N/=2;
		}
		if(first)
			puts("No number can be output !");
		else
			putchar('\n');
	}
	return 0;
}

抱歉!评论已关闭.