C解两道题

偷懒间做了两道题,供大家把玩吧。我的解法不见得最好,但是是可以work的。大家有解法可以贴到评论里,我们可以切磋下。

1.If p is the perimeter of a right angle triangle with integral length sides, {a,b,c}, there are exactly three solutions for p = 120. click

{20,48,52}, {24,45,51}, {30,40,50}

For which value of p <=1000, is the number of solutions maximised?

翻译过来就是:

如果p是这样一个数,满足三元组{a,b,c}之和是p,且a,b,c能组成直角三角形的三个边。当p=120的时候,且有三个解:

{20,48,52}, {24,45,51}, {30,40,50}

那么在p<=1000以内,满足以上条件的解的个数最多的p是几?

答案:840.

 

  

View Code
int a1[3000]={};

for(int a=1;a<1000;a++)
for(int b=a;b<1000;b++)
for(int c=b;c<1000;c++)
if(((a*a)+(b*b)==(c*c))&&((a+b+c)<=1000))
a1[a
+b+c]++;

for(int i=0;i<3000;i++)
if(a1[i]>=3)
cout
<<i<<":"<<a1[i]<<endl;

2.A permutation is an ordered arrangement of objects. For example, 3124 is one possible permutation of the digits 1, 2, 3 and 4. If all of the permutations are listed numerically or alphabetically, we call it lexicographic order. The lexicographic permutations of 0, 1 and 2 are:

012 021 102 120 201 210

What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9? click

翻译:A permutation is an ordered arrangement of objects。(这句我真翻译不好,大意就是排列的定义)比如,3124是数字1,2,3,4的一种可能排列。如果把所有的排列按照数字或者字母列出来,我们称为字典顺序。数字0,1,2的字典排序为:

012 021 102 120 201 210

那么数字0,1,2,3,4,5,6,7,8,9的字典排序的第1000000(100万)个排列数字是几?

  答案:2783915460(貌似这个可以用笔算出来,我稍后再给出计算方法,或者哪位大大给算算。)

 

 

View Code
__int64 m1=123456789,m2=9876543210;
__int64 count
=0;


for(m1=123456789;m1<m2;m1++)
{
__int64 i
=m1;
__int64 a[
10]={};
__int64 len
=0;
while(i!=0)
{
__int64 d
=i%10;
a[d]
=1;
i
=i/10;
len
++;
}
bool flag=true;
for(__int64 k=len==9?1:0;k<10;k++)
{
if(a[k]==0)
{
flag
=false;
break;
}
}

if(flag)
{
count
++;
if(count<=1000000)
{
cout
<<count<<"::"<<m1<<endl;
}
else
break;
}

}

附上笔算解法:

设最终的数字为X=??????????
假设0
~9这10个数字排队,排了10对,每队的排头分别是0~9,队伍是从小向大的方向站队。
因为确定排头后,每队剩下的9个数字全排列就是9!
=3628880.
每队都有9!个数字,那么第1000000个应该是在2开头的队里,从来确定X的首位数字是2?????????.
9*3=1088640.那么X应该距离2队的队尾1088640-100000=88640.也就是说X在2队距离队尾88640个的位置之后。
剩下的9个数字9,
8765431,0继续站队。继续按照上面的方式排队,确定排头,那么每队的人数是8!=40320.那么从后向前数的话,X应该在队伍7里第88640-40320*2=8000个位置之后。
好了,X的第2个数字也确定了,X
=27????????。
继续,
9865431,0继续站队。同样的确定排头,每队是7!=5040个。那么X在队伍8里第8000-5040*1=2960的位置之后。所以,X的第3位数字定了,X=278???????
继续,
965431,0排队,每队是6!=720,同样,X在队伍3里第2960-720*4=80的位置之后。X的第4个数字确定,X=2783??????
继续,
96541,0排队,每队是5!=120个数字,那么毫无疑问,X在队伍9里第80-120*0=80的位置之后。X=27839?????
继续,
6541,0排队,每队是4!=24个数字,X在队伍队伍1里第80-24*3=8之后。X=278391????
还剩6,
540,每队是3!=6个 数字,那么X在队伍5里第8-6*1=2之后。X=2783915???
剩下6,
40,排队是2!=2个数字。所以X在6队之后第一个,4队里的第一个。这时候的全排列为:640604460406064046.
所以最后X
=2783915460
posted @ 2011-06-03 10:52  DiggingDeeply  阅读(1473)  评论(1编辑  收藏  举报