您的位置:首页 > 其它

NYOJ-525 一道水题【模拟】

2012-04-11 15:32 281 查看
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=525

解题思路:

用到了字符串截取函数strtok

本来想用sscanf的正则表达式的,但是不会写。。。百度了一下,知道了大概,貌似不能对数字用。估计能把(没仔细找~~)

代码如下:

#include <iostream>
#include <cstring>
#include<stdio.h>
#include<string>
#include<algorithm>
using namespace std;

const int N = 5010;
char str
;
string ans
;

bool cmp(string a, string b)
{
	int lena = a.size(), lenb = b.size();
	if(lena != lenb)
		return lena < lenb;
	else
	{
		for(int i = 0; i < lena; ++i)
			if(a[i] != b[i])
				return a[i] < b[i];
	}
}

int main()
{
	int len, i, j, k, num;
	while(scanf("%s", str) != EOF)
	{
		num = 0;
		char *ptr=strtok(str,"5");
		while(ptr!=NULL)
		{
			string temp;
			len = strlen(ptr);
			for(i = 0; ptr[i] == '0'; ++i);
			if(i == len)
				ans[num++] = "0";
			else
			{
				for(j = i; j < len; ++j)
					temp.push_back(ptr[j]);
				ans[num++] = temp;
			}
			ptr=strtok(NULL,"5");
		}
		sort(ans, ans + num, cmp);
		for(int i = 0; i < num; ++i)
			cout<<ans[i]<<" ";
		cout<<endl;
	}
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: