题目列表:
2013年:带分数
2014年:六角填数
2015年:三羊献瑞
2016年:凑算式
1.带分数
题目描述
100 可以表示为带分数的形式:100 = 3 + 69258 / 714
还可以表示为:100 = 82 + 3546 / 197
注意特征:带分数中,数字1~9分别出现且只出现一次(不包含0)。
类似这样的带分数,100 有 11 种表示法。
题目要求:
从标准输入读入一个正整数N (N<1000*1000)
程序输出该数字用数码1~9不重复不遗漏地组成带分数表示的全部种数。
注意:不要求输出每个表示,只统计有多少表示法!例如:
用户输入:
100
程序输出:
11再例如:
用户输入:
105
程序输出:
6
分析:
N = a +
1.将数组中的9个数进行切分
2.首先确定a范围,因为后面还要加上某个不小于0的数,所以如果N是三位数,那么a最多也只能是三位数
3.再确定b的范围,因为c至少要有一位,所以b的下标最大只能到7
4.c的范围不用确定,因为除去a,b,剩下的就是c
注意:在使用next_permutation()函数时,数组的初始化必须要将1~9从小到大排列
代码:
#include<iostream>
#include<algorithm>
using namespace std;
int a[] = {1,2,3,4,5,6,7,8,9};
int N;
int getBits(){
int temp = N;
int cnt = 0;
while(temp){
cnt++;
temp/=10;
}
return cnt;
}
int getNum(int b,int e){
int n = 0;
for(int i = b;i <= e;i++){
n = n*10+a[i];
}
return n;
}
bool check(){
for(int i = 0;i < getBits();i++){//第一个数的范围
int a = getNum(0,i);
for(int j = i+1;j < 8;j++){//第二个数的范围
int b = getNum(i+1,j);
int c = getNum(j+1,8);
if(b%c==0 && a+b/c==N){
return true;
}
}
}
return false;
}
int main(){
cin >> N;
int ans = 0;
do{
if(check()){
ans++;
}
}while(next_permutation(a,a+9));
cout << ans;
return 0;
}
2.六角填数
如图所示六角形中,填入1~12的数字。
使得每条直线上的数字之和都相同。
图中,已经替你填好了3个数字,请你计算星号位置所代表的数字是多少?
请通过浏览器提交答案,不要填写多余的内容。
答案: 10
分析:
将除了1,3,8这三个数以外的9个数从小到大放入数组中进行全排列,将图中的9个圆圈进行编号,编号对应数组下标,符合每边和相等的就输出
代码:
#include<iostream>
#include<algorithm>
using namespace std;
int n[] = {2,4,5,6,7,9,10,11,12};
bool check(){
int a = 1+n[0]+n[3]+n[5];
int b = n[5]+n[6]+n[7]+n[8];
int c = 1+n[1]+n[4]+n[8];
int d = 3+n[6]+n[3]+8;
int e = 3+n[7]+n[4]+n[2];
int f = 8+n[0]+n[1]+n[2];
if(a==b&&b==c&&d==e&&e==f&&a==d){
return true;
}
return false;
}
int main(){
do{
if(check()){
cout << n[3];
return 0;
}
}while(next_permutation(n,n+9));
}
3.三羊献瑞
观察下面的加法算式:
祥 瑞 生 辉 + 三 羊 献 瑞 ------------------- 三 羊 生 瑞 气
(如果有对齐问题,可以参看【图1.jpg】)
其中,相同的汉字代表相同的数字,不同的汉字代表不同的数字。
请你填写“三羊献瑞”所代表的4位数字(答案唯一),不要填写任何多余内容。
答案:1085
分析:
这题和上题一样,只不过需要注意的是:既然是数,那么祥和三对应的数不能为0
代码:
#include<iostream>
#include<algorithm>
using namespace std;
int a[] = {0,1,2,3,4,5,6,7,8,9};
bool check(){
int b = a[0]*1000+a[1]*100+a[2]*10+a[3];
int c = a[4]*1000+a[5]*100+a[6]*10+a[1];
int d = a[4]*10000+a[5]*1000+a[2]*100+a[1]*10+a[7];
if(a[0]!=0 && a[4]!=0 && b+c==d){
return true;
}
return false;
}
int main(){
do{
if(check()){
cout << a[4] << a[5] << a[6] << a[1];
return 0;
}
}while(next_permutation(a,a+10));
}
4.凑算式
B DEF A + — + ——— = 10 C GHI这个算式中A~I代表1~9的数字,不同的字母代表不同的数字。
比如:
6+8/3+952/714 就是一种解法,
5+3/1+972/486 是另一种解法。
这个算式一共有多少种解法?
答案:29
分析:
1.分析题中的案例,我们可以知道B/C不一定能整除,所以为了不产生精度问题,我们将和
进行通分,通分后得到的就是一个整数了:
2.保证通分后能除尽,不然int/int=int(就算你算出来的是double,这里的运算结果也是int),对结果可能会产生误差
代码:
#include<iostream>
#include<algorithm>
using namespace std;
int a[] = {1,2,3,4,5,6,7,8,9};
bool check(){
int GHI = a[6]*100+a[7]*10+a[8];
int DEF = a[3]*100+a[4]*10+a[5];
if((a[1]*GHI+a[2]*DEF)%(a[2]*GHI) == 0 && a[0] + (a[1]*GHI+a[2]*DEF)/(a[2]*GHI) == 10){
return true;
}
return false;
}
int main(){
int ans = 0;
do{
if(check()){
ans++;
}
}while(next_permutation(a,a+9));
cout << ans;
return 0;
}