您的位置:首页 > 编程语言 > C语言/C++

C++Primer第五版 3.1节练习

2015-09-11 08:05 441 查看
练习3.1:使用恰当的using声明重做1.4.1节(第11页)和2.6.2节(第67页)的练习。

答:程序代码如下。

/*
*
*2015/5/14
*重做 1.4.1节点练习,3个,练习1.9,练习1.10,练习1.11 
*/
#include <iostream>

using std::cin;
using std::cout;
using std::endl;

int main()
{
    //练习1.9
    cout << "1.练习1.9:" << endl;
    int sum = 0;
    int i = 50;
    while (i <= 100)
    {
        sum += i;
        ++i;    
    } 
    cout << "练习1.9的结果:" << sum << endl;
    cout << "\t\n";

    //练习1.10
    cout << "2.练习1.20:" << endl;
    int j = 10;
    cout << "练习1.20的结果:" << endl; 
    while (j > 0)
     {
        cout << j << " ";
        --j;    
     } 
    cout << endl;
    cout << "\t\n";
    //练习1.11
    cout << "2.练习1.11:" << endl;
    int v1, v2;
    cout << "请输入两个整数v1,v2: "<< endl;
    cin >> v1 >> v2;
    cout << "练习1.11的结果:" << endl;
    if (v1 > v2)
    {
        for (int i = v2; i <= v1 ; ++i)
            cout << i << " ";
     } 
     else 
     { 
        while (v1 <= v2)
         {
            cout << v1 << " ";
            ++v1;   
         }  
     }
     cout << endl;

    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: