标题:带分数
100 可以表示为带分数的形式:100 = 3 + 69258 / 714
还可以表示为:100 = 82 + 3546 / 197
注意特征:带分数中,数字1~9分别出现且只出现一次(不包含0)。
类似这样的带分数,100 有 11 种表示法。
题目要求:
从标准输入读入一个正整数N (N<1000*1000)
程序输出该数字用数码1~9不重复不遗漏地组成带分数表示的全部种数。
注意:不要求输出每个表示,只统计有多少表示法!
例如:
用户输入:
100
程序输出:
11
再例如:
用户输入:
105
程序输出:
6
资源约定:
峰值内存消耗 < 64M
CPU消耗 < 3000ms
请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。
所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。
注意: main函数需要返回0
注意: 只使用ANSI C/ANSI C++ 标准,不要调用依赖于编译环境或操作系统的特殊函数。
output:
100=3+69258/714
100=81+5643/297
100=81+7524/396
100=82+3546/197
100=91+5742/638
100=91+5823/647
100=91+7524/836
100=94+1578/263
100=96+1428/357
100=96+1752/438
100=96+2148/537
TOTAL:11
time:0.500073
请按任意键继续. . .
提交时,注意选择所期望的编译器类型。
100 可以表示为带分数的形式:100 = 3 + 69258 / 714
还可以表示为:100 = 82 + 3546 / 197
注意特征:带分数中,数字1~9分别出现且只出现一次(不包含0)。
类似这样的带分数,100 有 11 种表示法。
题目要求:
从标准输入读入一个正整数N (N<1000*1000)
程序输出该数字用数码1~9不重复不遗漏地组成带分数表示的全部种数。
注意:不要求输出每个表示,只统计有多少表示法!
例如:
用户输入:
100
程序输出:
11
再例如:
用户输入:
105
程序输出:
6
资源约定:
峰值内存消耗 < 64M
CPU消耗 < 3000ms
请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。
所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。
注意: main函数需要返回0
注意: 只使用ANSI C/ANSI C++ 标准,不要调用依赖于编译环境或操作系统的特殊函数。
注意: 所有依赖的函数必须明确地在源文件中 #include <xxx>, 不能通过工程设置而省略常用头文件。
Thinking:
□ □ □ □ □ □ □ □ □
↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑
0 1 2 3 4 5 6 7
Bewteen 9 numbers, there're 8 position can be inserted for '+' & "/",also '+' must in front of '/'.
Equation: N-a=b/c
#include<iostream>//VS2008
#include<algorithm>
#include<windows.h>
using namespace std;
int arr[]={1,2,3,4,5,6,7,8,9},len=sizeof(arr)/sizeof(int),total=0;
int create(int i,int j)
{
int sum=0,k=i;
for(;k<=j;k++)
sum=sum*10+arr[k];
return sum;
}
int bits(int n)
{
int count=0;
for(;n;count++)
n/=10;
return count;
}
int main()
{// Condition: N-a=b/c
LARGE_INTEGER t1,t2,tc;
QueryPerformanceFrequency(&tc);
QueryPerformanceCounter(&t1);
int a,b,c,plusPos,divPos,N=100,bit_a,bit_Na,bit_remain;//plusPos for the position of plus,divPos means: the position of division sign
do
{
for(plusPos=0;plusPos<len-2;plusPos++)
{
a=create(0,plusPos);//create a
bit_Na=bits(N-a);//the bits of (N-a)
bit_a=bits(a);
if(bit_Na>9-bit_a-1||a>N)
break;
bit_remain=9-bit_a;
if(bit_remain%2==1&&bit_Na%2==1)//For instance:n(1000~9999),m(10~99),the bits of n/m which range from 1000/99≈10.1(2 bits) to 9999/10≈999.9(quite close to 4 bit)
divPos=(bit_remain/2)+(bit_Na/2)+1;//for each n,m also the answer.
else
divPos=(bit_remain/2)+(bit_Na/2);
b=create(plusPos+1,divPos+plusPos);//create b
c=create(plusPos+divPos+1,8);//create c
if((N-a)*c==b)
{
cout<<N<<"="<<a<<"+"<<b<<"/"<<c<<endl;
total++;
}
}
}
while(next_permutation(arr,arr+len));
cout<<"TOTAL:"<<total<<endl;
QueryPerformanceCounter(&t2);
cout<<"time:"<<1.0*(t2.QuadPart-t1.QuadPart)/tc.QuadPart<<endl;
return 0;
}
output:
100=3+69258/714
100=81+5643/297
100=81+7524/396
100=82+3546/197
100=91+5742/638
100=91+5823/647
100=91+7524/836
100=94+1578/263
100=96+1428/357
100=96+1752/438
100=96+2148/537
TOTAL:11
time:0.500073
请按任意键继续. . .
提交时,注意选择所期望的编译器类型。