非常有意思的幾個算術、算法題

1.題目標題:高斯日記

 

大數學家高斯有個好習慣:無論如何都要記日記。

    他的日記有個與衆不同的地方,他從不註明年月日,而是用一個整數代替,比如:4210

    後來人們知道,那個整數就是日期,它表示那一天是高斯出生後的第幾天。這或許也是個好習慣,它時時刻刻提醒着主人:日子又過去一天,還有多少時光可以用於浪費呢?

    高斯出生於:1777年4月30日。
    
    在高斯發現的一個重要定理的日記上標註着:5343,因此可算出那天是:1791年12月15日。

    高斯獲得博士學位的那天日記上標着:8113  

    請你算出高斯獲得博士學位的年月日。

提交答案的格式是:yyyy-mm-dd, 例如:1980-03-21

請嚴格按照格式,通過瀏覽器提交答案。
注意:只提交這個日期,不要寫其它附加內容,比如:說明性的文字。

分析:這實際就是“三天打魚、兩天曬網問題”。

程序實現:(java寫的)

[java] view plaincopy
  1. package Forth;  
  2.   
  3. public class DateOfNdays {  
  4. /* 
  5.  * 高斯日記(給定日期,算出X天后的日期) 
  6.  */  
  7.     /* 
  8.      * 是否是閏年 
  9.      */  
  10.     boolean IsleapYear(int year){  
  11.         return (year % 400 == 0 || year % 4 == 0 && year % 100 != 0);  
  12.     }  
  13.       
  14.     /* 
  15.      * 獲得某年某月的最大天數 
  16.      */  
  17.     int GetMaxDay(int year,int month,int day){  
  18.         switch(month){  
  19.         case 1:  
  20.         case 3:  
  21.         case 5:  
  22.         case 7:  
  23.         case 8:  
  24.         case 10:  
  25.         case 12:  
  26.             return 31;  
  27.         case 4:  
  28.         case 6:  
  29.         case 9:  
  30.         case 11:  
  31.             return 30;  
  32.         case 2:  
  33.             return (IsleapYear(year)?29:28);  
  34.         default:  
  35.             return -1;  
  36.         }  
  37.     }  
  38.     /* 
  39.      * 獲得X天后的日期 
  40.      */  
  41.     void GetXDays(int year,int month,int day,int X){  
  42.         for(int i = 1; i <= X; i++){  
  43.             if(day != GetMaxDay(year,month,day)){  
  44.                 day++;  
  45.             }else{  
  46.                 if(month != 12){  
  47.                     month++;  
  48.                     day = 1;  
  49.                 }else{  
  50.                     month = day = 1;  
  51.                     year++;  
  52.                 }  
  53.             }  
  54.                   
  55.         }  
  56.         System.out.println(X+"天后的日期是"+year+"/"+month+"/"+day);  
  57.     }  
  58.   
  59. }  

答案:1799-7-16

 

2.題目標題: 排它平方數

    小明正看着 203879 這個數字發呆。

    原來,203879 * 203879 = 41566646641

    這有什麼神奇呢?仔細觀察,203879 是個6位數,並且它的每個數位上的數字都是不同的,並且它平方後的所有數位上都不出現組成它自身的數字。

    具有這樣特點的6位數還有一個,請你找出它!

    再歸納一下篩選要求:
    1. 6位正整數
    2. 每個數位上的數字不同
    3. 其平方數的每個數位不含原數字的任何組成數位

答案是一個6位的正整數。

請通過瀏覽器提交答案。
注意:只提交另一6位數,題中已經給出的這個不要提交。
注意:不要書寫其它的內容(比如:說明性的文字)。

