int compare(char a,int lena, char b, int lenb)
{
//前态:(句子a,a的长度,句子b, b的长度)
//a大就返回1,a小就返回-1,相等返回0
int max;
if (lena < lenb)
max = lenb;
for(int i = 0; i < max; i++)
{
if(a[i] > b[i])
return 1;
if(a[i] < b[i])
return -1;
}
return 0;
}
int differ(char* a, int lena)//计算不同字符数
{
int q[21] = {0};//用于查重
int num = 0;
for(int i = 0; i < lena; i++)
{
int flag = 0;
for(int j = 0; j < lena; j++)
{
if (a[i] == q[j] )
flag = 1;
}
if (flag == 0)
num++;
q[i] = a[i];
}
return num;
}
#include<stdint.h>#include<stdio.h>#include<stdlib.h>#include<string.h>typedefuint32_tu32;typedefint32_ti32;typedefstruct{charstr[21];i32digit_cnt;}Rec;intcmp(voidconst*restrictlhs,voidconst*restrictrhs){return((Rec*)lhs)->digit_cnt==((Rec*)rhs)->digit_cnt?strcmp(((Rec*)lhs)->str,((Rec*)rhs)->str):((Rec*)rhs)->digit_cnt-((Rec*)lhs)->digit_cnt;}intmain(){u32t;scanf("%u",&t);for(u32i=0;i<t;++i){printf("case #%u:\n",i);u32n;scanf("%u",&n);Recrecs[100];for(u32i=0;i<n;++i){scanf("%s",recs[i].str);recs[i].digit_cnt=0;// 26 letters take one bit indivisually.u32bit_cnt=0;for(char*j=recs[i].str;*j;++j){u32bit=1<<(*j-'A');if((bit_cnt&bit)==0)bit_cnt|=bit,++recs[i].digit_cnt;}}qsort(recs,n,sizeof(Rec),cmp);for(u32i=0;i<n;++i)puts(recs[i].str);}}
各种睿智bug,写了我两个小时才ac
include
include
int compare(char a,int lena, char b, int lenb)
{
//前态:(句子a,a的长度,句子b, b的长度)
//a大就返回1,a小就返回-1,相等返回0
int max;
if (lena < lenb)
max = lenb;
for(int i = 0; i < max; i++)
{
if(a[i] > b[i])
return 1;
if(a[i] < b[i])
return -1;
}
return 0;
}
int differ(char* a, int lena)//计算不同字符数
{
int q[21] = {0};//用于查重
int num = 0;
for(int i = 0; i < lena; i++)
{
int flag = 0;
for(int j = 0; j < lena; j++)
{
if (a[i] == q[j] )
flag = 1;
}
if (flag == 0)
num++;
q[i] = a[i];
}
return num;
}
int main()
{
}
include
include
include
int main(){
int t,d=0;
scanf(“%d”,&t);
while(t>0){
int x[100],n,k,i,j;
scanf(“%d”,&n);
char str[100][20];
for(i=0;i<n;i++){
scanf(“%s”,str[i]);
x[i]=1;
for(j=1;j<strlen(str[i]);j++){
for(k=0;k<=j-1;k++){
if(str[i][j]==str[i][k]){
break;
}else if(k==j-1){
x[i]++;
}
}
}
}
}
合理组织结构可以方便操作。如果数据量再大一点,可以考虑对索引排序而不是直接排序(交换的代价较高)。
include
include
include
typedef struct{
char str[21];
int sum;
}data;
int cmp(const void a,const void b){
data s1=(data)a;
data s2=(data)b;
if(s1->sum==s2->sum)return strcmp(s1->str,s2->str);
else return s2->sum-s1->sum;
}
int main(){
int t;
scanf(“%d”,&t);
for(int wwh=0;wwh<t;wwh++){
int n;
scanf(“%d”,&n);
data ch=(data)malloc(n*sizeof(data));
for(int i=0;i<n;i++){
scanf(“%s”,ch[i].str);
int len=strlen(ch[i].str);
int code[26]={0};
for(int j=0;j<len;j++){
code[ch[i].str[j]-‘A’]++;
}
ch[i].sum=0;
for(int j=0;j<26;j++){
if(code[j]!=0)ch[i].sum++;
}
}
qsort(ch,n,sizeof(data),cmp);
printf(“case #%d:\n”,wwh);
for(int i=0;i<n;i++){
printf(“%s\n”,ch[i].str);
}
}
return 0;
}
哪位大佬可以说一下这个代码怎么才能显示正常
include
include
include
include
using namespace std;
struct word {
string s;
int num;
};
bool cmp(const word& a, const word& b) {
if (a.num != b.num)
return a.num > b.num;
return a.s < b.s;
}
int main() {
int t, count = 0;
cin >> t;
while (t–) {
int n;
cin >> n;
vector g(n);
for (int i = 0; i < n; i++) {
string temp;
cin >> temp;
set look;
for (int j = 0; j < temp.size(); j++)
look.insert(temp[j]);
g[i].s = temp;
g[i].num = look.size();
}
sort(g.begin(), g.end(), cmp);
cout << “case #” << count++ << “:\n”;
for (int i = 0; i < n; i++)
cout << g[i].s << endl;
}
return 0;
}
include
include
include
char alpha[26]= {‘A’,’B’,’C’,’D’,’E’,’F’,’G’,’H’,’I’,’J’,’K’,’L’,’M’,’N’,
‘O’,’P’,’Q’,’R’,’S’,’T’,’U’,’V’,’W’,’X’,’Y’,’Z’
};
void swap(char str1,char str2);
int main()
{
int m;
scanf(“%d”,&m);
for(int i1=0; i1<m; i1++)
{
int n;
scanf(“%d”,&n);
char str[n][30];
int con[n];
for(int c1=0; c1<n; c1++)
{
con[c1]=0;
}
}
include
include
include
typedef struct
{
char s[1001];
char c[1001];
int differ;
} Array;
int cmp(const voida, const voidb)
{
int r;
Arrayaa=(Array)a;
Arraybb=(Array)b;
return (r=bb->differ-aa->differ)?r:strcmp(aa->s,bb->s);
}
int cmp1(const voida,const voidb)
{
return (char)a-(char)b;
}
int main()
{
int T,t,n,i,j;
Array a[1001];
scanf(“%d”,&T);
for(t=0; t<T; t++)
{
}
测试数据都对,调试也没什么问题,就是一直WA,有没有大神能指导一下啊233
C 语言版本: