周末大优惠变形(适合while循环,以0结束)

题目描述
商场周末大优惠,规定凡购物总价超过100元时,超过100的那部分便可以打9折。小冬冬和妈妈一起购买了一大批物品,你能帮他算出最终的应付款吗?

输入
一行,空格隔开的若干个整数,以0结束。

输出
一行,一个小数(保留三位小数)。

样例输入 Copy
4 5 8 9 0

样例输出 Copy
26.000

#include <iostream>
#include <iomanip>
 
using namespace std;
 
int main()
{
    double sum = 0, cost;
    int a;
    cin >> a;
    while (a!=0)
    {
        sum += a;
        cin >> a;
    }
    if(sum<100)
        cost = sum;
    else
        cost = 100.0 + (sum - 100.0) * 0.9;
    cout << fixed << setprecision(3) << cost << endl;
    return 0;
}

img

#include <iostream>
#include <iterator>
#include <ranges>
#include <iomanip>

using namespace std;

int main()
{
    double sum = 0;
    for (auto x : ranges::subrange(istream_iterator<int>(cin), istream_iterator<int>()))
    {
        if (x == 0)
            break;
        sum += x;
    }
    double cost = sum <= 100.0 ? sum : 100.0 + (sum - 100.0) * 0.9;
    cout << fixed << setprecision(3) << cost << endl;
    return 0;
}
$ g++ -Wall -std=c++20 main.cpp
$ ./a.out
4 5 8 9 0
26.000