1. 程式人生 > 其它 >HDU1106字串排序題

HDU1106字串排序題

技術標籤:字串排序演算法

這個題使用strtok會更加的簡單,自己寫的有問題,這也是在看了大神也在使用這個函式,上網查了之後才通過的。
strtok(s,delim):分解字串為一組字串。s為要分解的字元,delim為分隔符字元(如果傳入字串,則傳入的字串中每個字元均為分割符)。
atof-把字串轉換成浮點數.

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<string.h>
using namespace std; typedef long long ll; const int maxx=1e3+10; char s[maxx]; struct node{ ll s; }num[maxx]; int cmp(node a,node b){ return a.s<b.s; } int main(){ while(cin>>s){ getchar(); int k=0; char *t; t=strtok(s,"5"); while(t){ num[k++].s=atof(t); t=strtok
(NULL,"5"); } if(k>0){ sort(num,num+k,cmp); for(int i=0;i<k-1;i++){ cout<<num[i].s<<" "; } cout<<num[k-1].s<<endl; } } return 0; }