该文章是算法设计与分析(第二版)中各章的课后习题 主编 : 李春葆
在本文中,主要是对该文第一章,绪论中的在线编程题进行代码解答。
代码运行环境是:DEVc++
问题一:
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
bool com(string &a,string &b){
return a.length() < b.length();
}
int main(){
string s[] = {"car","cats","carriage","doggies","koala"};
vector<string> a(s,s+5);
sort(a.begin(),a.end());
vector<string>::iterator it = a.begin();
cout<<"按照字典序排序:"<<endl;
for(it ; it < a.end();it++){
cout << *it << " ";
}
cout <<endl<<"--------------------------------"<<endl;
sort(a.begin(),a.end(),com);
//自定义比较函数,安装字符串长度排序
vector<string>::iterator it1 = a.begin();
cout<<"按照长度排序:"<<endl;
for(it1 ; it1 < a.end();it1++){
cout << *it1 << " ";
}
return 0;
}
代码运行截图:
问题二:
#include <iostream>
#include <vector>
#include <string>
#include <unordered_map>
#include <algorithm>
using namespace std;
int main(){
string a = "";
string b = "";
cout<<"请输入一个字符串:"<<endl;
char x ;
cin>>x;
//输入0时,表示字符

本文展示了几个关于算法和编程的实例,包括使用C++对字符串数组进行字典序和长度排序,删除指定字符的字符串操作,提取字符串中的星号及其数量,实现大整数乘法,以及判断旋转词。这些例子涵盖了基础数据结构、排序算法和字符串处理技巧。
最低0.47元/天 解锁文章
2795

被折叠的 条评论
为什么被折叠?



