公式

A - Online Shopping 解説 by en_translator


The amount of money he has to pay can be obtained by the following steps.

  1. Find the total amount of price of the items to buy.
  2. If the total amount is greater than or equal to \(S\) yen, the answer is the value itself; otherwise, it is obtained by adding \(K\) to it.

The total amount of price of the items to buy is the sum of \((P_i\times Q_i)\) over \(i=1,2,\ldots,N\), which can be found using a for statement.
Then, you can determine the shipping fee using an if statement.

Therefore, the problem has been solved.
Note that the amount of money he has to buy is at most \(100\times(10000\times 100)+10000<2^{31}\) yen, which can be handled with a signed \(32\)-bit integer type.

Sample code in C++:

#include <bits/stdc++.h>
using namespace std;

int main() {
	int n,s,k,p,q;
	int ans=0;

	cin>>n>>s>>k;
	for(int i=0;i<n;i++){
		cin>>p>>q;
		ans+=(p*q);
	}
	if(ans<s)ans+=k;
	
	cout<<ans<<endl;
	return 0;
}

Sample code in Python:

n,s,k=map(int, input().split())
ans=0

for i in range(n):
    p,q=map(int, input().split())
    ans+=(p*q)

if ans<s:
    ans+=k

print(ans)

投稿日時:
最終更新: