Java--藍橋杯真題

來源:互聯網
上載者:User

標籤:pac   ann   port   自身   a+b   .com   package   int   long   

題目:


三角形面積

如【圖1】所示。圖中的所有小方格面積都是1。
那麼,圖中的三角形面積應該是多少呢?

請填寫三角形的面積。不要填寫任何多餘內容或說明性文字。

 

chuchu解決方案:

 1 package package1; 2 import java.util.Scanner; 3  4 public class Q2 { 5     int x; 6     int y; 7     static class locate{ 8         private int x; 9         private int y;10         11         locate(int x,int y){12             this.x = x;13             this.y = y;14         }15         16         static double getDist(locate l1,locate l2) {17             return Math.sqrt((l1.x-l2.x)*(l1.x-l2.x)+(l1.y-l2.y)*(l1.y-l2.y));18         }19     }20     21     static double Hailun(double a,double b,double c) {22         double p = (a+b+c)/2.0;23         double res = Math.sqrt(p*(p-a)*(p-b)*(p-c));24         return res;25     }26     27     public static void main(String[] args) {28             29         //Scanner input = new Scanner(System.in);30         locate A = new locate(4,0);31         locate B = new locate(0,8);32         locate C = new locate(8,6);33 34         double ab = locate.getDist(A, B);35         double bc = locate.getDist(C, B);36         double ac = locate.getDist(A, C);37         38         double ans = Hailun(ab,ac,bc);39         System.out.println(ans);40         41         42     }43 }

題目二:


立方變自身

觀察下面的現象,某個數位立方,按位累加仍然等於自身。
1^3 = 1
8^3 = 512 5+1+2=8
17^3 = 4913 4+9+1+3=17
...

請你計算包括1,8,17在內,符合這個性質的正整數一共有多少個?

請填寫該數字,不要填寫任何多餘的內容或說明性的文字。

朕解決方案:

 

 1 package CH3; 2  3 import java.util.Scanner; 4  5 public class test2 { 6     public static void main(String args[]) { 7         for (int i = 0; i < 1000000; i++) { 8  9             long number = i * i * i;10             int sum = 0;11             while (number != 0) {12                 sum += number % 10;13                 number = number / 10;14             }15 16             if (sum == i) {17                 System.out.println(i);18 19             }20 21         }22 23     }24 25 }

 

Java--藍橋杯真題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.