[java] view plaincopy
  1. package Forth;  
  2.   
  3. public class ExcludeNumber {  
  4. /* 
  5.  * 排它平方數 
  6.  */  
  7.     long PingNum = 0//存儲平方  
  8.       
  9.     /* 
  10.      * 判斷一個數是否有重複數字 
  11.      */  
  12.     public boolean IsSame(long x){  
  13.         long store[] = new long[7];  
  14.         long r;  
  15.         int i = 0;  
  16.         while(x != 0){ //分離該數的每一位,存入store數組  
  17.             r = x % 10;  
  18.             x = x / 10;  
  19.             store[i] = r;  
  20.             i++;  
  21.         }  
  22.           
  23.         long result;  
  24.         for(int j = 0; j < 5; j++){ //判斷有無重複數  
  25.             result = store[j];  
  26.             for(int k = j+1; k < 6; k++){  
  27.                 if(result == store[k])  
  28.                     return true;  
  29.             }  
  30.         }  
  31.           
  32.         return false;  
  33.     }  
  34.     public boolean IsSame2(long Num,long n){  
  35.         long store[] = new long[7];  
  36.         long r;  
  37.         int i = 0;  
  38.         while( n != 0){  
  39.             r = n % 10;  
  40.             n = n / 10;  
  41.             store[i] = r;  
  42.             i++;  
  43.         }  
  44.           
  45.         long storeping[] = new long[18];  
  46.         i = 0;  
  47.         while(Num != 0){  
  48.             r = Num % 10;  
  49.             Num = Num / 10;  
  50.             storeping[i] = r;  
  51.             i++;  
  52.         }  
  53.                   
  54.         for(int k = 0; k < 6; k++){   //將該數的每一位與其平方的每一位比較,看是否有相同的數  
  55.             for(int j = 0; j < i; j++){  
  56.                 if(store[k] == storeping[j])  
  57.                     return true;  
  58.             }  
  59.         }  
  60.           
  61.         return false;  
  62.     }  
  63.       
  64.     public void selectNum(){ //篩選排他數  
  65.           
  66.         for(long n = 100000; n <= 999999;n++){  
  67.             if(IsSame(n))  //有相同的數字,則跳過  
  68.                 continue;  
  69.             else{  
  70.                 PingNum = n*n;  
  71.                 if(IsSame2(PingNum,n)) //該數的平方中是否有與該數相同的數字  
  72.                     continue;  
  73.                 else                   //符合條件,則打印  
  74.                     System.out.println(n);  
  75.             }  
  76.         }  
  77.     }  
  78. }  

答案:639172

3.標題: 振興中華

    小明參加了學校的趣味運動會,其中的一個項目是:跳格子。

    地上畫着一些格子,每個格子裏寫一個字,如下所示:(也可參見p1.jpg)

從我做起振
我做起振興
做起振興中
起振興中華


    比賽時,先站在左上角的寫着“從”字的格子裏,可以橫向或縱向跳到相鄰的格子裏,但不能跳到對角的格子或其它位置。一直要跳到“華”字結束。


    要求跳過的路線剛好構成“從我做起振興中華”這句話。

    請你幫助小明算一算他一共有多少種可能的跳躍路線呢?

答案是一個整數,請通過瀏覽器直接提交該數字。
注意:不要提交解答過程,或其它輔助說明類的內容。

[java] view plaincopy
  1. /* 
  2.  * 振興中華 
  3.  */  
  4.     int array[][] =   
  5.            {{0,1,2,3,4},  
  6.             {1,2,3,4,5},  
  7.             {2,3,4,5,6},  
  8.             {3,4,5,6,7}};  
  9.     int copyarray[][] = new int[4][5]; //顯示路徑的拷貝數組  
  10.     int startI = 0, startJ = 0;  // 入口  
  11.     int endI = 3, endJ = 4;  // 出口  
  12.     int success = 0;  
  13.     int count = 0;  
  14.   
  15.       
  16.     /* 
  17.      * 初始化拷貝數組 
  18.      */  
  19.     public void initcopy(){   
  20.         for(int k = 0; k < 4; k++){  
  21.             for(int d = 0; d < 5; d++){  
  22.                 copyarray[k][d] = 10;  
  23.             }  
  24.         }  
  25.     }  
  26.     /* 
  27.      * 遍歷訪問 
  28.      */  
  29.     public void visit(int i,int j){  
  30.             copyarray[i][j] = array[i][j];  
  31.         if(i == endI &&  j == endJ){  
  32.             count++;  
  33.             System.out.print("\n 顯示路徑:\n");  
  34.             for(int k = 0; k < 4; k++){  
  35.                 for(int d = 0; d < 5; d++){  
  36.                     if(copyarray[k][d] == 0)  
  37.                         System.out.print("從");  
  38.                     else if(copyarray[k][d] == 1)  
  39.                         System.out.print("我");  
  40.                     else if(copyarray[k][d] == 2)  
  41.                         System.out.print("做");  
  42.                     else if(copyarray[k][d] == 3)  
  43.                         System.out.print("起");  
  44.                     else if(copyarray[k][d] == 4)  
  45.                         System.out.print("振");  
  46.                     else if(copyarray[k][d] == 5)  
  47.                         System.out.print("興");  
  48.                     else if(copyarray[k][d] == 6)  
  49.                         System.out.print("中");  
  50.                     else if(copyarray[k][d] == 7)  
  51.                         System.out.print("華");    
  52.                     else if(copyarray[k][d] == 10)  
  53.                         System.out.print("一");    
  54.                     else   
  55.                         System.out.print("一");    
  56.   
  57.                 }  
  58.                 System.out.println();  
  59.             }  
  60.         }  
  61.         if(j != 4 && array[i][j+1] == array[i][j]+1) visit(i,j+1);  
  62.         if(i != 3 && array[i+1][j] == array[i][j]+1) visit(i+1,j);  
  63.           
  64.         copyarray[i][j] = 10;  
  65.     }  
  66.     /* 
  67.      * 主調方法 
  68.      */  
  69.     public void test(){  
  70.   
  71.         initcopy();  
  72.         visit(startI,startJ);  
  73.         System.out.println("共有 "+count+" 種");  
  74.     }  
  75.       
  76. }  

