该文章是算法设计与分析(第二版)中各章的课后习题 主编 : 李春葆
在本文中,主要是对该文第一章,绪论中的在线编程题进行代码解答。
代码运行环境是: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时,表示字符串的末尾
while(x != '0'){
a += x;
cin>>x;
}
string s = "";
cout<<"请输入要删除的字符:"<<endl;
cin>>b;
//将要删除的元素保存到哈希表中
unordered_map<char,int> Ha;
for(auto h : b){
Ha.insert(make_pair(h,1));
}
//字符串C保存删除后的元素
cout<<"删除后的字符:" <<endl;
string c = "";
for(auto h :a){
if(Ha.find(h) == Ha.end()){
c += h;
}
}
cout << c;
return 0;
}
运行截图:
问题三:
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
int main(){
string s = "";
cout <<"请输入一个带*号的字符串:"<<endl;
cin>>s;
string front = "",back = "";
int k = 0;
for(auto i : s){
if(i == '*'){
front += i;
k++;
}else{
back += i;
}
}
cout <<"-----------------"<<endl;
front += back;
cout <<front<<endl;
cout <<k<<endl;
return 0;
}
运行截图:
问题四:
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
//【问题描述】有两个用字符串表示的非常大的大整数,算出它们的乘积,
//也用字符串表示.不能用系统自带的大整数类型
using namespace std;
string mul(string s1,string s2){
int len1 = s1.size() , len2 = s2.size();
//cout <<"len1 =" <<len1 <<" "<<"len2 = "<<len2<<endl;
int a[len1] , b[len2], i = 0;
for(auto h : s1){
a[len1 - i -1] = h - '0';
cout <<a[len1 - i -1] <<" ";
i++;
}
cout <<endl;
i = 0;
for(auto h : s2){
b[len2 - i -1] = h - '0';
cout <<b[len2 - i -1]<<" " ;
i++;
}
cout <<endl;
//初始化结果数组
int result[len1 + len2] = {0};
for(i = 0 ; i < len1 ;i++){
for(int j = 0 ; j < len2 ; j++){
result[i+j] += a[i] * b[j];
}
}
//进位处理
for(i = 0 ; i < len1 + len2 ;i++){
if(result[i] > 10){
int temp = result[i] / 10;
result[i] = result[i] % 10;
if(temp > 0){
result[i+1] += temp;
}
}
}
//求前导0个数
int pre = 0;
for(i = len1 + len2 - 1 ; i >= 0 ;i--){
if(result[i] == 0){
pre++;
}else{
break;
}
}
//将数组中的元素转换为字符串
string ansow = "";
cout<<endl;
for(i = len1 + len2 - pre - 1 ; i >= 0;i--){
int a = result[i];
ansow += a +'0';
}
return ansow;
}
int main(){
string s1 = "72106547548473106236",s2 = "982161082972751393";
//ansow = 70820244829634538040848656466105986748
string result = "";
result = mul(s1,s2);
cout <<result;
return 0;
}
运行截图:
问题五:
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
//【问题描述】如果字符串t是字符串s的后面若干个字符循环右
//移得到的,称s和 t是旋转词,例如"abcdef"和"efabcd"是旋转词,
//而"abcdef"和"feabcd"不是 旋转词
bool rotateString(string s, string goal) {
if(s.size() != goal.size()){
cout << s.size() << " "<< goal.size()<<endl;
return false;
}
s += s;
int a = s.find(goal);
if(a == string::npos){
return false;
}
return true;
}
int main(){
string s = "" , goal = "";
s += "abcdef";
goal += "feabcd";
cout<<s<<endl;
cout<<goal<<endl;
bool k = rotateString(s,goal);
if(k == 0){
cout <<"NO"<<endl;
}else{
cout <<"YES"<<endl;
}
return 0;
}
运行截图:
问题六:
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <unordered_map>
using namespace std;
int main(){
int time = 5;
int a[time] = {1,2,1,1,3};
unordered_map<int,int> ha;
cout <<"访问的次数:"<<time<<endl;
cout <<"访问的读者编号:"<<endl;
for(auto h : a){
ha[h]++;
//输出结果
cout <<ha[h]<<" ";
}
return 0;
}
运行截图:
问题七:
#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <algorithm>
using namespace std;
map<int, int> mp;
vector<int> vec;
bool cmp(int a, int b) {
if(mp[a] != mp[b]) {
return mp[a] >= mp[b];
}
return a<=b;
}
int main(){
int n, idx = 0;
cin >> n;
while(n--) {
int inp;
cin >> inp;
mp[inp]++;
if(mp[inp] == 1) {
vec.push_back(inp);
}
}
sort(vec.begin(), vec.end(), cmp);
for(int i = 0; i < vec.size(); i++) {
cout << vec[i] << ' ' << mp[vec[i]] << endl;
}
return 0;
}
// 12 5 2 3 3 1 3 4 2 5 2 3 5
运行截图: