排序-HDU1106

本文详细介绍了如何使用C++的strtok函数来解决HDU1106题目,通过分割字符串并转换为整数进行排序,提供了完整的代码示例,展示了如何利用strtok函数对字符串进行有效处理。

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

在这里插入图片描述
hdu1106

思路:
运用c++所带strtok函数截取字符串;
转化为int再重新排序即可;
原型:char *strtok(char *s,const char *delim)
strtok()用来将字符串分割成一个个片段,参数s指向将要被分隔的字符串,参数delim则为分隔字符串,当strtok()在参数s的字符串中发现到参数delim的分隔字符是,则会将该字符改为 ‘\0’ 字符,在第一次调用时,strtok()必须给与参数s字符串,往后的调用则将参数s设置成NULL。每次调用成功则返回被分隔片段指针。

#include<bits/stdc++.h>
#include<time.h>
#include<cctype>
#include<iostream>
#include<ostream>
#include<cstdio>
#include<algorithm>
#include<iomanip>
#include<cstring>
#include<string>
#include<cmath>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;//1061109567
const int INF=0x7fffffff;//2147483647
const int SUP=0x80000000;//-2147483648
#define MAX 1000000
int main()
{
    char a[1005];
    int arr[1005];
    while(cin>>a)
    {
        int i;
        char *pos=strtok(a,"5");
        for(i=0;pos!=NULL;i++)
        {
            arr[i]=atoi(pos);
            //cout<<arr[i]<<endl;
            pos=strtok(NULL,"5");
        }
        sort(arr,arr+i);
        //cout<<i<<endl;
        cout<<arr[0];
        for(int j=1;j<i;j++)
            cout<<" "<<arr[j];
        cout<<endl;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值