C/C++ Programming Learning-Week 6 ③ Class size

Topic link

Title description

A certain class of students participates in a programming competition, and the final result is more than p% but less than q%. Now give you P and Q, you have to figure out the minimum number of people in the class.

Input format
Enter two real numbers p, q, separated by spaces, each number has at most two decimal places, and 0.00 ≤ p <q ≤ 99.99.

Output format
Output the least possible number of people in the class.

Sample Input

13 14.1

Sample Output

15

Sample Input 2

67.73 67.92

Sample Output 2

28

Ideas

When the large percentage minus the small percentage is the percentage of one person, the total class size is the smallest. When you understand this, you are close to solving the problem.

C language code:

#include<stdio.h>
int main()
{
    
    
	double p, q;
	int i;
	scanf("%lf%lf", &p, &q);
	for(i = 1; ; i++)//用i模拟班级总人数,大的百分比乘人数-小的百分比乘人数如果近似等1则此时i为最少班级人数
		if((int)(i * q / 100.0) - (int)(i * p / 100.0) == 1)//强制类型转换,只需要用括号将你要转换的类型扩起来,放在要转换的变量前面即可。
		{
    
    
			printf("%d", i);
			break;
		}
	return 0;
}

C++ code:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    
    
	double p, q;
	while(cin >> p >> q)
	{
    
    
		for(int i = 1; i < 100000; i++)
		{
    
    
			int a = i * 1.0 / 100 * p;
			int b = i * 1.0 / 100 * q;
			if(b - a == 1)
			{
    
    
				cout << i << endl;
				break;
			}
		}
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_44826711/article/details/112911905