• 第一篇 基本能力的编程实验 第2章 简单模拟的编程实验


    2.1 直叙式模拟的实验范例

          2.1.1 Speed Limit

    D:Data Structure Experiment for Collegiate Programming Contest and EducationSimple SimulationSimple SimulationSpeed Limit.cpp

    #include "pch.h"
    #include <iostream>           //预编译指令
    using namespace std;          //使用C++标准程序库中的所有标识符
    int main()                    //主函数
    {                             //主函数开始       
        int n, i, x, y, z, ans;   //声明整型变量n、i、x、y、z、ans
        //多组测试数据,每次循环处理一组       
        while (cin >> n, n > 0)//反复输入当前组的数据对数,直至输入结束
        {
            ans = z = 0;
            //模拟跑表运行来计算
            for (i = 0;i < n;i++) {  //输入和计算当前数据组
                cin >> x >> y;//输入当前时速和总耗费时间
                ans += (y - z)*x;  //累计总里程
                z = y;  //记下当前总耗费时间
            }
            cout << ans << " miles" << endl;//输出当前数据组的总里程
        }
        return 0;
    }

          2.1.2 Ride to School

    2.2 筛选法模拟的实验范例

          2.2.1 Self Numbers

    2.3 构造法模拟的实验范例

          2.3.1 Bee

    注意答案可能超过长整型数据上限,因此a和b的类型设为long long。

    D:Data Structure Experiment for Collegiate Programming Contest and EducationSimple SimulationSimple SimulationBee.cpp

    #include "pch.h"
    #include <iostream>    //预编译指令
    using namespace std;   //使用C++标准程序库中的所有标识符
    
    int main(void) {
        int n;
        cin >> n;         //输入年数
        while (n > -1) {
            long long a = 1;//雌蜂数的初始值为1,雄蜂数的初始值为0。注意答案可能超过长整型数据上限
            long long b = 0;
            for (int i = 0;i < n;i++) {    //递推
                long long c, d;
                c = b + 1;                 //计算下一年雌蜂和雄蜂的数量    
                d = a + b;
                a = c;
                b = d;
            }
            cout << b << ' ' << a + b << endl;//输出N年后雄蜂的数量和蜜蜂的总数
            cin >> n;                 //输入下一个年数
        }
    
    }

    2.4 相关题库

    2.4.1 Gold Coins

    自己写的蹩脚程序:

    D:Data Structure Experiment for Collegiate Programming Contest and EducationSimple SimulationSimple SimulationGold Coins.cpp

    #include "pch.h"
    #include <iostream>       //预编译指令
    using namespace std;      //使用C++标准程序库中的所有标识符
    int main() {
        int n;
        cin >> n;//
        while (n > 0) {
            int ans = 0;
            for (int i = 0, j = 1;i <= n;j++) {  //外循环,枚举每个时间段j
                int k = j;//k为时间段内的剩余天数 1*1 2*2 3*3
                while (k-- && ++i <= n) {//内循环,计算时间段j内奖励的金币数
                ans+=j;
                }
            }
            cout << n<<' '<<ans << endl;
            cin >> n;
        }
        return 0;
    }

    2.4.2 The 3n+1 problem

    2.4.3 Pascal Library

    2.4.4 Calendar

    2.4.5 MANAGER

  • 相关阅读:
    用GUI完成了斗地主发牌
    地主发牌
    用三种循环完成了累加
    DataSet、DataTable、DataRow的数据复制方法
    C# Merge into的使用详解
    grid+report 怎么在项目中使用
    SQL Server 2008 R2:error 26 开启远程连接详解
    EF框架的优点是什么?
    Oracle中的case when then else end 应用
    SQL中的case when then else end用法
  • 原文地址:https://www.cnblogs.com/ZHONGZHENHUA/p/10375801.html
Copyright © 2020-2023  润新知