AcWing 1209. 带分数
原题链接
简单
作者:
hxzz
,
2020-04-03 14:24:47
,
所有人可见
,
阅读 609
C++ 代码
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
//a*c + b = c*N;
int calc(int l, int r){
int res = 0;
for(int i = l; i <= r; i++)
res = res * 10 + a[i];
return res;
}
int main(){
int cnt = 0;
int n;
cin >> n;
for(int i = 0; i < 7; i++)
for(int j = i + 1; j < 8; j++){
int a = calc(0, i);
int b = calc(i + 1, j);
int c = calc(j + 1, 8);
//注意判断条件,因为C++中除法是整除,所以要转化为加减乘来计算
if(a * c + b == c * n) cnt++;
}
while(next_permutation(a, a+9)){
for(int i = 0; i < 7; i++)
for(int j = i + 1; j < 8; j++){
int a = calc(0, i);
int b = calc(i + 1, j);
int c = calc(j + 1, 8);
//注意判断条件,因为C++中除法是整除,所以要转化为加减乘来计算
if(a * c + b == c * n) cnt++;
}
}
cout << cnt << endl;
}