poj1350

本文提供了一段用于解决特定数学问题的C++代码实现。该程序通过不断迭代直至找到问题的解,即数字6174,这被称为卡普雷卡尔常数。文章中包括了输入字符串的验证、排序和计算等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

简单题

ContractedBlock.gifExpandedBlockStart.gifView Code
#include <iostream>
#include
<cstdio>
#include
<cstdlib>
#include
<cstring>
#include
<algorithm>
using namespace std;

char st[100];

bool check()
{
for (int i = 1; i < 4; i++)
if (st[i] != st[i - 1])
return true;
return false;
}

int main()
{
//freopen("t.txt", "r", stdin);
while (scanf("%s", st) != EOF)
{
if (st[0] == '-')
break;
printf(
"N=%s:\n", st);
if (!check() || strlen(st) != 4)
{
printf(
"No!!\n");
continue;
}
int t = 0;
int len = 4;
while (1)
{
sort(st, st
+ len);
int x, y;
x
= y = 0;
for (int i = len - 1; i >= 0; i--)
x
= x * 10 + st[i] - '0';
for (int i = 0; i < len; i++)
y
= y * 10 + st[i] - '0';
printf(
"%d-%d=%d\n", x, y, x - y);
t
++;
if (x - y == 6174 || x - y == 0)
break;
x
= x - y;
len
= 0;
while (x > 0)
{
st[len]
= x % 10 + '0';
x
/= 10;
len
++;
}
}
printf(
"Ok!! %d times\n", t);
}
return 0;
}

转载于:https://www.cnblogs.com/rainydays/archive/2011/06/22/2087583.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值