2990. 文献排序

shwei
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
void lowerword(string &s) {
        for (int i = 0; i < s.size(); i++) {
            s[i] = tolower(s[i]);
        }
}
bool cmp(string s1, string s2) {
    lowerword(s1), lowerword(s2);
    return s1 < s2; 
}
vector <string> words;
int main(void) {
    int T;
    cin >> T;
    int t = 0;
    while (T--) {
        int n;
        cin >> n;
        getchar();
        while (n--) {     
            string s;
            getline(cin, s);
            words.push_back(s);
        }
        sort(words.begin(), words.end(), cmp);

        cout << "case #" << t << ":" << endl;
        t++;
        for (auto x : words) {
            cout << x << endl;
        }      
        words.clear();
    }
    return 0;
}
10175101214

为什么不能用gets和strupr函数?

10175101282

Linux下gets因为安全性问题不被建议使用;strupr不属于c标准库,因此也不支持。

星子

strupr要自己写QAQ

10175101245

使用C++的algorithm中的transform可以实现字符串大小写转换。

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
class StrangeString
{
  private:
  public:
    string original;
    StrangeString() : original("")
    {
    }
    StrangeString(string s) : original(s)
    {
    }
    friend bool operator<(const StrangeString &left, const StrangeString &right)
    {
        string leftcpy = left.original;
        string rightcpy = right.original;
        transform(leftcpy.begin(), leftcpy.end(), leftcpy.begin(), ::tolower);
        transform(rightcpy.begin(), rightcpy.end(), rightcpy.begin(), ::tolower);
        return leftcpy < rightcpy;
    }
    friend istream &operator>>(istream &is, StrangeString &s)
    {
        getline(is, s.original);
        return is;
    }
    friend ostream &operator<<(ostream &os, const StrangeString &s)
    {
        os << s.original;
        return os;
    }
};
int main()
{
    int T;
    cin >> T;
    cin.get();
    for (int case_num = 0; case_num < T; case_num++)
    {
        cout << "case #" << case_num << ":" << endl;
        int n;
        cin >> n;
        cin.get();
        vector<StrangeString> arr(n);
        for (int i = 0; i < n; i++)
        {
            cin >> arr[i];
        }
        sort(arr.begin(), arr.end());
        for (int i = 0; i < arr.size(); i++)
        {
            cout << arr[i] << endl;
        }
    }
    return 0;
}

排序题,都可以创建类,重载<,用vector容器装起来然后sort解决。虽然代码看起来比较长,但整个写的过程几乎不需要思考和debug,事实上的开发效率更高。

我是一只咸鱼

include

using namespace std;

include

include

include

include

int cmp(const void a,const void b)
{
char s1,s2;
s1=(char)a;
s2=(char
)b;
char ch1,ch2;
while(1)
{
if(isalpha(s1))
ch1=toupper(
s1);
if(isalpha(s2))
ch2=toupper(
s2);
if(ch1!=ch2)
return ch1-ch2;
else
{
s1++;s2++;
}
}
}
int main(void)
{
int i1,T,N,i,j;
cin>>T;
for(i1=0;i1>N;
getchar();
char a[N][1001];
for(i=0;i<N;i++)
{
cin.getline(a[i],1001);
}
qsort(a,N,sizeof(a[0]),cmp);
cout<<”case #”<<i1<<”:”<<endl;
for(j=0;j<N;j++)
{
fputs(a[j],stdout);
cout<<endl;
}
}
return 0;
}

10175102219ww

忽然觉得结构体真好用!(C语言)

include

include

include

typedef struct
{
char num[201],tmp[201];
}T;

int cmp(const void a,const void b)
{
T x=(T )a,y=(T )b;
return strcmp(x->tmp,y->tmp);
}
int main()
{
int N,t,i,j,n;
char s[201],ans[201];
T ss[200];
scanf(“%d”,&N);
for(t=0;t<N;t++)
{
scanf(“%d\n”,&n);
for(i=0;i=’A’&&s[j]<=’Z’)
ans[j]=s[j]+32;
else
ans[j]=s[j];
}
ans[j]=’\0’;
strcpy(ss[i].num,s);
strcpy(ss[i].tmp,ans);
}
qsort(ss,n,sizeof(ss[0]),cmp);
printf(“case #%d:\n”,t);
for(i=0;i<n;i++)
printf(“%s\n”,ss[i].num);
}
return 0;
}

Li Dao

字符串排序,重新定义一下strcmp即可

include

using namespace std;
int T;
string strs;
vector V;

int cmp(const string& aa,const string& bb)
{
int ll=min(aa.length(),bb.length());
for(int i=0;i>n;
getline(cin,strs);
for(int i=1;i<=n;i++)
{
getline(cin,strs);
V.push_back(strs);
}
sort(V.begin(),V.end(),cmp);
for(int i=0;i<V.size();i++) cout<<V[i]<>T;
for(int step=0;step<T;step++)
{
printf(“case #%d:\n”,step);
solve();
}
return 0;
}

注意tolower是把字符变为小写
如果本来就是小写,就不变

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