题解:P7621 [AHOI2021初中组] 超市购物

P7621 题解

题意

\(n\) 种商品,第 \(i\) 种商品的价格为 \(a_i\),买了 \(b_i\) 个,求打八五折后总价钱(截至一位小数,不要四舍五入)。

题解

计算很简单,难在输出,可以用字符串截取到小数点后一位的位置输出。

#include <bits/stdc++.h>
using namespace std;
/*=====================*/
typedef long long ll;
/*=====================*/
#define endl '\n'
/*=====================*/
const double SALE=0.85;
/*=====================*/
void Solve(void)
{
    int n;cin>>n;
    vector<pair<double,int>> arr(n+1,{0,0});
    for(int i=1;i<=n;i++)
    {
        cin>>arr[i].first>>arr[i].second;
    }
    double sum=0;
    for(int i=1;i<=n;i++)
    {
        sum+=arr[i].first*arr[i].second;
    }
    sum*=SALE;
    // cout<<ans<<endl;
    string str=to_string(sum);
    // cout<<str<<endl;
    int pos=str.find(".");
    // cout<<pos<<endl;
    string ans=str.substr(0,pos+2); //pos+1是下标,由于长度-1=下标,所以+1,到达小数点,再+1到达后一位,所以+2
    cout<<ans<<endl;
}
/*=====================*/
int main()
{
    // freopen("shopping.in","r",stdin);
    // freopen("shopping.out","w",stdout);
    ios::sync_with_stdio(false);
    cin.tie(nullptr),cout.tie(nullptr);
    int T=1;//cin>>T;
    while(T--)Solve();
    return 0;
}
posted @ 2025-08-24 13:33  yufh  阅读(11)  评论(0)    收藏  举报
浏览器标题切换
浏览器标题切换end