PAT A 1069 The Black Hole of Numbers (20分)

一、思路

一些int和string转换的方法

1、string转int:
(1)atoi(char*)
(2)sscanf(char *, “%d”, &intager)从c字符串中读入整数;

2、int转string:
(1)to_string(int)
(2)sprintf(str, “%d”, intager)将整数输出至c字符串;

本体坑点总结:

1、题目给出的输入未必是四位整数,如不会给出0001而使直接给出1,因此注意用整数读入,或自己添0;
2、测试点5错误:输入6174的情况
3、同时注意输入0的情况

二、代码

#include <cstdio>
#include <algorithm>
#include <cstdlib>
using namespace std;
int main()
{
	int N;
	scanf("%d", &N);
	do
	{
		char temp[5];
		sprintf(temp, "%04d", N);
		sort(temp, temp + 4);
		N = atoi(temp);
		reverse(temp, temp + 4);
		printf("%s - %04d = %04d\n", temp, N, atoi(temp) - N);
		N = atoi(temp) - N;
	}while( N && N != 6174 );
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值