2878. 字串排序

710698117
```c
#include<iostream>
#include<algorithm>
using namespace std;
struct node{
    string s;
    int num;
    int havenum;//有数字为0,没数字为1 
}A[110];
bool cmp(node x,node y){
    if(x.havenum!=y.havenum)
        return x.havenum<y.havenum;
    else{
        if(x.havenum==1&&x.num!=y.num){
                return x.num<y.num;
        }
        else
            return x.s<y.s;
    }
}
void setvalue(string s,node &a){
    a.s=s;
    a.num=0;
    a.havenum=0;
    for(int i=0;i<s.size();i++){
        if(s[i]>='0'&&s[i]<='9'){
            a.havenum=1;
            a.num=a.num*10+(s[i]-'0');
        }
    }
}
int main()
{
    string s;
    int k=0;
    while(cin>>s)
        setvalue(s,A[k++]);
    sort(A,A+k,cmp);
    for(int i=0;i<k;i++){
        cout<<A[i].s;
        if(i!=k-1)
            cout<<" ";
    }
    return 0;
}

```

10142510109

AC代码供参考

include

include

include

include

include

include

using namespace std;
string strs[105];
bool hasNum(string str)
{
for (int i = 0; i < str.length(); i++)
{
if (str[i] >= ‘0’ && str[i] <= ‘9’)
return true;
}
return false;
}
bool cmp(string a, string b)
{
if (hasNum(a) && !hasNum(b)) return false;
if (!hasNum(a) && hasNum(b)) return true;
if (!hasNum(a) && !hasNum(b)) return a < b;
int num1 = 0,num2 = 0;
for (int i = 0; i < a.size(); i++)
{
if (a[i] >= ‘0’ && a[i] <= ‘9’)
{
while (i < a.size() && a[i] >= ‘0’ && a[i] <= ‘9’)
{
num1 = num1 * 10 + (a[i] - ‘0’);
i++;
}
break;
}
}
for (int i = 0; i < b.size(); i++)
{
if (b[i] >= ‘0’ && b[i] <= ‘9’)
{
while (i < b.size() && b[i] >= ‘0’ && b[i] <= ‘9’)
{
num2 = num2 * 10 + (b[i] - ‘0’);
i++;
}
break;
}
}
if (num1 == num2) return a < b;
return num1 < num2;
}
int main()
{
int cnt = 0;
string str;
char c;
while (cin >> str)
{
strs[cnt++] = str;
if ((c = getchar()) == ‘\n’) break;
}
if (cnt == 1)
{
cout << str << endl;
return 0;
}
sort(strs,strs+cnt,cmp);
for (int i = 0; i < cnt-1; i++)
cout << strs[i] << ” “;
cout << strs[cnt-1] << endl;
return 0;
}

gongfunaicha

这个有什么特殊情况吗
数字大小相同时,仍然沿用原来的strcmp方式。我是没注意这个。。

10175102262 LarsPendragon

C的写法,调试的时候发现了许多漏洞,分享一下AC代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {char str[31]; int n;}S;
int cmp(const void *a, const void *b)
{
    S *p1=(S*)a, *p2=(S*)b;
    if(p1->n==p2->n) return strcmp(p1->str,p2->str);
    return p1->n-p2->n;
}
int main(void)
{
    char all[3005];
    S s[100]={0};
    gets(all);
    int cnt=0, number=0, i=0, j=0, check=0;
    while(all[j]!='\0')
    {
        if(all[j]==' ')
        {
            s[cnt].str[i]='\0';
            if(check==1)s[cnt].n=number;
            else {s[cnt].n=-1;}
            cnt++;
            number=0;
            i=0;
            check=0;
        }
        else
            s[cnt].str[i++]=all[j];
            if(all[j]>='0'&&all[j]<='9'){
                number=number*10+(all[j]-'0');
                check=1;}
        j++;
    }
    s[cnt].str[i]='\0';
    if(check==1)s[cnt].n=number;
    else {s[cnt].n=-1;}
    cnt++;
    qsort(s, cnt, sizeof(s[0]), cmp);
    for(i=0;i<cnt-1;++i)
        printf("%s ",s[i].str);
    printf("%s\n",s[i].str);
    return 0;
}
Li Dao

C++的写法,供大家参考

include

using namespace std;
string tmp;
vector V;

int q(string aa)
{
int ll=aa.length();
int isnum=0,pos;
for(int i=0;i>ret;
return ret;
}
}
int cmp(string aa,string bb)
{
int numa=q(aa),numb=q(bb);
if(numa!=numb) return numa>tmp) V.push_back(tmp);
sort(V.begin(),V.end(),cmp);
cout<<V[0];
for(int i=1;i<V.size();i++) cout<<’ ‘<<V[i];
cout<<endl;
return 0;
}

大鱼不吃鱼

不知道哪里有问题,第6个一直过不掉,有人帮忙看看么

include

include

include

include

include

using namespace std;
const int maxn=110;
string str[maxn];

bool strnumcmp (string str1,string str2)
{
int str1Num1=-1;
int str2Num1=-1;
int num1=0;
int num2=0;
int flag=0;//表示第一个数字没找到
for(int i=0;i=‘0’&&str1[i]<=‘9’)
{
if(flag==0)
{
str1Num1=i;
flag=1;//寻找最后一个数字下标
}
num1++;
}
}
flag=0;//表示第一个数字没找到
for(int i=0;i=‘0’&&str2[i]<=‘9’)
{
num2++;
if(flag==0)
{
str2Num1=i;
flag=1;//寻找最后一个数字下标
}
}
}
//两个都没有数字
if(str1Num1==-1&&str2Num1==-1)
{
return str1<str2;
}
//一个有数字,一个没有数字
else if(str1Num1!=-1&&str2Num1==-1)
{
return false;
}
else if(str1Num1==-1&&str2Num1!=-1)
{
return true;
}
//两个都有数字
else if(str1Num1!=-1&&str2Num1!=-1)
{
if(num1==num2)
{
int i=str1Num1;
int j=str2Num1;
while(i<(str1Num1+num1))
{
if(str1[i]!=str2[j])
{
return str1[i]<str2[j];
}
i++;
j++;
}
if(i==(str1Num1+num1))
{
return str1num2)
return false;
else return true;
}
}
}

int main()
{
int num=0;
while(cin>>str[num])
{
num++;
}
sort(str,str+num,strnumcmp);
for(int i=0;i<num;i++)
{
cout<<str[i];
if(i<(num-1))
{
printf(” “);
}
}
printf(“\n”);
return 0;

}

你当前正在回复 博客/题目
存在问题!