C解兩道題

來源:互聯網
上載者:User

偷懶間做了兩道題,供大家把玩吧。我的解法不見得最好,但是是可以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,8,7,6,5,4,3,1,0繼續站隊。繼續按照上面的方式排隊,確定排頭,那麼每隊的人數是8!=40320.那麼從後向前數的話,X應該在隊伍7裡第88640-40320*2=8000個位置之後。
好了,X的第2個數字也確定了,X=27????????。
繼續,9,8,6,5,4,3,1,0繼續站隊。同樣的確定排頭,每隊是7!=5040個。那麼X在隊伍8裡第8000-5040*1=2960的位置之後。所以,X的第3位元字定了,X=278???????
繼續,9,6,5,4,3,1,0排隊,每隊是6!=720,同樣,X在隊伍3裡第2960-720*4=80的位置之後。X的第4個數字確定,X=2783??????
繼續,9,6,5,4,1,0排隊,每隊是5!=120個數字,那麼毫無疑問,X在隊伍9裡第80-120*0=80的位置之後。X=27839?????
繼續,6,5,4,1,0排隊,每隊是4!=24個數字,X在隊伍隊伍1裡第80-24*3=8之後。X=278391????
還剩6,5,4,0,每隊是3!=6個 數字,那麼X在隊伍5裡第8-6*1=2之後。X=2783915???
剩下6,4,0,排隊是2!=2個數字。所以X在6隊之後第一個,4隊裡的第一個。這時候的全排列為:640,604,460,406,064,046.
所以最後X=2783915460
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.