String類函數的使用

String類函數的使用

一、CF74A-Room Leader

題目:

Sample Input:
5
Petr 3 1 490 920 1000 1200 0
tourist 2 0 490 950 1100 1400 0
Egor 7 0 480 900 950 0 1000
c00lH4x0R 0 10 150 0 0 0 0
some_participant 2 1 450 720 900 0 0

Sample Output:
tourist

Note:
The number of points that each participant from the example earns, are as follows:
Petr — 3860
tourist — 4140
Egor — 4030
c00lH4x0R —  - 350
some_participant — 2220
Thus, the leader of the room is tourist.

nn:namehackshackhack100hack50輸入n爲人的數量,接着輸入n行:人名name,hack成功的數量s,hack失敗的數量,五道題每道題對應的得分。\\其中hack成功得100分,hack失敗失去50分,輸出得分最多的人名。

注意點:

dataC++變量名不能取data,會與C++函數名衝突。


代碼:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define ull unsigned long long
#define inf 0x7fffffff
using namespace std;
const int N=55;
struct node
{
    string name;
    int s,u;
    int score[6];
    int sum;
};

node Data[N];

int main()
{
    int n;
    int ans=-inf;
    int pos=0;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>Data[i].name;
        scanf("%d%d%d%d%d%d%d",&Data[i].s,&Data[i].u,&Data[i].score[1],&Data[i].score[2],&Data[i].score[3],&Data[i].score[4],&Data[i].score[5]);
        Data[i].sum=Data[i].score[1]+Data[i].score[2]+Data[i].score[3]+Data[i].score[4]+Data[i].score[5]+100*Data[i].s-50*Data[i].u;
        if(Data[i].sum>ans)
        {
            ans=Data[i].sum;
            pos=i;
        }
    }

    cout<<Data[pos].name<<endl;
    return 0;
}

二、CF118A-String Task

題目:
:a,e,i,o,u,y(  !?   "y"???).三種操作:\\①、不分大小寫地刪除所有元音字母:a,e,i,o,u,y(\ \ !? \ \ \ 元音字母還有"y"???算了好好讀題。)\\②、其他所有字母之前加上'.'。\\③、大寫字母轉小寫字母。

Sample Input:
tour
Sample Output:
.t.r
Sample Input:
Codeforces
Sample Output:
.c.d.f.r.c.s
Sample Input:
aBAcAba
Sample Output:
.b.c.b

注意:
str.erase(i,len)ilen①、str.erase(i,len)函數—刪除從i開始長度爲len的子串,刪除後,後面的字符會遞補代替被刪除的位置。
str.insert(i,string str)istr       ,②、str.insert(i,string\ str)函數—在i之前插入字符串str,插入的參數是串,不是單個字符,\\\ \ \ \ \ \ \ 且插入後,後面的字符的下標會隨之增大,如果不跳過當前插入的字符,則會死循環。
tolower(char c)ctoupper(char c)c③、tolower(char\ c)將字符c轉化爲小寫字母,toupper(char\ c)將字符c轉化爲大寫字母。


代碼:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define ull unsigned long long
#define inf 0x7fffffff
using namespace std;


int main()
{
    string str;
    cin>>str;

    for(int i=0;i<str.size();i++)
    {
        if(str[i]=='a'||str[i]=='A'||str[i]=='e'||str[i]=='E'||str[i]=='i'||str[i]=='I'||str[i]=='o'||str[i]=='O'||str[i]=='u'||str[i]=='U'||str[i]=='Y'||str[i]=='y')  {str.erase(i,1);i--;}
        else
        {
            if(str[i]>='A'&&str[i]<='Z') str[i]=tolower(str[i]);
            str.insert(i,".");
            i++;
        }
    }
    cout<<str<<endl;
    return 0;
}

三、CF112A-Petya and Strings

題目:

ABA>B1A=B0A<B1不分大小寫地比較字符串A和字符串B,若A>B,輸出1;A=B,輸出0;A<B,輸出-1。

Sample Input:
aaaa
aaaA
Sample Output:
0
Sample Input:
abs
Abz
Sample Output:
-1
Sample Input:
abcdefg
AbCdEfF
Sample Output:
1

總結:

transform(s1.begin(),s1.end(),s1.begin(),::tolower);s1s1.compare(s2)s1s2s1>s21s1=s20s1<s21兩個函數:\\①、transform(s1.begin(),s1.end(),s1.begin(),::tolower);將s1全部轉化爲小寫。\\②、s1.compare(s2),字符串s1與s2區分大小寫比較,若s_1>s_2輸出1;s_1=s_2輸出0;s_1<s_2輸出-1。


代碼:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#define ull unsigned long long
#define inf 0x7fffffff
using namespace std;


int main()
{
    string s1,s2;
    cin>>s1>>s2;
    transform(s1.begin(),s1.end(),s1.begin(),::tolower);
    transform(s2.begin(),s2.end(),s2.begin(),::tolower);

    cout<<s1.compare(s2)<<endl;
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章