小白书79页对字符串和字符的排序
来源:互联网 发布:淘宝保证金诈骗 编辑:程序博客网 时间:2024/05/22 20:21
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<cmath>
using namespace std;
int n;
char word[2000][10],sorted[2000][10];
int cmp_char(const void* _a,const void* _b)
{
char* a=(char*)_a;
char* b=(char*)_b;
return *a-*b;
}
int cmp_string(const void* _a,const void* _b)
{
char* a=(char*)_a;
char* b=(char*)_b;
return strcmp(a,b);
}
int main()
{
n=0;
for(;;)
{
scanf("%s",word[n]);
if(word[n][0]=='*')
break;
n++;
}
qsort(word,n,sizeof(word[0]),cmp_string);
for(int i=0;i<n;i++)
{
strcpy(sorted[i],word[i]);
qsort(sorted[i],strlen(sorted[i]),sizeof(char),cmp_char);
}
char s[10];
while(~scanf("%s",s))
{
qsort(s,strlen(s),sizeof(char),cmp_char);
int found=0;
for(int i=0;i<n;i++)
if(strcmp(sorted[i],s)==0)
{
found=1;
printf("%s ",word[i]);
}
if(!found) printf(":(");
printf("\n");
}
return 0;
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<cmath>
using namespace std;
int n;
char word[2000][10],sorted[2000][10];
int cmp_char(const void* _a,const void* _b)
{
char* a=(char*)_a;
char* b=(char*)_b;
return *a-*b;
}
int cmp_string(const void* _a,const void* _b)
{
char* a=(char*)_a;
char* b=(char*)_b;
return strcmp(a,b);
}
int main()
{
n=0;
for(;;)
{
scanf("%s",word[n]);
if(word[n][0]=='*')
break;
n++;
}
qsort(word,n,sizeof(word[0]),cmp_string);
for(int i=0;i<n;i++)
{
strcpy(sorted[i],word[i]);
qsort(sorted[i],strlen(sorted[i]),sizeof(char),cmp_char);
}
char s[10];
while(~scanf("%s",s))
{
qsort(s,strlen(s),sizeof(char),cmp_char);
int found=0;
for(int i=0;i<n;i++)
if(strcmp(sorted[i],s)==0)
{
found=1;
printf("%s ",word[i]);
}
if(!found) printf(":(");
printf("\n");
}
return 0;
}
关于sort函数以及qsort函数的具体差别网上博客说的也不是很清楚,此题本想改成sort的用法,能力不够,改不对,因此还是用qsort
0 0
- 小白书79页对字符串和字符的排序
- 对字符串中字符出现的次数进行排序
- 对字符的排序
- 对字符串中的字符进行自然排序
- java 对字符串中的字符自然排序
- 冒泡排序对字符串字符进行排序并输出
- 字符串的排序 接收一个字符串,对其中所有的字符窜进行排序,如:helloitcast,打印:acehillostt
- sort函数对数组和字符串的排序
- sort和qsort对字符数组排序
- sort和qsort对字符数组排序
- 以字符串中出现次数最多的字符的个数为权值,对一个字符串数组进行升序排序
- qsort()实现对字符数组的排序
- Oracle字符串字段内的字符排序
- Oracle字符串字段内的字符排序
- 3个字符的字符串排序
- SQL字符串字段内的字符排序
- 字符串字符由小到大的排序输出
- 我对字符数组/字符串/字符指针的理解!
- java--继承之--点,面,圆柱
- linux内存管理之malloc、vmalloc、kmalloc的区别
- 二项式系数点滴
- Dive into python第三章内置数据类型总结
- 如何判断链表有环并计算环的长度
- 小白书79页对字符串和字符的排序
- CodeVs 1011 数的计算
- HDU 3001 三进制状压DP
- c primer plus编程练习第十一章(纯手打欢迎指错)
- [Java大数+输入输出优化] hdu 5047 Sawtooth
- C++中宏定义与内联函数区别
- acdream 1412 2-3 Trees dp
- uva 724 Prime Ring Problem
- 快播网站最后,她打开厢房的窗户,把一盆浊水给倒进湖里