HDU1106 排序

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1106

看到那道题以前没过,就拿出来搞一搞,结果疯狂WA,看了讨论区的所有测试数据,完美通过,还测试了要不要去重。最后发现一个说如果字符长度为1,直接输出。我就没在意(因为不可能)。然后无奈的试了一下,AC。

这道题就是坑。

AC代码:

#include<iostream>
#include<queue>
#include<algorithm>
#include<stack>
#include<string>
#include<map>
#include<set>
#include<cstdio>
#include<cstdlib>
#include<cctype>
#include<cstring>

using namespace std;
const int MAX = 10010;
const int INF = 0X3f3f3f;

string s, temp;

vector<string> t;
vector<int> tt;

int main() {
	//freopen("in.txt", "r", stdin);
    while(cin >> s) {
    	if(s.length() == 1)//无解的特判 
    		cout << s << endl;
    	else {
	        for(int i = 0; s[i]; i++) {//遍历一遍 
	            if(s[i] == '5' || i == s.length()-1) {//遇到5 和 处理最后一个字符 
	                if(i == s.length()-1 && s[i] != '5')//如果最后一个不是5 
	                    temp += s[i];
	                if(temp.size() != 0) {//分离的一个字符串 
	                    t.push_back(temp);
	                    temp.clear();
	                    continue;
	                }
	            } else {
	                temp += s[i];
	            }
	        }
	        for(int i = 0; i < t.size(); i++) {//结果 
	            long long x = atoi(t[i].c_str());
	            tt.push_back(x);
	        }
	        sort(tt.begin(), tt.end());//排序 
	        for(int i = 0; i < tt.size(); i++) {
	            if(i == 0)
	                cout << tt[i];
	            else
	                cout << " " << tt[i];
	        }
	        cout << endl; 
	        temp.clear();
	        tt.clear();
	        t.clear(); 
	    }
    }
    return 0;
}

然后了解到了一个新的函数   strtok()  功能强大

AC代码:

#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <string>
#include <bitset>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#include <sstream>
#include <stack>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define FO freopen("in.txt", "r", stdin);
#define lowbit(x) (x&-x)
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const ll mod=1000000007;
const int inf = 0x3f3f3f3f;
ll powmod(ll a,ll b) {ll res=1;a%=mod;for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
template <class T>
inline bool scan_d(T &ret)
{
    char c; int sgn;
    if (c = getchar(), c == EOF) return 0; //EOF
    while (c != '-' && (c < '0' || c > '9')) c = getchar();
    sgn = (c == '-') ? -1 : 1;
    ret = (c == '-') ? 0 : (c - '0');
    while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0');
    ret *= sgn;
    return 1;
}
inline void out(ll x)
{
    if (x > 9) out(x / 10);
    putchar(x % 10 + '0');
}

const int maxn = 1010;
char s[maxn];
int a[maxn], ans;
char *p;
int v;

int main() {
	while(gets(s)) {
		ans = 0;
		p = strtok(s, "5");//(地址,"分隔符") 
		while(p){w
			sscanf(p, "%d", &v);//把 p 写入 v 
			a[ans++] = v;
			p = strtok(NULL, "5");//第二次调用NULL 
		}
		sort(a, a+ans);
		rep(i, 0, ans) {
			if(i == 0)
				printf("%d", a[i]);
			else 
				printf(" %d", a[i]);
		}
		printf("\n");
	}
}

猜你喜欢

转载自blog.csdn.net/qq_40932661/article/details/81257328