[Blue Bridge Cup] Real Exam Training 2014 C++A Group Question 1 Guess the age

Guess the age

Title: Guess the age
Xiao Ming took two younger sisters to the Lantern Festival. When someone asked them how old they were, they said mischievously: "The product of our two ages is 6 times the sum of our ages." Xiao Ming added: "They are not twins, and the difference in age must be no more than 8 years old."
Please write: the age of Xiao Ming's younger sister.
Note: Only write the age number of a person, please submit the answer through the browser. Don't write any extra content.

Answer: 10

Problem analysis

 Violent solution

#include <iostream>
using namespace std;

int main(int argc, char** argv) {
	for(int i = 1; i < 50; i++){
		for(int j = 1; j < 50; j++){
			if(j != i && abs(i-j) <= 8){
				if(6*(i+j) == i*j){
					cout << i << " " << j;
					break;
				}
			}
		}
	}
	return 0;
}

 

Guess you like

Origin blog.csdn.net/weixin_44566432/article/details/115196466