PKU ACM 1016 Numbers That Count

题目链接:Numbers That Count


主要思路:

这题不涉及什么算法,只是简单的字符串处理。需要注意的地方就是最终答案的优先级和边界的处理。

一开始我用了 _itoa 函数,一直WA,很奇怪。后来自己动手写了整形到字符串的函数,终于才AC了。

看来有时候还是自己动手,丰衣足食啊偷笑

 

源代码:

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

void inttostr(int i, string& outstr)
{
	int number = 0;
	do
	{
		number = i % 10;
		outstr = (char)(number + '0') + outstr;
		i = i / 10;
	}while(i);
}

string GetInventory(string s)
{
	string inventory = "";
	string sn = "";
	string si = "";

	int i = 0;
	int board[10] = {0};
	for(i = 0; i < s.length(); i++)
	{
		board[s[i] - '0']++;
	}

	for(i = 0; i <= 9; i++)
	{
		sn = "";
		si = "";
		if(board[i] == 0)	continue;
		inttostr(board[i], sn);
		inttostr(i, si);
		inventory = inventory + sn + si;
	}

	return inventory;
}

int main()
{
	string result[17];
	int times;
	int i;

	while(true)
	{
		for(i = 0; i < 17; i++)
		{
			result[i] = "";
		}

		cin >> result[0];
		if(result[0] == "-1")	break;

		times = 1;
		while(times)
		{
			if(times > 15)
			{
				cout << result[0] << " can not be classified after 15 iterations " << endl;
				break;
			}

			result[times] = GetInventory(result[times - 1]);
			if(result[1] == result[0])
			{
				cout << result[0] << " is self-inventorying " << endl;
				break;
			}

			if(result[times] == result[times - 1])
			{
				cout << result[0] << " is self-inventorying after " << times - 1 << " steps" << endl; 
				break;
			}

			for(i = 0; i < times; i++)
			{
				if(result[i] == result[times])
				{
					cout << result[0] << " enters an inventory loop of length " << times - i << endl;
					break;
				}
			}

			if(i < times)	break;
			times++;
		}
	}
	
	return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值