zoukankan      html  css  js  c++  java
  • 2017蓝桥杯购物单(C++B组)

    原题:

    标题: 购物单

           小明刚刚找到工作,老板人很好,只是老板夫人很爱购物。老板忙的时候经常让小明帮忙到商场代为购物。小明很厌烦,但又不好推辞。这不,XX大促销又来了!老板夫人开出了长长的购物单,都是有打折优惠的。小明也有个怪癖,不到万不得已,从不刷卡,直接现金搞定。现在小明很心烦,请你帮他计算一下,需要从取款机上取多少现金,才能搞定这次购物。取款机只能提供100元面额的纸币。小明想尽可能少取些现金,够用就行了。你的任务是计算出,小明最少需要取多少现金。以下是让人头疼的购物单,为了保护隐私,物品名称被隐藏了。
        

    ****     180.90       88折

    ****      10.25       65折

    ****      56.14        9折

    ****     104.65        9折

    ****     100.30       88折

    ****     297.15       半价

    ****      26.75       65折

    ****     130.62       半价

    ****     240.28       58折

    ****     270.62        8折

    ****     115.87       88折

    ****     247.34       95折

    ****      73.21        9折

    ****     101.00       半价

    ****      79.54       半价

    ****     278.44        7折

    ****     199.26       半价

    ****      12.97        9折

    ****     166.30       78折

    ****     125.50       58折

    ****      84.98        9折

    ****     113.35       68折

    ****     166.57       半价

    ****      42.56        9折

    ****      81.90       95折

    ****     131.78        8折

    ****     255.89       78折

    ****     109.17        9折

    ****     146.69       68折

    ****     139.33       65折

    ****     141.16       78折

    ****     154.74        8折

    ****      59.42        8折

    ****      85.44       68折

    ****     293.70       88折

    ****     261.79       65折

    ****      11.30       88折

    ****     268.27       58折

    ****     128.29       88折

    ****     251.03        8折

    ****     208.39       75折

    ****     128.88       75折

    ****      62.06        9折

    ****     225.87       75折

    ****      12.89       75折

    ****      34.28       75折

    ****      62.16       58折

    ****     129.12       半价

    ****     218.37       半价

    ****     289.69       8折


    以上是购物清单,利用记事本的查找替换功能,将以上数据跟改为如下模式方便C++文件流函数读取:

         180.90       88
          10.25       65
          56.14        90
         104.65        90
         100.30       88
         297.15       50
          26.75       65
         130.62       50
         240.28       58
         270.62        80
         115.87       88
         247.34       95
          73.21        90
         101.00       50
          79.54       50
         278.44        70
         199.26       50
          12.97        90
         166.30       78
         125.50       58
          84.98        90
         113.35       68
         166.57       50
          42.56        90
          81.90       95
         131.78        80
         255.89       78
         109.17        90
         146.69       68
         139.33       65
         141.16       78
         154.74        80
          59.42        80
          85.44       68
         293.70       88
         261.79       65
          11.30       88
         268.27       58
         128.29       88
         251.03        80
         208.39       75
         128.88       75
          62.06        90
         225.87       75
          12.89       75
          34.28       75
          62.16       58
         129.12       50
         218.37       50

         289.69       80


    更改为如上模式后,就可以利用C++进行计算了,具体代码如下:


    #include<iostream>
    #include<fstream>
    #include<string.h>
    #include<sstream>
     
    using namespace std;


    int main(int argc,char** argv){

    ifstream fin;                                                    //利用ifstream函数从文本中读取数据。
    fin.open("data.txt");

    int sth[50];
    int money[50];
    int i = 0;

    int resault = 0;

    string data_temp;

    while(fin){                                                        //利用循环遍历,将所有数据保存到预定义好的数组中等待计算。


    i++;                                                           //表示数组下标

    string sth_temp,money_temp;
    stringstream sth_,money_;                      //利用stringstream函数将C++string类型强制类型转化为int类型

    fin >> sth_temp;
    fin >> money_temp;

    sth_ << sth_temp;
    money_ << money_temp;

    sth_ >> sth[i];
    money_ >> money[i];


    resault = sth[i] * money[i] + resault;


    }

    cout << resault / 100;                                                //最后输出结果。

    }





    如图所示,结果为5118,以为要求是整百数,所以提交结果为5200;

    作者:蓝月

    -------------------------------------------

    个性签名:能我之人何其多,戒骄戒躁,脚踏实地地走好每一步

  • 相关阅读:
    Linux中history执行历史命令方法
    Linux中返回上一次目录
    Linux的vi编辑模式下常用快捷键
    [Android] TextView上同时显示图标和文字
    [Android] macOS的Android Studio快捷键
    [Android] 转-RxJava+MVP+Retrofit+Dagger2+Okhttp大杂烩
    [iOS] 测试设备解决自签名证书问题
    [macOS] keychain的跳坑之旅!git拉取的权限问题
    [PHP] swoole在daemonize模式下,chdir失效问题
    [macOS] macOS下,VirtualBox安装CentOS7.4, 搭建nginx, mysql, PHP5.6&PHP7.1
  • 原文地址:https://www.cnblogs.com/viplanyue/p/12700763.html
Copyright © 2011-2022 走看看