算法设计与分析第一章 课后习题答案(c++)

该文章是算法设计与分析(第二版)中各章的课后习题 主编 : 李春葆

在本文中,主要是对该文第一章,绪论中的在线编程题进行代码解答。

代码运行环境是: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

运行截图:

  • 1
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
算法设计与分析基础 课后答案 第二版 中英文 作者简介:莱维丁是Villanova大学计算科学系的教授。他的论文 A New Road Map of Algorithm Design Techniques:Picking Up Where the Traditional Classification Leaves Off(《算法设计技术新途径:弥补传统分类法的缺憾》)受到业内人士极高的评价。在SIGCSE会议上,作者做过多次关于算法教学的演讲。 译者简介:潘彦,计算机专业人士,国际电气电子工程师学会(IEEE)会员。 作者基于丰富的教学经验,开发了一套对算法进行分类的新方法。这套方法站在通用问求解策略的高度,能对现有的大多数算法都能进行准确分类,从而使本书的读者能够沿着一条清晰的、一致的、连贯的思路来探索算法设计与分析这一迷人领域。本书作为第2版,相对第1版增加了新的习题,还增加了“迭代改进”一章,使得原来的分类方法更加完善。 本书十分适合作为算法设计和分析的基础教材,也适合任何有兴趣探究算法奥秘的读者使用,只要读者具备数据结构和离散数学的知识。 This file contains the exercises, hints, and solutions for Chapter 1 of the book ”Introduction to the Design and Analysis of Algorithms,” 2nd edition, by A. Levitin. The problems that might be challenging for at least some students are marked by ; those that might be difficult for a majority of students are marked by  . Exercises 1.1…………

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值