zoukankan      html  css  js  c++  java
  • C++ Primer章课后编程问题



    1、
    代码
    #include<iostream>
    int main()
    {
      using namespace std;
      int num1;
      int num2;
      int total=0;
      cout << "请输入開始数字
    ";
      cin >> num1;
      cout << "请输入结束数字
    ";
      cin >> num2;
      for (num1; num1<=num2; num1++)
        total = num1 + total;
      cout << num1 << " 和 " << num2 << "之间的整数和为 " << total <<endl;
      return 0;
    }
    执行结果


    2、
    代码
    #include<iostream>
    int main()
    {
      using namespace std;
      double total = 0.0;
      double in;
      cout << "请输入数字:";
      cin >> in;
      while (in != 0)
      {
        total += in;
        cout << "全部输入数的和为:" << total << "
    ";
        cout << "请输入下一个数字:";
        cin >> in;
      }
      cout << "执行结束";
      return 0;
    }
    执行结果


    3、
    代码
    #include<iostream>
    int main()
    {
      using namespace std;
      double daphne=100;
      double cleo=100;
      int year=1;
      while (daphne >= cleo)
      {
        daphne += 100*0.1;
        cleo += cleo*0.05;
        cout << "第" << year << "年,daphne投资价值为 " << daphne << "cleo投资价值为 " << cleo <<endl;
        year++;
      }
      cout << "第 " << year << "年,时cleo超过daphne的投资价值"<< endl;
      return 0;
    }
    执行结果


    4、
    代码
    #include<iostream>
    const char *months[12] = {"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"};
    int main()
    {
        using namespace std;
        int sales[12];
        int total;
        for (int i=1; i<=12; i++)
        {
            cout << "请输入" << months[i-1] << "销售数量:";
            cin >> sales[i-1];
        }
        for (int j=0; j<12; j++)
        {
            total = total+sales[j];
        }
        cout << "总销售为:" << total <<endl;
        return 0;
    }
    执行结果


    5、
    代码
    #include<iostream>
    const char *months[12] = {"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"};
    int main()
    {
        using namespace std;
        int sales[3][12];
        int total[3] = {0};   //一定要初始化。不然初始值不为0
        int sum;
        for (int a=1;a<=3;a++)
        {
                for (int i=1; i<=12; i++)
                {
                    cout << "请输入第"<< a << "年" << months[i-1] << "销售数量:";
                    cin >> sales[a-1][i-1];
                }
        }
        for (int b=0; b<3; b++)
        {
                for (int j=0; j<12; j++)
                {
                    total[b] = total[b]+sales[b][j];
                }
                sum = sum + total[b];
                cout << "第" << b+1 << "年总销量为" << total[b] <<endl;
        }
        cout << "总销售为:" << sum <<endl;
        return 0;
    }
    执行结果


    6、
    代码
    <pre name="code" class="cpp">//一定要加while (cin.get() != '
    '); 
    #include<iostream>
    using namespace std;
    const int LEN = 60;
    struct Car
    {
      char brand[LEN];
      int year;
    };
    int main()
    {
      int num;
      cout << "How many cars do you wish to catalog?";
      cin >> num;
      while (cin.get() != '
    ');
      Car *ps = new Car[num];
      for (int i=0;i<num;i++)
      {
        cout << "Car #" << (i+1) << ":
    ";
        cout << "Please enter the make:";
        cin.getline(ps[i].brand, LEN);
        cout << "Please enter the year made:";
        cin >> ps[i].year;
        while(cin.get() != '
    ');
      }
      cout << "Here is your collection:
    ";
      for (int i=0; i<num; i++)
      {
        cout << ps[i].year << " " << ps[i].brand <<endl;
      }
      delete [] ps;
      return 0;
    }
    
    
    执行结果

    7、
    代码
    #include<iostream>
    #include<cstring>
    const int STR_LEN=60;
    int main()
    {
      using namespace std;
      char words[STR_LEN];
      int count=0;
      cout << "Enter words(to stop,type the word done):
    ";
      while (cin >> words && strcmp("done", words))
        ++count;
      cout << "You entered a total of " << count << " words .
    " <<endl;
      return 0;
    }
    执行结果


    8、
    代码
    #include<iostream>
    #include<string>
    int main()
    {
      using namespace std;
      string words;
      int count=0;
      cout << "Enter words {to stop,type the word done}:
    ";
      while (cin >> words && words != "done")
        ++count;
      cout << "You entered a total of " << count << " words .
    ";
      return 0;
    }
    执行结果


    9、
    代码
    #include<iostream>
    int main()
    {
      using namespace std;
      int row;
      cout << "Enter number of row:";
      cin >> row;
      for (int i=0; i<row; i++)
      {
        for (int j=row-i; j>1; j--)
        {
          cout << ".";
        }
        for (int k=0; k<=i; k++)
        {
          cout << "*";
        }
        cout << "
    ";
      }
      return 0;
    }
    执行结果



    版权声明:本文博主原创文章。博客,未经同意不得转载。

  • 相关阅读:
    [Paper Review]Distilling the Knowledge in a Neural Network,2015
    Taylor Series and Gradient Descent
    Regularization from Large Wights Perspective
    Generative Model vs Discriminative Model
    State Function Approximation: Linear Function
    Temporal-Difference Control: SARSA and Q-Learning
    php扩展(centos+php7.2)---Composer: The requested PHP extension ext-intl * is missing from your system
    docker常用命令
    cURL error 60: SSL certificate problem: unable to get local issuer certificate
    【Magento 2学习】
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/4888896.html
Copyright © 2011-2022 走看看