P8295 题解

· · 题解

前言

题目传送门

\color{red}{see}\space \color{green}{in}\space \color{blue}{my}\space \color{purple}{blog}

这题并不困难,代码也挺短的,题目理解稍有困难。

题目大意

(n+1) 家商店,也就是 n 家商店与 NSC 商店。

求 这 (n+1) 家商店 1000 克香肠的价钱的最小值

思路

将所有商店 1000 克香肠的价钱都求出来,再求最小值即可。

比如,xy 克的香肠,转换成 1000 克就是 \dfrac{1000 \times x}{y} 元。

代码就很容易写了,注意最后需要保留两位小数。

完整代码

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;

int main()
{
    int x, y;
    scanf("%d%d", &x, &y);
    double ans = 1.0 * x / y * 1000;  //乘上 1.0 强制变成浮点数。 
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
    {
        scanf("%d%d", &x, &y);
        ans = min(ans, 1.0 * x / y * 1000);
    }
    printf("%.2lf", ans);  //强制保留两位小数。 
    return 0;
}