noip第10课作业

1.     统计不同类型字符出现次数

【问题描述】

输入一个字符串(假设长度不超过1000个字符),统计其中大写,小写,数字,其他字符出现的次数。

【样例输入】Hello,what are you doing 123?

【样例输出】1  19  3  6

#include<iostream>
#include<cstring>
using namespace std;
int main() {
    char a[1001];
    int dx = 0,xx = 0,sz = 0,qt = 0;
    int l, i;
    //输入字符串 
    cin.getline(a,1001);
    l = strlen(a);
    for(i = 0; i < l; i++) {
        if(a[i] >='A' && a[i] <='Z') {
            dx++;
        } else if(a[i] >='a' && a[i] <='z') {
            xx++;
        } else if(a[i] >='0' && a[i] <='9') {
            sz++;
        } else {
            qt++;
        }
    }
    cout << dx <<" " << xx <<" "<< sz<<" " << qt;
    return 0;
}

2.     删除数字字符,并统计删除的数字的个数

【问题描述】

从键盘输入一个由大、小写字母和数字组成的任意一个字符串(不需判断),其长度不小于 8,不大于 30。现要求将字符串

中的所有数字字符删除,其他字符依照原有顺序保持不变,并统计删除的数字的个数。

【输入文件]】 

只有一行,包含只由大、小写字母和数字组成的一个字符串(其长度 8≤L≤30)。

【输出文件]】

有两行:

第一行:为删除数字字符后的字符串;

第二行:为统计删除的数字的个数。

【要求】每行的输出数据从第一列输出

【样例输入】

ABCD123efg678

【样例输出】 

ABCDefg

6

#include<iostream>
#include<cstring>
using namespace std;
int main() {
    char a[31];
    int i,l,sum = 0;
    cin >> a;
    l=strlen(a);
    for(i=0; i<l; i++) {
        if(a[i]>='0' && a[i]<='9') {
            sum++;
        } else {
            cout << a[i];
        }
    }
    cout << endl;
    cout << sum;
    return 0;
}

1.     判断字符串是否为回文

【问题描述】

输入一个字符串,输出该字符串是否回文。回文是指顺读和倒读都一样的字符串。

输入为一行字符串(字符串中没有空白字符,字符串长度不超过100)。

如果字符串是回文,输出yes;否则,输出no。

【样例输入】

abcdedcba

【样例输出】

Yes

#include<iostream>
#include<cstring>
using namespace std;
int main() {
    char str1[101],str2[101];
    int i;
    long len;
    cin >> str1;
    len = strlen(str1);
    for(i=0; i<len; i++) {
        str2[i]=str1[len-1-i];
    }
    str2[i]='\0'; 
    if(strcmp(str1,str2)==0) {
        cout << "yes";
    } else {
        cout << "no";
    }
    return 0;
}

2.     石头剪子布

【问题描述】

石头剪子布,是一种猜拳游戏。起源于中国,然后传到日本、朝鲜等地,随着亚欧贸易的不断发展它传到了欧洲,到了近现代逐渐风靡世界。简单明了的规则,使得石头剪子布没有任何规则漏洞可钻,单次玩法比拼运气,多回合玩法比拼心理博弈,使得石头剪子布这个古老的游戏同时用于“意外”与“技术”两种特性,深受世界人民喜爱。

游戏规则:石头打剪刀,布包石头,剪刀剪布。

现在,需要你写一个程序来判断石头剪子布游戏的结果。

输入:输入包括N+1行:

第一行是一个整数N,表示一共进行了N次游戏。1 <= N <= 100。

接下来N行的每一行包括两个字符串,表示游戏参与者Player1,Player2的选择(石头、剪子或者是布):S1 S2

字符串之间以空格隔开S1,S2只可能取值在{"Rock", "Scissors", "Paper"}(大小写敏感)中。

输出:输出包括N行,每一行对应一个胜利者(Player1或者Player2),或者游戏出现平局,则输出Tie。

【样例输入】

3

Rock Scissors

Paper Paper

Rock Paper

【样例输出】

Player1

Tie

Player2

#include <iostream>
#include <string> 
#include <cstring>
using namespace std;
int main() {
    int N;
    string a, b;
    cin >> N;
    for (int i=0; i<N; i++) {
        cin >> a;
        cin >> b;
        if (a.compare("Rock") == 0) {
            if (b.compare("Rock") == 0) {
                cout << "Tie" << endl;
            } else if (b.compare("Scissors") == 0) {
                cout << "Player1" << endl;
            } else {
                cout << "Player2" << endl;
            }
        } else if (a.compare("Scissors") == 0) {
            if (b.compare("Rock") == 0) {
                cout << "Player2" << endl;
            } else if (b.compare("Scissors") == 0) {
                cout << "Tie" << endl;
            } else {
                cout << "Player1" << endl;
            }
        } else {
            if (b.compare("Rock") == 0) {
                cout << "Player1" << endl;
            } else if (b.compare("Scissors") == 0) {
                cout << "Player2" << endl;
            } else {
                cout << "Tie" << endl;
            }
        }
    }
    return 0;
}

3.     找第一个只出现一次的字符

【问题描述】

给定一个只包含小写字母的字符串,请你找到第一个仅出现一次的字符。如果没有,输出no。

输入:一个字符串,长度小于100000。

输出:输出第一个仅出现一次的字符,若没有则输出no。

【样例输入】

abcabd

【样例输出】

c

#include<iostream>
#include<cstring>
using namespace std;
int main() {
    //定义字符数组 
    char str[100001];
    //定义一个长度    定义存储每一个字符出现的次数的数组 
    int len,a[26]= {0};
    //输入字符串 
    cin >> str;
    //求字符串长度 
    len = strlen(str); 
    //abacad    a存每个字符的个数  a0++  2   a++  1 
    for(int i=0; i<len; i++) {
        a[str[i]-97]++;
    }
    for(int i=0; i<len; i++) {
        if(a[str[i]-97]==1) {
            cout << str[i];
            return 0;
        }
    }
    cout << "no";
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/zhplovelnn/p/10374206.html