Luogu P1909 Buy Pencil Problem Solution

topic:

Teacher P needs to go to the store to buy n pencils as gifts for the children to participate in NOIP. She found that there are 3 types of pencils in the store. The number of pencils in different packages may be different, and the price may also be different. To be fair, Teacher P decided to buy only pencils in the same packaging.
The store does not allow the pencils to be unpacked, so Teacher P may need to buy more than n pencils to give children gifts.
Now Teacher P wants to know how much it would cost to buy at least n pencils if the quantity of each package in the store is sufficient.

Input format The
first line contains a positive integer n, indicating the number of pencils needed.
In the next three lines, each line uses 2 positive integers to describe a package of pencils: the first integer represents the number of pencils in this package, and the second integer represents the price of this package.
Ensure that all 7 numbers are positive integers not exceeding 10,000.

The output format is
1 integer, which represents the least money that Teacher P needs to spend.

Sample input and output
input 1

57
2 2
50 30
30 27

Output 1

54

Input 2

9998
128 233
128 2333
128 666

Output 2

18407

The key to this question is how to find the money needed for each pen according to the number of pens needed. For accuracy and simplicity, the colon operator is used here.

#include <stdio.h>

int main() {
	int num;//需要铅笔的数量
	int pen1, $pen1;//penl1:第一种包装数量,$pen1:这种包装价格
	int pen2, $pen2;
	int pen3, $pen3;
	int m1, m2, m3=0;//花费钱
	int min;//最少花费的钱

	scanf("%d", &num);
	scanf("%d %d", &pen1, &$pen1);
	scanf("%d %d", &pen2, &$pen2);
	scanf("%d %d", &pen3, &$pen3);

	int a = num / pen1;  num%pen1 == 0 ? m1=a * $pen1 : m1=(a + 1)*$pen1;  
	int b = num / pen2;  num%pen2 == 0 ? m2=b * $pen2 : m2=(b + 1)*$pen2;
	int c = num / pen3;  num%pen3 == 0 ? m3=c * $pen3 : m3=(c + 1)*$pen3;

	if (m1 < m2) {
		min = m1;
	}
	else {
		min = m2;
	}
	if (min < m3) {
		min = min;
	}
	else {
		min = m3;
	}
	printf("%d\n", min);

	/*调试用
	printf("m1=%d\n", m1);
	printf("m2=%d\n", m2);
	printf("m3=%d\n", m3);
    */
	return 0;
}

Acm Xiaobai, if you have any questions, you are welcome to point it out, thank you

Guess you like

Origin blog.csdn.net/weixin_44093867/article/details/97621595