NYOJ_51 6174问题

题目地址

分析:

以后排序直接用库函数提供的sort很方便。
核心问题在于需要把一个数拆开排序。
其实在判断的时候直接判断6174即可,不需要判断是否和前一个数字相等。

心得:

水题需要锻炼自己的速度。

代码实现:

#include<iostream>
#include<algorithm>
using namespace std;

void ReverseMinus(int &a0)
{
	int temp1,temp2;
	int *t = new int [4];
	t[0] = a0/1000;
	t[1] = a0/100%10;
	t[2] = a0/10%10;
	t[3] = a0%10;
	//BubbleSort(t,4);
	sort(t,t+4);
	temp1 = t[0]+t[1]*10+t[2]*100+t[3]*1000;
	temp2 = t[3]+t[2]*10+t[1]*100+t[0]*1000;
	a0 = temp1 -temp2;
}
int main()
{
	int n;
	cin>>n;
	while(n--)
	{
		int a;
		cin>>a;
		int i=0;
		while(++i)
		{
			int a0= a;
			ReverseMinus(a);
			if(a0 == a)    //和前一个数不相等 
			{
				cout<<i<<endl;
				break;
			}
		}
	}
	return 0;
}

/*
void BubbleSort(int *a, int n) //冒泡排序 
{
	int i,j,last;
	i = n-1; //n-1趟
	while(i>0)
	{
		last = 0;
		for( j=0;j<i;j++)
			if(a[j+1]<a[j])
			{
				int temp;
				temp = a[j+1];
				a[j+1] = a[j];
				a[j] = temp;
				last =j;
			}
			i = last;	
	} 
}*/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值