锘??xml version="1.0" encoding="utf-8" standalone="yes"?> In 1949 the Indian mathematician D.R. Kaprekar discovered a class of numbers called self-numbers. For any positive integer n, define d(n) to be n plus the sum of the digits of n. (The d stands for digitadition, a term coined by Kaprekar.) For example, d(75) = 75 + 7 + 5 = 87. Given any positive integer n as a starting point, you can construct the infinite increasing sequence of integers n, d(n), d(d(n)), d(d(d(n))), .... For example, if you start with 33, the next number is 33 + 3 + 3 = 39, the next is 39 + 3 + 9 = 51, the next is 51 + 5 + 1 = 57, and so you generate the sequence 33, 39, 51, 57, 69, 84, 96, 111, 114, 120, 123, 129, 141, ... The number n is called a generator of d(n). In the sequence above, 33 is a generator of 39, 39 is a generator of 51, 51 is a generator of 57, and so on. Some numbers have more than one generator: for example, 101 has two generators, 91 and 100. A number with no generators is a self-number. There are thirteen self-numbers less than 100: 1, 3, 5, 7, 9, 20, 31, 42, 53, 64, 75, 86, and 97. Write a program to output all positive self-numbers less than 10000 in increasing order, one per line. When printing out a document, normally the first page is printed first, then the second, then the third, and so on until the end. However, when creating a fold-over booklet, the order of printing must be altered. A fold-over booklet has four pages per sheet, with two on the front and two on the back. When you stack all the sheets in order, then fold the booklet in half, the pages appear in the correct order as in a regular book. For example, a 4-page booklet would print on 1 sheet of paper: the front will contain page 4 then page 1, and the back will contain page 2 then page 3. Your task is to write a program that takes as input the number of pages to be printed, then generates the printing order. Each test case consists of a positive integer n on a line by itself, where n is the number of pages to be printed; n will not exceed 100. Output must be in ascending order by sheet, front first, then back.
聽聽聽 1銆佹敞鎰忓姵閫哥粨鍚堬紝闃叉鑲岃叡鍔蟲崯銆傞暱鏃墮棿鎿嶄綔鐢佃剳浼氬鑷存墜鎸囧叧鑺傘佹墜鑵曘佹墜鑷傝倢鑲夈佸弻鑲┿侀閮ㄣ佽儗閮ㄧ瓑閮ㄤ綅鍑虹幇閰歌儉鐤肩棝錛屽洜姝わ紝鎿嶄綔鑰呭湪宸ヤ綔涓灝忔椂鍚庡簲浼戞伅10鍒嗛挓錛屾垨鑰呭仛鍋氬伐闂存搷銆?
銆銆2銆佽娉ㄦ剰鐢ㄧ溂鍗敓銆傜溂鐫涗笌鏂囩ǹ銆佺溂鐫涗笌灞忓箷鐨勮窛紱誨簲淇濇寔鍦?0鍘樼背浠ヤ笂錛屾渶濂介噰鐢ㄥ厜涓嬭20搴︾殑瑙嗚銆傚伐浣滄椂錛屽簲鍦ㄩ潰閮ㄥ強鍙屾墜娑傛姽闃茶緪灝勭殑鎶よ偆娌廣?
銆銆3銆佸瓡濡囨瘡鍛ㄦ帴瑙︾數鑴戞椂闂翠笉瓚呰繃20灝忔椂銆傝闃叉闀挎椂闂村潗浣嶅紩璧風泦鑵旇娑叉粸鐣欎笉鐣咃紝鍋氬埌寮犻┌鏈夊害錛涜娉ㄦ剰鐢佃剳涓庡駭妞呭潗濮跨殑楂樹綆閰嶅悎錛岃鑳庡効鍋ュ悍鍙戣偛銆?
銆
銆4銆侀暱鏈熶粠浜嬬數鑴戞搷浣滆咃紝搴斿鍚冧竴浜涙柊椴滅殑钄彍鍜屾按鏋溿傚悓鏃跺鍔犵淮鐢熺礌A銆丅1銆丆銆丒鐨勬憚鍏ャ備負棰勯槻瑙掕啘騫茬嚗銆佺溂騫叉訂銆佽鍔涗笅闄嶃佺敋鑷沖嚭鐜板鐩茬棁絳夛紝
鐢佃剳鎿嶄綔鑰呭簲澶氬悆浜涘瘜鍚淮鐢熺礌A鐨勯鐗╋紝濡傝眴鍒跺彛銆侀奔銆佺墰濂躲佹牳妗冦侀潚鑿溿佸ぇ鐧借彍錛岃タ綰㈡熆銆佺┖蹇冭彍鍙婃柊椴滄按鏋滅瓑銆傜淮鐢熺礌C鍙互鏈夋晥鍦版姂鍒剁粏鑳炴哀鍖栥傜淮鐢熺礌
E涓昏浣滅敤鏄細闄嶄綆鑳嗗浐閱囷紝娓呴櫎韜綋鍐呭瀮鍦撅紝棰勯槻鐧藉唴闅溿傛牳妗冨拰鑺辯敓涓惈鏈変赴瀵岀殑緇寸敓绱燛銆傜淮鐢熺礌B1鍙互鍔犲己紲炵粡緇嗚優鐨勮惀鍏伙紝緙撹В紲炵粡鐨勭揣寮犵姸鎬併?
銆銆5銆佹瘡澶╂場鐐圭豢鑼躲傝尪鍙朵腑鐨勮剛澶氱硸錛屽彲鏀瑰杽鑲屼綋閫犺宸ヨ兘銆備漢浣撴敞鍏ヨ剛澶氱硸鍚庯紝鍦ㄧ煭鏃墮棿鍐呭嵆鍙寮烘満浣撻潪鐗瑰紓鎬у厤鐤姏銆傝尪鍙惰繕鑳介槻杈愬皠鎹熷銆?
銆銆6銆佷負浜嗛伩鍏嶈崸鍏夊睆鍙嶅厜鎴栦笉娓呮櫚錛岀數鑴戜笉搴旀斁緗湪紿楁埛鐨勫闈㈡垨鑳岄潰錛涚幆澧冪収鏄庤鏌斿拰錛屽鏋滄搷浣滆呰韓鍚庢湁紿楁埛搴旀媺涓婄獥甯橈紝閬垮厤浜厜鐩存帴鐓у皠鍒板睆騫曚笂鍙嶅悜鍑烘槑浜殑褰卞儚閫犳垚鐪奸儴鐨勭柌鍔熾?
銆銆7銆佺數鑴戞埧闂磋緇忓父閫氶錛屽湪瀹ゅ唴瀹夎鎹㈡皵鎵囨垨絀鴻皟錛屽噺杞繪捍姣斾簩鑻憢鍠冨韜綋鐨勫獎鍝嶃傝綆楁満闄勮繎鐨勭伆灝樺瘑搴﹁姣旀満鎴垮叾瀹冪┖闂撮珮鍑轟笂鐧懼嶏紝瀹冧滑闀挎椂闂撮檮鐫浜庝漢鐨勭毊鑲や笂錛屽彲瀵艱嚧鍚勭被鐨偆鐥呫傜數鑴戞埧闂磋淇濇寔娓呮磥鍗敓錛岀數鑴戣瀹氭湡鎿︽礂銆?
銆銆8銆佷竴鑸漢姣忓垎閽熺湪鐪煎皯浜?嬈′細浣跨溂鐫涘共鐕ャ備竴涓漢鍦ㄧ數鑴戝伐浣滄椂鐪ㄧ潧嬈℃暟鍙強騫蟲椂鐨勪笁鍒嗕箣涓錛屽洜鑰屽噺灝戜簡鐪煎唴娑︽粦鍓傚拰閰剁殑鍒嗘硨銆傚簲璇ュ鐪ㄧ溂錛屾瘡闅斾竴灝忔椂鑷沖皯璁╃溂鐫涗紤鎭竴嬈°?img src ="http://www.shnenglu.com/mahudu/aggbug/8769.html" width = "1" height = "1" />Output
1
3
5
7
9
20
31
42
53
64
|
| <-- a lot more numbers
|
9903
9914
9925
9927
9938
9949
9960
9971
9982
9993Solution
#include聽<iostream>
using聽namespace聽std;
const聽long聽N聽=聽10000;聽聽聽聽聽//鏈澶ц嚜鐒舵暟
char聽Arr[N聽+聽9*4]=
{0};聽聽聽//鏄惁鏄鎺掗櫎鐨勬暟瀛楋紵聽+9*4鏄負浜嗚鍐嶅鏀?浣嶆暟
long聽DealNum(long聽n)
{
聽聽long聽sum聽=聽n;
聽聽while聽(n聽!=聽0)
聽聽
{
聽聽聽聽sum聽+=聽n%10;
聽聽聽聽n聽/=聽10;
聽聽}
聽聽return聽sum;
}
int聽main()
{
聽聽int聽i;
聽聽for(i聽=聽1;聽i聽<聽N;聽i++)
聽聽
{
聽聽聽聽Arr[DealNum(i)]聽=聽1;
聽聽}
聽聽for(i聽=聽1;聽i聽<聽N;聽i++)
聽聽
{
聽聽聽聽if聽(!Arr[i])
聽聽聽聽聽聽聽聽cout<<i<<endl;
聽聽}
聽聽return聽0;
}
Front Back
------------- -------------
| | | | | |
| 4 | 1 | | 2 | 3 |
| | | | | |
------------- -------------Input聽
The input file contains one or more test cases, followed by a line containing the number 0 that indicates the end of the file.
Output聽
For each test case, output a report indicating which pages should be printed on each sheet, exactly as shown in the example. If the desired number of pages does not completely fill up a sheet, then print the word Blank in place of a number. If the front or back of a sheet is entirely blank, do not generate output for that side of the sheet.
Sample Input聽
1
14
4
0Sample Output聽
Printing order for 1 pages:
Sheet 1, front: Blank, 1
Printing order for 14 pages:
Sheet 1, front: Blank, 1
Sheet 1, back : 2, Blank
Sheet 2, front: 14, 3
Sheet 2, back : 4, 13
Sheet 3, front: 12, 5
Sheet 3, back : 6, 11
Sheet 4, front: 10, 7
Sheet 4, back : 8, 9
Printing order for 4 pages:
Sheet 1, front: 4, 1
Sheet 1, back : 2, 3Solution
#include聽<iostream>
using聽namespace聽std;
#define聽PAGES聽100
typedef聽struct聽side
{聽聽聽聽
聽聽聽聽int聽left,right;
}side;
typedef聽struct聽sheet
{
聽聽聽聽side聽front;
聽聽聽聽side聽back;聽聽聽聽
}sheet;
int聽numSides;
sheet聽sheets[PAGES];
void聽PrintPages(int聽numSides)
{
聽聽聽聽int聽numSidesNew;聽聽聽聽
聽聽聽聽int聽add,pages;
聽聽聽聽add聽=聽numSides%4;
聽聽聽聽if(add聽!=聽0)
{
聽聽聽聽聽聽聽聽numSidesNew聽=聽numSides聽+聽4聽-聽add;聽聽聽聽//聽澧炲姞鍚庣殑鎬婚潰鏁?numSides涓哄疄闄呯殑鎬婚潰鏁?/span>
聽聽聽聽}
聽聽聽聽else
聽聽聽聽聽聽聽聽numSidesNew聽=聽numSides;
聽聽聽聽pages聽=聽numSidesNew聽/聽4;聽聽聽聽//聽鎬葷焊寮犳暟
聽聽聽聽for(int聽i聽=聽0;聽i聽<聽pages;聽i++)
{
聽聽聽聽聽聽聽聽sheets[i].front.left聽=聽numSidesNew聽-聽2*i;
聽聽聽聽聽聽聽聽if(sheets[i].front.left聽>聽numSides)
{
聽聽聽聽聽聽聽聽聽聽聽聽sheets[i].front.left聽=聽0;聽聽聽聽//聽琛ㄦ槑搴斾負blank
聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽sheets[i].front.right聽=聽2*i+1;
聽聽聽聽聽聽聽聽if(sheets[i].front.right聽>聽numSides)
{
聽聽聽聽聽聽聽聽聽聽聽聽sheets[i].front.right聽=聽0;聽聽聽聽//聽琛ㄦ槑搴斾負blank
聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽sheets[i].back.left聽=聽2*(i+1);
聽聽聽聽聽聽聽聽if(sheets[i].back.left聽>聽numSides)
{
聽聽聽聽聽聽聽聽聽聽聽聽sheets[i].back.left聽=聽0;聽聽聽聽//聽琛ㄦ槑搴斾負blank
聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽sheets[i].back.right聽=聽numSidesNew聽-聽2*i聽-聽1;
聽聽聽聽聽聽聽聽if(sheets[i].back.right聽>聽numSides)
{
聽聽聽聽聽聽聽聽聽聽聽聽sheets[i].back.right聽=聽0;
聽聽聽聽聽聽聽聽}
聽聽聽聽}
聽聽聽聽cout聽<<聽"Printing聽order聽for聽"聽<<聽numSides聽<<聽"聽pages:"聽<<聽endl;
聽聽聽聽for(int聽j聽=聽0;聽j聽<聽pages;聽j++)
{
聽聽聽聽聽聽聽聽if(sheets[j].front.left聽||聽sheets[j].front.right)
{
聽聽聽聽聽聽聽聽聽聽聽聽cout聽<<聽"Sheet聽"聽<<聽j+1聽<<",聽front:聽";
聽聽聽聽聽聽聽聽聽聽聽聽if(sheets[j].front.left)
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽cout聽<<聽sheets[j].front.left聽<<聽",";
聽聽聽聽聽聽聽聽聽聽聽聽else
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽cout聽<<聽"Blank,";
聽聽聽聽聽聽聽聽聽聽聽聽cout聽<<聽"聽";
聽聽聽聽聽聽聽聽聽聽聽聽if(sheets[j].front.right)
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽cout聽<<聽sheets[j].front.right;
聽聽聽聽聽聽聽聽聽聽聽聽else
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽cout聽<<聽"Blank,";
聽聽聽聽聽聽聽聽聽聽聽聽cout聽<<聽endl;
聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽if(sheets[j].back.left聽||聽sheets[j].back.right)
{
聽聽聽聽聽聽聽聽聽聽聽聽cout聽<<聽"Sheet聽"聽<<聽j+1聽<<",聽back聽:聽";
聽聽聽聽聽聽聽聽聽聽聽聽if(sheets[j].back.left)
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽cout聽<<聽sheets[j].back.left聽<<聽",";
聽聽聽聽聽聽聽聽聽聽聽聽else
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽cout聽<<聽"Blank,";
聽聽聽聽聽聽聽聽聽聽聽聽cout聽<<聽"聽";
聽聽聽聽聽聽聽聽聽聽聽聽if(sheets[j].back.right)
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽cout聽<<聽sheets[j].back.right;
聽聽聽聽聽聽聽聽聽聽聽聽else
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽cout聽<<聽"Blank";
聽聽聽聽聽聽聽聽聽聽聽聽cout聽<<聽endl;
聽聽聽聽聽聽聽聽}
聽聽聽聽}
}
int聽main()
{
聽聽聽聽int聽numSides;
聽聽聽聽while(cin聽>>聽numSides)
{
聽聽聽聽聽聽聽聽if(numSides聽==聽0)
{
聽聽聽聽聽聽聽聽聽聽聽聽break;
聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽PrintPages(numSides);
聽聽聽聽}
聽聽聽聽return聽0;
}
鏃ュ墠鍦ㄤ功涓婄湅鍒頒竴孌典嬌鐢ㄥ欏瑰紡閫艱繎璁$畻騫蟲柟鏍圭殑浠g爜錛岃嚦浠婇兘娌℃悶鏄庣櫧浣滆呮槸鎬庢牱鎺?br />綆楀嚭閭d釜鍏紡鐨勩備絾鍦ㄥ皾璇曡В鍐抽棶棰樼殑榪囩▼涓紝瀛﹀埌浜嗕笉灝戜笢瑗匡紝浜庢槸渚挎湁浜嗚繖綃囧績
寰楋紝鍐欏嚭鏉ュ拰澶у鍏變韓銆傚叾涓湁閿欐紡鐨勫湴鏂癸紝榪樿澶у澶氬鎸囨暀銆?br />
鐨勭‘錛屾濡傝澶氫漢鎵璇寸殑閭f牱錛岀幇鍦ㄦ湁鏈塅PU錛屾湁3DNow錛屾湁SIMD錛岃璁鴻蔣浠剁畻娉曞ソ鍍忎笉
鍚堟椂瀹溿傚叧浜巗qrt鐨勮瘽棰樺叾瀹炴棭鍦?003騫翠究宸插湪聽GameDev.net涓婂緱鍒頒簡騫挎硾鐨勮璁猴紙鍙?br />瑙佹垜瀹炲湪闈炲父鐏槦浜嗭紝褰撶劧涓嶆帓闄よ繕鏈夊叾浠栧皻鍦ㄥ啣鐜嬫槦鐨勪漢錛屽樋鍢匡級銆傝屽皾璇曟帰絀惰璇?br />棰樺垯瀹屽叏鏄嚭浜庢湰浜虹殑鍏磋叮鍜屽ソ濂囧績錛堟崲鍙ヨ瘽璇村氨鏄棤鐭ワ級銆?br />
鎴戝彧鏄釜beginner錛屾墍浠ヨ繖縐嶅ぇ鏄ぇ闈炵殑闂鎴戜篃璇翠笉娓呮錛堝湪GameDev.net涓婁篃鏈夊緢澶?br />綾諱技鐨勪簤璁猴級銆備絾鏃犺濡備綍錛孋armack鍦―OOM3涓繕鏄嬌鐢ㄤ簡杞歡綆楁硶錛岃屽鐭ラ亾涓鐐規暟
瀛︾煡璇嗗3D緙栫▼鏉ヨ涔熷彧鏈夊ソ澶勬病鍧忓銆?D鍥懼艦緙栫▼鍏跺疄灝辨槸鏁板錛屾暟瀛︼紝榪樻槸鏁板銆?br />
鏂囩珷鍘熸湰鏄敤HTML緙栨帓鐨勶紝鎵浠ュ彧鎴彇浜嗛儴鍒嗘湁姣旇緝鏈夎叮鐨勪笢瑗挎斁鍦ㄨ繖閲屻傚師鏂囧湪鎴戠殑
涓漢涓婚〉涓婏紝鍚屾椂涔熸彁渚涗簡2綃囪鏂囩殑涓嬭澆錛歨ttp:
//greatsorcerer.go2.icpcn.com/info/fastsqrt.html
=========================================================
鍦?D鍥懼艦緙栫▼涓紝緇忓父瑕佹眰騫蟲柟鏍規垨騫蟲柟鏍圭殑鍊掓暟錛屼緥濡傦細姹傚悜閲忕殑闀垮害鎴栧皢鍚戦噺褰掍竴
鍖栥侰鏁板鍑芥暟搴撲腑鐨剆qrt鍏鋒湁鐞嗘兂鐨勭簿搴︼紝浣嗗浜?D娓告垙紼嬪紡鏉ヨ閫熷害澶參銆傛垜浠笇鏈?br />鑳藉鍦ㄤ繚璇佽凍澶熺殑綺懼害鐨勫悓鏃訛紝榪涗竴姝ユ彁楂橀熷害銆?br />
Carmack鍦≦UAKE3涓嬌鐢ㄤ簡涓嬮潰鐨勭畻娉曪紝瀹冪涓嬈″湪鍏紬鍦哄悎鍑虹幇鐨勬椂鍊欙紝鍑犱箮闇囦綇浜嗘墍
鏈夌殑浜恒傛嵁璇磋綆楁硶鍏跺疄騫朵笉鏄疌armack鍙戞槑鐨勶紝瀹冪湡姝g殑浣滆呮槸Nvidia鐨凣ary聽Tarolli
錛堟湭緇忚瘉瀹烇級銆?br />錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛?br />//
//聽璁$畻鍙傛暟x鐨勫鉤鏂規牴鐨勫掓暟
//
float聽InvSqrt聽(float聽x)
{
float聽xhalf聽=聽0.5f*x;
int聽i聽=聽*(int*)&x;
i聽=聽0x5f3759df聽-聽(i聽>>聽1);聽//聽璁$畻絎竴涓繎浼兼牴
x聽=聽*(float*)&i;
x聽=聽x*(1.5f聽-聽xhalf*x*x);聽//聽鐗涢】榪唬娉?br />return聽x;
}
錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞
璇ョ畻娉曠殑鏈川鍏跺疄灝辨槸鐗涢】榪唬娉曪紙Newton-Raphson聽Method錛岀畝縐癗R錛夛紝鑰孨R鐨勫熀紜鍒?br />鏄嘲鍕掔駭鏁幫紙Taylor聽Series錛夈侼R鏄竴縐嶆眰鏂圭▼鐨勮繎浼兼牴鐨勬柟娉曘傞鍏堣浼拌涓涓笌鏂?br />紼嬬殑鏍規瘮杈冮潬榪戠殑鏁板鹼紝鐒跺悗鏍規嵁鍏紡鎺ㄧ畻涓嬩竴涓洿鍔犺繎浼肩殑鏁板鹼紝涓嶆柇閲嶅鐩村埌鍙互
鑾峰緱婊℃剰鐨勭簿搴︺傚叾鍏紡濡備笅錛?br />錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛?br />鍑芥暟錛歽=f(x)
鍏朵竴闃跺鏁頒負錛歽'=f'(x)
鍒欐柟紼嬶細f(x)=0聽鐨勭n+1涓繎浼兼牴涓?br />x[n+1]聽=聽x[n]聽-聽f(x[n])聽/聽f'(x[n])
錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛?br />NR鏈鍏抽敭鐨勫湴鏂瑰湪浜庝及璁$涓涓繎浼兼牴銆傚鏋滆榪戜技鏍逛笌鐪熸牴瓚沖闈犺繎鐨勮瘽錛岄偅涔堝彧闇
瑕佸皯鏁板嚑嬈¤凱浠o紝灝卞彲浠ュ緱鍒版弧鎰忕殑瑙c?br />
鐜板湪鍥炶繃澶存潵鐪嬬湅濡備綍鍒╃敤鐗涢】娉曟潵瑙e喅鎴戜滑鐨勯棶棰樸傛眰騫蟲柟鏍圭殑鍊掓暟錛屽疄闄呭氨鏄眰鏂?br />紼?/(x^2)-a=0鐨勮В銆傚皢璇ユ柟紼嬫寜鐗涢】榪唬娉曠殑鍏紡灞曞紑涓猴細
x[n+1]=1/2*x[n]*(3-a*x[n]*x[n])
灝?/2鏀懼埌鎷彿閲岄潰錛屽氨寰楀埌浜嗕笂闈㈤偅涓嚱鏁扮殑鍊掓暟絎簩琛屻?br />
鎺ョ潃錛屾垜浠璁炬硶浼拌絎竴涓繎浼兼牴銆傝繖涔熸槸涓婇潰鐨勫嚱鏁版渶紲炲鐨勫湴鏂廣傚畠閫氳繃鏌愮鏂?br />娉曠畻鍑轟簡涓涓笌鐪熸牴闈炲父鎺ヨ繎鐨勮繎浼兼牴錛屽洜姝ゅ畠鍙渶瑕佷嬌鐢ㄤ竴嬈¤凱浠h繃紼嬪氨鑾峰緱浜嗚緝婊?br />鎰忕殑瑙c傚畠鏄庢牱鍋氬埌鐨勫憿錛熸墍鏈夌殑濂ュ灝卞湪浜庤繖涓琛岋細
i聽=聽0x5f3759df聽-聽(i聽>>聽1);聽//聽璁$畻絎竴涓繎浼兼牴
瓚呯駭鑾悕鍏跺鐨勮鍙ワ紝涓嶆槸鍚楋紵浣嗕粩緇嗘兂涓涓嬬殑璇濓紝榪樻槸鍙互鐞嗚В鐨勩傛垜浠煡閬擄紝IEEE
鏍囧噯涓嬶紝float綾誨瀷鐨勬暟鎹湪32浣嶇郴緇熶笂鏄繖鏍瘋〃紺虹殑錛堝ぇ浣撴潵璇村氨鏄繖鏍鳳紝浣嗙渷鐣ヤ簡寰?br />澶氱粏鑺傦紝鏈夊叴瓚e彲浠OOGLE錛夛細
錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛?br />bits錛?1聽30聽...聽0
31錛氱鍙蜂綅
30-23錛氬叡8浣嶏紝淇濆瓨鎸囨暟錛圗錛?br />22-0錛氬叡23浣嶏紝淇濆瓨灝炬暟錛圡錛?br />錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛?br />鎵浠ワ紝32浣嶇殑嫻偣鏁扮敤鍗佽繘鍒跺疄鏁拌〃紺哄氨鏄細M*2^E銆傚紑鏍圭劧鍚庡掓暟灝辨槸錛歁^(-1/2)*2^
(-E/2)銆傜幇鍦ㄥ氨鍗佸垎娓呮櫚浜嗐傝鍙>聽>1鍏跺伐浣滃氨鏄皢鎸囨暟闄や互2錛屽疄鐜?^(E/2)鐨勯儴鍒嗐?br />鑰屽墠闈㈢敤涓涓父鏁板噺鍘誨畠錛岀洰鐨勫氨鏄緱鍒癕^(1/2)鍚屾椂鍙嶈漿鎵鏈夋寚鏁扮殑絎﹀彿銆?br />
鑷充簬閭d釜0x5f3759df錛屽憙錛屾垜鍙兘璇達紝鐨勭‘鏄竴涓秴綰х殑Magic聽Number銆?br />
閭d釜Magic聽Number鏄彲浠ユ帹瀵煎嚭鏉ョ殑錛屼絾鎴戝茍涓嶆墦綆楀湪榪欓噷璁ㄨ錛屽洜涓哄疄鍦ㄥお綣佺悙浜嗐?br />綆鍗曟潵璇達紝鍏跺師鐞嗗涓嬶細鍥犱負IEEE鐨勬誕鐐規暟涓紝灝炬暟M鐪佺暐浜嗘渶鍓嶉潰鐨?錛屾墍浠ュ疄闄呯殑灝?br />鏁版槸1+M銆傚鏋滀綘鍦ㄥぇ瀛︿笂鏁板璇炬病鏈夋墦鐬岀潯鐨勮瘽錛岄偅涔堝綋浣犵湅鍒?1+M)^(-1/2)榪欐牱鐨勫艦
寮忔椂錛屽簲璇ヤ細椹笂鑱旀兂鐨勫埌瀹冪殑娉板嫆綰ф暟灞曞紑錛岃岃灞曞紑寮忕殑絎竴欏瑰氨鏄父鏁般備笅闈㈢粰
鍑虹畝鍗曠殑鎺ㄥ榪囩▼錛?br />錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛?br />瀵逛簬瀹炴暟R>0錛屽亣璁懼叾鍦↖EEE鐨勬誕鐐硅〃紺轟腑錛?br />鎸囨暟涓篍錛屽熬鏁頒負M錛屽垯錛?br />
R^(-1/2)
=聽(1+M)^(-1/2)聽*聽2^(-E/2)
灝?1+M)^(-1/2)鎸夋嘲鍕掔駭鏁板睍寮錛屽彇絎竴欏癸紝寰楋細
鍘熷紡
=聽(1-M/2)聽*聽2^(-E/2)
=聽2^(-E/2)聽-聽(M/2)聽*聽2^(-E/2)
濡傛灉涓嶈冭檻鎸囨暟鐨勭鍙風殑璇濓紝
(M/2)*2^(E/2)姝f槸(R>>1)錛?br />鑰屽湪IEEE琛ㄧず涓紝鎸囨暟鐨勭鍙峰彧闇綆鍗曞湴鍔犱笂涓涓亸縐誨嵆鍙紝
鑰屽紡瀛愮殑鍓嶅崐閮ㄥ垎鍒氬ソ鏄釜甯告暟錛屾墍浠ュ師寮忓彲浠ヨ漿鍖栦負錛?br />
鍘熷紡聽=聽C聽-聽(M/2)*2^(E/2)聽=聽C聽-聽(R>>1)錛屽叾涓瑿涓哄父鏁?br />
鎵浠ュ彧闇瑕佽В鏂圭▼錛?br />R^(-1/2)
=聽(1+M)^(-1/2)聽*聽2^(-E/2)
=聽C聽-聽(R>>1)
姹傚嚭浠ゅ埌鐩稿璇樊鏈灝忕殑C鍊煎氨鍙互浜?br />錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛?br />涓婇潰鐨勬帹瀵艱繃紼嬪彧鏄垜涓漢鐨勭悊瑙o紝騫舵湭寰楀埌璇佸疄銆傝孋hris聽Lomont鍒欏湪浠栫殑璁烘枃涓
緇嗚璁轟簡鏈鍚庨偅涓柟紼嬬殑瑙f硶錛屽茍灝濊瘯鍦ㄥ疄闄呯殑鏈哄櫒涓婂鎵炬渶浣崇殑甯告暟C銆傛湁鍏磋叮鐨勬湅鍙?br />鍙互鍦ㄦ枃鏈壘鍒頒粬鐨勮鏂囩殑閾炬帴銆?br />
鎵浠ワ紝鎵璋撶殑Magic聽Number錛屽茍涓嶆槸浠嶯鍏冨畤瀹欑殑鏌愪釜鏄熺郴鐢變簬鏃剁┖鎵洸鑰屾帀鍒板湴鐞冧笂
鐨勶紝鑰屾槸鍑犵櫨騫村墠灝辨湁鐨勬暟瀛︾悊璁恒傚彧瑕佺啛鎮塏R鍜屾嘲鍕掔駭鏁幫紝浣犳垜鍚屾牱鏈夎兘鍔涗綔鍑虹被浼?br />鐨勪紭鍖栥?br />
鍦℅ameDev.net聽涓婃湁浜哄仛榪囨祴璇曪紝璇ュ嚱鏁扮殑鐩稿璇樊綰︿負0.177585%錛岄熷害姣擟鏍囧噯搴撶殑
sqrt鎻愰珮瓚呰繃20%銆傚鏋滃鍔犱竴嬈¤凱浠h繃紼嬶紝鐩稿璇樊鍙互闄嶄綆鍒癳-聽004聽鐨勭駭鏁幫紝浣嗛?br />搴︿篃浼氶檷鍒板拰sqrt宸笉澶氥傛嵁璇村湪DOOM3涓紝Carmack閫氳繃鏌ユ壘琛ㄨ繘涓姝ヤ紭鍖栦簡璇ョ畻娉曪紝
綺懼害榪戜箮瀹岀編錛岃屼笖閫熷害涔熸瘮鍘熺増鎻愰珮浜嗕竴鎴紙姝e湪鍔姏寮勬簮鐮侊紝璋佹湁鍙戞垜涓浠斤級銆?br />
鍊煎緱娉ㄦ剰鐨勬槸錛屽湪Chris聽Lomont鐨勬紨綆椾腑錛岀悊璁轟笂鏈浼樼鐨勫父鏁幫紙綺懼害鏈楂橈級鏄?br />0x5f37642f錛屽茍涓斿湪瀹為檯嫻嬭瘯涓紝濡傛灉鍙嬌鐢ㄤ竴嬈¤凱浠g殑璇濓紝鍏舵晥鏋滀篃鏄渶濂界殑銆備絾濂?br />鎬殑鏄紝緇忚繃涓ゆNR鍚庯紝鍦ㄨ甯告暟涓嬭В鐨勭簿搴﹀皢闄嶄綆寰楅潪甯稿帀瀹籌紙澶╃煡閬撴槸鎬庝箞鍥?br />浜嬶紒錛夈傜粡榪囧疄闄呯殑嫻嬭瘯錛孋hris聽Lomont璁や負錛屾渶浼樼鐨勫父鏁版槸0x5f375a86銆傚鏋滄崲鎴?br />64浣嶇殑double鐗堟湰鐨勮瘽錛岀畻娉曡繕鏄竴鏍風殑錛岃屾渶浼樺父鏁板垯涓郝?x5fe6ec85e7de30da錛堝張涓
涓護浜哄啋姹楃殑Magic聽Number聽-聽-b錛夈?br />
榪欎釜綆楁硶渚濊禆浜庢誕鐐規暟鐨勫唴閮ㄨ〃紺哄拰瀛楄妭欏哄簭錛屾墍浠ユ槸涓嶅叿縐繪鎬х殑銆傚鏋滄斁鍒癕ac涓婅窇
灝變細鎸傛帀銆傚鏋滄兂鍏峰鍙Щ妞嶆э紝榪樻槸涔栦箹鐢╯qrt濂戒簡銆備絾綆楁硶鎬濇兂鏄氱敤鐨勩傚ぇ瀹跺彲
浠ュ皾璇曟帹綆椾竴涓嬬浉搴旂殑騫蟲柟鏍圭畻娉曘?br />
涓嬮潰緇欏嚭Carmack鍦≦UAKE3涓嬌鐢ㄧ殑騫蟲柟鏍圭畻娉曘侰armack宸茬粡灝哘UAKE3鐨勬墍鏈夋簮浠g爜鎹?br />緇欏紑婧愪簡錛屾墍浠ュぇ瀹跺彲浠ユ斁蹇冧嬌鐢紝涓嶇敤鎷呭績浼氬彈鍒板緥甯堜俊銆?br />錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛嶏紞錛?br />//
//聽Carmack鍦≦UAKE3涓嬌鐢ㄧ殑璁$畻騫蟲柟鏍圭殑鍑芥暟
//
float聽CarmSqrt(float聽x){
union{
int聽intPart;
float聽floatPart;
}聽convertor;
union{
int聽intPart;
float聽floatPart;
}聽convertor2;
convertor.floatPart聽=聽x;
convertor2.floatPart聽=聽x;
convertor.intPart聽=聽0x1FBCF800聽+聽(convertor.intPart聽>>聽1);
convertor2.intPart聽=聽0x5f3759df聽-聽(convertor2.intPart聽>>聽1);
return聽0.5f*(convertor.floatPart聽+聽(x聽*聽convertor2.floatPart));
}
Write a program to calculate the Fibonacci Numbers.
The input to your program would be a sequence of numbers smaller or equal than 5000, each on a separate line, specifying which Fibonacci number to calculate.
Your program should output the Fibonacci number for each input value, one per line.
5
7
11
The Fibonacci number for 5 is 5
The Fibonacci number for 7 is 13
The Fibonacci number for 11 is 89
#include
<iostream>
using
namespace
std;
int
main()
{
聽聽
int
first,next,temp,n;
聽聽
while(cin >> n) {
聽聽聽聽聽
first = 0;
聽聽聽聽聽
next = 1;
聽聽聽聽聽
temp = 0;
聽聽聽聽聽
if(n == 0 || n == 1) {
聽聽聽聽聽聽聽
cout << "The Fibonacci number for" << " " << n << " " << "is" << " " << n << endl;
聽聽聽聽聽 }
聽聽聽聽聽
else {
聽聽聽聽聽聽聽
for(inti = 2; i <= n; i++) {
聽聽聽聽聽聽聽聽聽聽
temp = first + next;
聽聽聽聽聽聽聽聽聽聽
first = next;
聽聽聽聽聽聽聽聽聽聽
next = temp;
聽聽聽聽聽聽聽 }
聽聽聽聽聽聽聽
cout << "The Fibonacci number for" << " " << n << " " << "is" << " " << temp << endl;
聽聽聽聽聽 }
聽聽 }
聽聽
return 0;
}
Problems in Computer Science are often classified as belonging to a certain class of problems (e.g., NP, Unsolvable, Recursive). In this problem you will be analyzing a property of an algorithm whose classification is not known for all possible inputs.
Consider the following algorithm:
1. input n
2. print n
3. if n = 1 then STOP
4. if n is odd then tex2html_wrap_inline44
5. else tex2html_wrap_inline46
6. GOTO 2
Given the input 22, the following sequence of numbers will be printed 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
It is conjectured that the algorithm above will terminate (when a 1 is printed) for any integral input value. Despite the simplicity of the algorithm, it is unknown whether this conjecture is true. It has been verified, however, for all integers n such that 0 < n < 1,000,000 (and, in fact, for many more numbers than this.)
Given an input n, it is possible to determine the number of numbers printed (including the 1). For a given n this is called the cycle-length of n. In the example above, the cycle length of 22 is 16.
For any two numbers i and j you are to determine the maximum cycle length over all numbers between i and j.
The input will consist of a series of pairs of integers i and j, one pair of integers per line. All integers will be less than 1,000,000 and greater than 0.
You should process all pairs of integers and for each pair determine the maximum cycle length over all integers between and including i and j.
You can assume that no opperation overflows a 32-bit integer.
For each pair of input integers i and j you should output i, j, and the maximum cycle length for integers between and including i and j. These three numbers should be separated by at least one space with all three numbers on one line and with one line of output for each line of input. The integers i and j must appear in the output in the same order in which they appeared in the input and should be followed by the maximum cycle length (on the same line).
1 10
100 200
201 210
900 1000
1 10 20
100 200 125
201 210 89
900 1000 174
#include
<iostream>
using
namespace
std;
int
cycle(intm)
{
聽聽
int
i = 1;
聽聽
while (m != 1){
聽聽聽聽聽
if(m%2)
聽聽聽聽聽聽聽
m = m*3 + 1;
聽聽聽聽聽
else
聽聽聽聽聽聽聽
m /= 2;
聽聽聽聽聽
i++;
聽聽 }
聽聽
return
i;
}聽聽
int
main()
{
聽聽
int
m,n,max,temp;
聽聽
int
mOriginal,nOriginal;
聽聽
int
i;
聽聽
while (cin >> m >> n){
聽聽聽聽聽
mOriginal = m;
聽聽聽聽聽
nOriginal = n;
聽聽聽聽聽
if (m > n){
聽聽聽聽聽聽聽
temp = m;
聽聽聽聽聽聽聽
m = n;
聽聽聽聽聽聽聽
n = temp;
聽聽聽聽聽 }
聽聽聽聽聽
max = cycle(m);
聽聽聽聽聽
for (i = m+1; i <= n; i++){
聽聽聽聽聽聽聽
temp = cycle(i);
聽聽聽聽聽聽聽
if (temp > max){
聽聽聽聽聽聽聽聽聽聽
max = temp;
聽聽聽聽聽聽聽 }
聽聽聽聽聽 }聽
聽聽聽聽聽
cout << mOriginal << " " << nOriginal << " " << max << endl;
聽聽 }
聽聽
return 0;
}