算法竞赛入门经典(第二版)第2章部分学习实现

1 求和问题

input如下:

1 3

4 5

0

output如下:

4

9

要求:一次性输入,一次性输出

fstream实现

#include <iostream>
#include <fstream>
 
using namespace std;
 
int main()
{
    fstream file_buff("re", ios::out | ios::in | ios::trunc);
    cout << "please input a pair of intergers:" << endl;
    int a, b, sum;
    while(cin>>a){
        if(a==0)
            break;
        cin >>b;
        sum = 0;
        sum = a+b;
        file_buff << sum << '\n';
    }
 
    string line_str;
    file_buff.seekg(0);
    while(getline(file_buff, line_str))
        cout << line_str << endl;
    file_buff.close();
    return 0;
}


2 要实现的同1,不过不能输出中间文件

 sstream实现

#include <iostream>
#include <sstream>

using namespace std;
 
int main()
{
    cout << "please input a pair of intergers:" << endl;
    string line;
    int a(0), b(0), sum(0), need_break(0);
    stringstream re;
    while(getline(cin, line), line.length()){
        stringstream mystream(line);
        sum = 0;
        while(mystream>>a){
            if (a==0){
                need_break = 1;
                break;
            }
            mystream >> b;
            sum = a+b;
        }
        if (need_break==1)
            break;
        re << sum << '\n';
    }
    cout << "cout all the results:" << endl;
    string re_out;
    while(re >> re_out)
        cout << re_out << endl;
    return 0;
}

我的感想:

先用getline读到string变量line中,对line进行处理,再将结果赋值给stringstream变量;

将结果反馈给控制台前,先要用stringstream读到变量中;

注意在第二层while循环前对sum赋值0.

 

3 算法竞赛入门经典第二版 35页 习题2-4

#include <iostream>
#include <iomanip>
 
using namespace std;
 
int main()
{
    cout << "请输入两个不大于1e6的正整数,第一个数小于第二个数:" << endl;
    long n=65536, m=655360;
    double sum(0);
    int num(0);
    while (cin >> n >> m){
        num++;
        if (n>0 && m>0 && m<1000000 && n<m){
            sum = 0;
            for (long i=n; i<=m; ++i){
                // 下面这一行至关重要,如果没有它,sum=inf(结果太小了)
                float tmp = static_cast<float>(1)/static_cast<float>(i);
                sum += tmp * tmp;
                }
            cout << setprecision(5) << fixed;
            cout << "Case " << num << ": " << sum << endl;
            cout.unsetf(ostream::floatfield);
        }
        else if (n*m==0)
            break;
        else
            cout << "请再次输入:" << endl;
    }
    return 0;
}

我的感想:
溢出问题呀,中间变量!


4 算法竞赛入门经典第二版 35页 习题2-5

#include <iostream>
#include <iomanip>  // 操作符
 
using namespace std;
 
int main()
{
    int a, b, c;
    int num(0);
    while (cin >> a >> b >>c){
        num++;
        if (a*b*c>0 && a<=1e6 && b<=1e6 && c<100){
            cout << setprecision(c) << fixed;
            cout << "Case " << num << ": " << static_cast<double>(a)/static_cast<double>(b) << endl;
            cout.unsetf(ostream::floatfield);  // 记得有始有终 
        }
        else
            if (a*b*c==0)
                break;
            else
                {cout << "please input reasonable numbers" << endl;
                continue;}
    }
    return 0;
}

我的感想:
1 static_cast强制类型转换的使用;
2 cout.unsetf有始有终!

5 算法竞赛入门经典第二版 35页 习题2-6

#include <iostream>
#include <vector>
 
using namespace std;
 
bool is_inVec(int x, vector<int>y){
    for (vector<int>::iterator p=y.begin(); p!=y.end(); ++p){
        if (*p == x)
            return true;
    }
    return false;
}
 
int main()
{
    int a, b, c, d, e, f, g, h, m;
vector<int> vec_int;
// ghm最大只能是987,故i最大只能取329
    for (int i=123; i<329; ++i){
        vec_int.clear(); // 当时没加这句话,结果错误!
        vector<int>::iterator p_vec;
        a = i/100;
        vec_int.push_back(a);
        b = (i%100)/10;
        if (!is_inVec(b, vec_int))
            vec_int.push_back(b);
        else
            continue;
        c = (i%100)%10;
        if (!is_inVec(c, vec_int))
            vec_int.push_back(c);
        else
            continue;
        d = 2*i/100;
        if (!is_inVec(d, vec_int))
            vec_int.push_back(d);
        else
            continue;
        e = (2*i%100)/10;
        if (!is_inVec(e, vec_int))
            vec_int.push_back(e);
        else
            continue;
        f = (2*i%100)%10;
        if (!is_inVec(f, vec_int))
            vec_int.push_back(f);
        else
            continue;
        g = 3*i/100;
        if (!is_inVec(g, vec_int))
            vec_int.push_back(g);
        else
            continue;
        h = (3*i%100)/10;
        if (!is_inVec(h, vec_int))
            vec_int.push_back(h);
        else
            continue;
        m = (3*i%100)%10;
        if (!is_inVec(m, vec_int))
            cout << i << " " << 2*i << " " << 3*i << endl;
    }
    return 0;
}

Time: 0.031s
1 清空操作很重要,谨记!
2 我这种方法应该不是最简单的,还有好的方法吗?求。


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值