結果截圖:

4.標題: 顛倒的價牌


    小李的店裏專賣其它店中下架的樣品電視機,可稱爲:樣品電視專賣店。

    其標價都是4位數字(即千元不等)。

    小李爲了標價清晰、方便,使用了預製的類似數碼管的標價籤,只要用顏色筆塗數字就可以了(參見p1.jpg)。

    這種價牌有個特點,對一些數字,倒過來看也是合理的數字。如:1 2 5 6 8 9 0 都可以。這樣一來,如果牌子掛倒了,有可能完全變成了另一個價格,比如:1958 倒着掛就是:8561,差了幾千元啊!!

    當然,多數情況不能倒讀,比如,1110 就不能倒過來,因爲0不能作爲開始數字。

    有一天,悲劇終於發生了。某個店員不小心把店裏的某兩個價格牌給掛倒了。並且這兩個價格牌的電視機都賣出去了!

    慶幸的是價格出入不大,其中一個價牌賠了2百多,另一個價牌卻賺了8百多,綜合起來,反而多賺了558元。

    請根據這些信息計算:賠錢的那個價牌正確的價格應該是多少?


答案是一個4位的整數,請通過瀏覽器直接提交該數字。
注意:不要提交解答過程,或其它輔助說明類的內容。

[java] view plaincopy
  1. package Forth;  
  2.   
  3. public class ReverseNumber {  
  4.   
  5.     /* 
  6.      * 實現數字的翻轉 
  7.      */  
  8.     public int reverse(int n){  
  9.       
  10.         int store[] = new int[4];  
  11.         int result = 0;  
  12.         int r;  
  13.         int i = 0;  
  14.           
  15.         while( n != 0){ //分離每個數,存入store中  
  16.             r = n % 10;  
  17.             n = n / 10;  
  18.             store[i] = r;  
  19.             i++;  
  20.         }  
  21.         for(int k = 0; k < i; k++){  
  22.             if(store[k] == 3 || store[k] == 4 || store[k] == 7)//數字中有3、4、7的不能翻轉  
  23.                 return -1;  
  24.             else if(store[k] == 6)//數字中有6,則轉爲9  
  25.                 store[k] = 9;  
  26.             else if(store[k] == 9)//數字中有9,則轉爲6  
  27.                 store[k] = 6;  
  28.         }  
  29.         for(int j = 0; j < i; j++){//計算翻轉後的數  
  30.             if(store[0] == 0//最後一個數字爲0,不能翻轉  
  31.                 return -1;  
  32.               
  33.             if(j == 0)  
  34.                 result = store[j];  
  35.             else  
  36.                 result = result*10 +store[j];  
  37.         }  
  38.   
  39.     //  System.out.println(result);  
  40.         return result;  
  41.     }  
  42.     public void selectNum(){  
  43.         int sub = 0;  
  44.         int sub2 = 0;  
  45.       
  46.         for(int i = 1000; i <= 9999; i++){//遍歷所有4位數  
  47.               
  48.             if(reverse(i) == -1)  
  49.                 continue;  
  50.             sub = i-reverse(i);  
  51.               
  52.             if(sub < 200 || sub >= 300)  
  53.                 continue;  
  54.             for(int j = 1000; j <= 9999; j++){  
  55.                   
  56.                 if(reverse(j) == -1)  
  57.                     continue;  
  58.                 sub2 = reverse(j)-j;  
  59.                   
  60.                 if(sub2 < 800 || sub2 >= 900)  
  61.                     continue;     
  62.                 if((sub2-sub) == 558){  
  63.                     System.out.println("賠錢的正確價格爲:"+i);  
  64.                     //System.out.println("賺錢的正確價格爲:"+j);  
  65.                 }  
  66.             }  
  67.         }  
  68.           
  69.     }  
  70. }  

答案:9088

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章