C++PTA习题总结(三)

第九章 类库和C++的标准模板库STL

1.关于用string定义字符串,下列选项中错误的是__D__。

A. string s; s = “hello C++”;
B. string s = “hello C++”;
C. string s(“hello C++”);
D. string s[“hello C++”];
2.使用C++标准string类定义一个字符串,需要包含的头文件_B___。

A. string.h
B. string
C. cstring
D. stdlib.h

3.字符串替换
将文本文件中指定的字符串替换成新字符串。
由于目前的OJ系统暂时不能支持用户读入文件,我们编写程序从键盘输入文件中的内容,当输入的一行为end时,表示结束。end后面有两个字符串,要求用第二个字符串替换文本中所有的第一个字符串。
输入格式:
Xi’an Institute of Posts and Telecommunications is co-designed and implemented by the People’s Government of Shaanxi Province and the Ministry of Industry and Information Technology.
The Institute is located in Xi’an, a historic city in Northwest China, famous for its magnificent ancient culture.
end (表示结束)
Institute (第一个字符串,要求用第二个字符串替换)
University (第二个字符串)
输出格式:
Xi’an University of Posts and Telecommunications is co-designed and implemented by the People’s Government of Shaanxi Province and the Ministry of Industry and Information Technology.The University is located in Xi’an, a historic city in Northwest China, famous for its magnificent ancient culture.
输入样例:
Xi’an Institute of Posts and Telecommunications is co-designed and implemented by the People’s Government of Shaanxi Province and the Ministry of Industry and Information Technology.
The Institute is located in Xi’an, a historic city in Northwest China, famous for its magnificent ancient culture.
end
Institute
University

输出样例:
Xi’an University of Posts and Telecommunications is co-designed and implemented by the People’s Government of Shaanxi Province and the Ministry of Industry and Information Technology.The University is located in Xi’an, a historic city in Northwest China, famous for its magnificent ancient culture.

#include <iostream>
#include <string>
using namespace std;
int main() {
    string a, b, c, t;
    int found, i;
    getline(cin, a);
    while(1) {
        getline(cin, t);
        i = t.compare("end");
        if (i == 0)
            break;
        a += "\n";
        a += t;
    }
    a += '\n';
    cin >> b;
    cin >> c;
    found = a.find(b);
    while (found != -1) {
        a.replace(found, b.length(), c);
        found = a.find(b, found + 1);
    }
    cout << a;
    return 0;
}

4.求解给定字符串的前缀
求解给定字符串的前缀。
输入格式:
输入数目不定的多对字符串,每行两个,以空格分开。 例如:
filename filepath
Tom Jack
输出格式:
返回两个字符串的最大前缀,例如:
The common prefix is file
No common prefix
输入样例:
filename filepath
Tom Jack

输出样例:
The common prefix is file
No common prefix

#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main() {
    string s1, s2;
    char s[10000];
    int i;
    while(cin >> s1 >> s2) {
        int flag = 0;
        int len = s1.size();
        if (s2.size() < len)
            len = s2.size();
        for (i = 0; i < len; i++) {
            if (s1[i] == s2[i]) {
                s[i] = s1[i];
                flag = 1;
            } else {
                break;
            }
        }
        if (flag)
            cout << "The common prefix is " << s << endl;
        else
            cout << "No common prefix" << endl;
    }
    return 0;
}

5.学号解析
川师的学号的某些位有特殊的含义,如从2016110101中可以看出该学生为2016级,就读于11系,班级为1班。根据输入的学号,利用程序进行解析,输出对应的信息。
输入格式:
一个学号
输出格式:
相关信息
输入样例:
在这里给出一组输入。例如:
2016110101

输出样例:
在这里给出相应的输出。例如:
year:2016
department:11
class:01

#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main() {
    char a[100];
    cin >> a;
    cout << "year:";
    for (int i =0; i < 4; i++)
        cout << a[i];
    cout << endl;
    cout << "department:";
    for (int i = 4; i < 6; i++)
        cout << a[i];
    cout << endl;
    cout << "class:";
    for (int i = 6; i < 8; i++)
        cout << a[i];
    cout << endl;
    return 0;
}

