原题链接:https://www.luogu.org/problemnew/show/P1618
题目大意:
1~9九个数字,组成三个三位数,这三个数满足:
- 每个数字只能使用一次;
- 三个数之间满足A:B:C的关系,且A<B<C;
输入A、B、C;
输出所有满足条件的三个三位数,占一行;
思路:
1)A最小为1,最小比例为1:2:3,而且满足条件一的最大数是987,最小数是123,所以第一个数n1范围是[123,397];
2)接下来n2 = n1/AB, n3 = n1/AC;
3)判断n1、n2、n3是否满足条件即可;
代码:
1.#include<iostream>
2.#include<iomanip>
3.
4.using namespace std;
5.
6.short F(const short a, const short b)//判断a和b两个数是否满足条件
7.{
8. short a1, a2, a3, b1, b2, b3, f = 0;
9. a1 = a/100;
10. a2 = a%100/10;
11. a3 = a%10;
12. b1 = b/100;
13. b2 = b%100/10;
14. b3 = b%10;
15. if( a1!=a2&&a2!=a3&&a1!=a3&&a1!=0&&a2!=0&&a3!=0 ){
16. if( a1!=b1&&a2!=b1&&a3!=b1&&a1!=b2&&a2!=b2&&a3!=b2&&a1!=b3&&a2!=b3&&a3!=b3 ){
17. if( b1!=b2&&b1!=b3&&b2!=b3&&b1!=0&&b2!=0&&b3!=0 ) f = 1;
18. }
19. }
20. return f;
21.}
22.
23.int main()
24.{
25. /*
26. A, B, C 在[1,9) A<B<C
27. max数 = 987;
28. 所以 第一个数最大值 = 987/3 = 329, 最小值是123;
29. */
30. short a, b, c, m = 0, n = 0;
31. cin >> a >> b >> c;
32. short f = 0, flag = 0;
33. for( int i=123; i<330; i++){
34. m = n = 0;
35. f = 0;
36. if( i%a==0 ){
37. if( m%b==0 ) m = i/a*b;
38. if( n%c==0 ) n = i/a*c;
39. }
40.
41. if( m>987||n>987 ) continue;
42. f = F(i, m);
43. if( f ){
44. f = F(i, n);
45. if( f ){
46. f = F(m, n);
47. }
48. }
49. if( f ){
50. cout << i << ' ' << m << ' ' << n << endl;
51. flag = 1;
52. }
53. }
54. if( flag==0 ) cout << "No!!!";
55.
56. return 0;
}