C++程序练习-3729:用set实现字符串的排序和查找

描述
输入若干只包含数字的字符串,要求按它们被看作整数时的大小排序,并以从大到小的顺序输出。重复的字符串只能留下一个。

接下来,查找某些给定的字符串是否是前面输入的某个串。

如果两个字符串被看作整数时值相同,则短的排前面。

字符串最多有400个字符,但它被看作一个整数时,其值是不超过 int 的表示范围的(即可能有大量多余的前导0)

不考虑空串
输入
第一部分是若干待排序的字符串(不超过 30000行), 以一行"END"结束。

接下来是若干行要查找的字符串 ,同样以 "END" 结束

输出
先将第一部分的待排序字符串按前述规则排序输出,一个字符串一行
然后输出一行"END",表示以下是查找结果
对于每个待查找的字符串,输出一行查找结果。如果该字符串是第一部分中的一个,则查找结果为该字符串后跟着" found";
如果该字符串是第一部分中的一个,则查找结果为该字符串后跟着" not found";


样例输入
0000
000
01
02
1
21
00001234
01234
5678
1234
1234
21
END
5876
1234
123
2
02
0
END
样例输出
5678
1234
01234
00001234
21
02
1
01
000
0000
END
5876 not Found
1234 found
123 not Found
2 not Found
02 found
0 not Found


完了才发现没用Set...无语//http://poj.grids.cn/practice/3729/ //Accepted 0kB 0ms 2594 B G++ #include <iostream> #include <string> #include <algorithm> using namespace std; //Class definition:Numeral string typedef class NumeralString{ public: string numstr; int num; int len; void setnum(); void setlen(); }NumeralString; //set the sum of numeral string void NumeralString::setnum(){ int i,flag,len = this->numstr.length(); this->num = 0; flag = 0; for(i = 0;i < len;i ++){ if(this->numstr[i] != '0' || flag){ this->num = this->num * 10 + (this->numstr[i] - '0'); flag = 1; } } } //set the length of numeral strring void NumeralString::setlen(){ this->len = this->numstr.length(); } //sorted by sum des,len asc int comp(const void * x,const void * y){ NumeralString * elem1 = (NumeralString *)x; NumeralString * elem2 = (NumeralString *)y; if(elem1->num == elem2->num){ return (elem1->len - elem2->len); } else{ return (elem2->num - elem1->num); } } //global definition NumeralString NumStr[401]; int main(){ //variables definition NumeralString temp; int j,i = 0,output = 0,found = 0; //input while(std::cin>>temp.numstr){ if(temp.numstr.compare("END") == 0 && output == 0){ //output flag output = 1; //sort qsort(&NumStr,i,sizeof(NumeralString),comp); //output the sorted result std::cout<<NumStr[0].numstr<<std::endl; for(j = 1;j < i;j ++){ if(NumStr[j].numstr.compare(NumStr[j - 1].numstr) == 0){ continue; } std::cout<<NumStr[j].numstr<<std::endl; } std::cout<<"END"<<std::endl; continue; } //exit if(temp.numstr.compare("END") == 0 && output == 1){ break; } //continue to add the data if(output == 0){ temp.setnum(); temp.setlen(); NumStr[i ++] = temp; } //start to find the element in the data table if(output == 1){ found = 0; for(j = 0;j < i;j ++){ if(temp.numstr.compare(NumStr[j].numstr) == 0){ std::cout<<temp.numstr<<" found"<<std::endl; found = 1; break; } } if(!found){ std::cout<<temp.numstr<<" not found"<<std::endl; } } } return 0; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值