6.分离目录路径和文件名
输入文件目录路径和文件名,要求分离成目录路径和文件名分别输出
输入格式:
例如:输入
c:\windows\winhelp.exe
输出格式:
c:\windows (目录路径)
winhelp.exe (文件名)
输入样例:
/usr/bin/man

输出样例:
/usr/bin
man

#include <iostream>
#include <string>
#include <cstring>
using namespace std;
void separate(const string &s) {
    size_t found = s.find_last_of("\\/");
    cout << s.substr(0, found) << endl;
    cout << s.substr(found + 1) << endl;
}
int main() {
    string s;
    getline(cin, s);
    separate(s);
}

7.验证手机号码(C++ Java)
某系统在新用户注册时必须输入手机号,为了提高系统效率,防止输错手机号,需要对手机号进行验证。 验证规则为: (1)长度为11位 (2)由数字0~9组成 (3)必须是1开头 以上3个条件同时满足,则验证通过,否则为不通过。
输入格式:
在一行中一个字符串,长度不超过50个字符。例如:
13802988920
输出格式:
如果验证通过则输出Yes,否则输出No。
输入样例:
13812345678

输出样例:
Yes

#include <iostream>
#include <cstring>
#include <string>
using namespace std;
int main()
{
 int i, j, tag=0, m;
 char a[50];
 gets(a);
 m = strlen(a);
 for(i = 0; i < m; i++) {
  if(! (a[i] >= '0' && a[i] <= '9')) {
   tag = 1;
   break;
  }
 }
 if (m == 11 && a[0] == '1' && tag == 0) {
  cout << "Yes" << endl;
 }
 else
        cout << "No" << endl;
 }

8.可以通过下标随机访问向量vector中的元素。T
9.当向量对象的内存用完之后,就会产生越界错误。F
10.若有下面的语句:

vector<int> v;
for (int i = 0; i < 4; i++)
    v.push_back(i + 1);
cout << v.size() << endl;

则

执行后程序的输出结果是D

A. 1
B. 2
C. 3
D. 4

11.设有定义 vector< string > v(10);执行下列哪条语句时会调用构造函数? C

A. v[0] += “abc”;
B. v[0] = “2018”;
C. v.push_back(“ZUCC”);
D. cout << (v[1] == “def”);

12.设有如下代码段:

std::map<char *, int> m;
const int MAX_SIZE = 100;
int main() {
    char str[MAX_SIZE];
    for (int i = 0; i < 10; i++) {
        std::cin >> str;
        m[str] = i;
    }
    std::cout << m.size() << std::endl;
}

读入10个字符串,则输出的 m.size() 为 B

A. 0
B. 1
C. 10

13.阅读程序并填空。
#include < iostream >
#include < cstdlib >
#include < map >
#include < string >
using namespace std;

class employee{
public:
employee(string name,string phoneNumber,string address){
this->name=name;
this->phoneNumber=phoneNumber;
this->address=address;
}
string name;
string phoneNumber;
string address;
};
int main()
{
map<int,employee*> employeeMap;
typedef pair<int,employee*>employeePair;
for(int employIndex=1001;employIndex<=1003;employIndex++){
char temp[10]; //临时存储单元
sprintf(temp,"%d",employIndex);//将转化为字符串存储在temp中
string tmp( temp ); // 通过temp构造string对象
employee* p= new employee(2分)(“employee-”+tmp,“85523927-”+tmp, “address-”+tmp);
employeeMap.insert(2分)(employeePair(employIndex,p));//将员工编号和员工信息插入到employeeMap对象中
}
int employeeNo=0;
cout<<“请输入员工编号:”;
cin>>employeeNo; // 从标准输入获得员工号
map<int, employee*>(2分) ::iterator it;
it=employeeMap.find(employeeNo); // 根据员工编号查找员工信息
if(it==employeeMap.end()(2分)){
cout<<“该员工编号不存在!”<<endl;
return -1;
}
cout<<“你所查询的员工编号为:”<<it ->first<<endl;
cout<<“该员工姓名:”<<it ->second->name<<endl;
cout<<“该员工电话:”<<it -> second -> phonrNumber(2分)<<endl;
cout<<“该员工地址:”<<it ->second->address<<endl;
return 0;
}

14.查找电话号码
文件phonebook1.txt中有若干联系人的姓名和电话号码。
高富帅 13312342222
白富美 13412343333
孙悟空 13512345555
唐三藏 13612346666
猪悟能 13712347777
沙悟净 13812348888
请你编写一个简单的通信录程序,当从键盘输入一个姓名时查找到对应的电话号码并输出。如果没找到则显示Not found.
由于目前的自动裁判系统暂时不能支持用户读入文件,我们编写程序从键盘输入文件中的姓名和电话号码,当输入的名字为noname时,表示结束。noname后面有一个名字,需要查找其对应的电话号码。
输入格式:
高富帅 13312342222
白富美 13412343333
孙悟空 13512345555
唐三藏 13612346666
猪悟能 13712347777
沙悟净 13812348888
noname (表示结束)
唐三藏 (需要查找此人的电话号码)
输出格式:
13612346666 (输出对应的电话号码)
输入样例:
白富美 13412343333
孙悟空 13512345555
唐三藏 13612346666
猪悟能 13712347777
沙悟净 13812348888
noname
白骨精

输出样例:
Not found.

#include <iostream>
#include <string>
#include <vector>
#include <map>
using namespace std;
int main() {
    map<string, string> m;
    string a;
    string b;
    while(1) {
        cin >> a;
        if (a == "noname")
            break;
        cin >> b;
            m.insert(pair<string, string>(a, b));
    }
    map<string, string> :: iterator p;
    string s;
    cin >> s;
    p = m.find(s);
    if (p != m.end())
        cout << p -> second << endl;
    else
        cout << "Not found." << endl;
    return 0;
}

15.姓名排序
从指定文本文件中读入若干学生姓名并按照拼音顺序排序后输出。
由于目前的OJ系统暂时不能支持用户读入文件和写文件,我们编写程序从键盘输入文件中的姓名,当输入的单词为end时,表示文件结束。将按照姓名拼音顺序排序后输出。
输入格式:
张三
李四
王五
马六
陈七
孙悟空
end
输出格式:
陈七 李四 马六 孙悟空 王五 张三
输入样例:
白富美
孙悟空
唐三藏
猪悟能
沙悟净
end

输出样例:
白富美 沙悟净 孙悟空 唐三藏 猪悟能

#include <iostream>
#include <string>
#include <set>
using namespace std;
int main() {
    string name;
    set<string> m;
    getline(cin, name);
    while (name != "end") {
        m.insert(name);
        getline(cin, name);
    }
    set<string> :: iterator p = m.begin();
    while (p != m.end())
        cout << *p++ << " ";
}

16.查找成绩并折算后输出
文件:期中考试成绩.txt中有若干学生的姓名和数学期中考试成绩。
Smith 67
Anderson 75
Lewis 83
Cook 58
David 96
请你编写一个简单的查询成绩程序,当从键盘输入一个姓名时查找到他的数学期中考试分数并按照21%折算后输出。如果没找到则显示Not found.
由于目前的OJ系统暂时不能支持用户读入文件,我们编写程序从键盘输入文件中的姓名和成绩,当输入的名字为noname时,表示结束。noname后面有一个名字,需要查找其成绩。
输入格式:
Smith 67
Anderson 75
Lewis 83
Cook 58
David 96
noname (表示结束)
Bill
输出格式:
Not found.
输入样例:
Smith 67
Anderson 75
Lewis 83
Cook 58
David 96
noname
Lewis

输出样例:
17.43

#include <iostream>
#include <string>
#include <map>
using namespace std;
int main() {
    map<string, int> m;
    string a;
    int b;
    while(1) {
        cin >> a;
        if (a == "noname")
            break;
        cin >> b;
        m.insert(pair<string, int>(a, b));
    }
    string s;
    cin >> s;
    map<string, int> :: iterator p = m.begin();
    p = m.find(s);
    if (p != m.end())
        cout << (p -> second) * 0.21 << endl;
    else
        cout << "Not found." << endl;
    return 0;
}

ps:其他的因为找不到上学期的c++题库,不能上传了

  • 9
    点赞
  • 69
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值