java練習題:輸出100以內與7有關的數、百馬百擔、打分(去掉最高、最低分)、二分法尋找資料

來源:互聯網
上載者:User

標籤:無法   for   value   print   序列   boolean   new   ++   bsp   

1.輸出100以內與7有關的數

註:

這些數分為三類:(1)7的倍數,(2)個位元字是7的數,(3)十位元字是7的數

        int i=1;        System.out.println("輸出100以內與7有關的數:");        for(i=1;i<=100;i++){            if(i%10==7 | i%7==0|i/10==7 ){//合格三類數                System.out.println(i);            }            else{                           }        

 

2.百馬百擔

註:

有一百匹馬,馱一百擔貨,大馬馱3擔,中馬馱2擔,兩隻小馬馱1擔,問有大,中,小馬各幾匹?

     int a=0,b=0,c=0;        double sum1=a+b+c,sum2=3*a+2*b+0.5*c;                for(a=0;a<=33;a++){//大馬不超過33匹            for(b=0;b<=50;b++){//中馬不超過50匹                for(c=0;c<=100;c++){//小馬不超過100匹                    

              if(sum1==100&&sum2==100){ System.out.println("大馬有"+a+"匹,中馬有"+b+"匹,小馬有"+c+"匹"); } else{ } } } }

 

3.比賽打分

註:

去掉2個最高分,去掉2個最低分,採用平均分

     int a[]=new int[10];        Scanner sc=new Scanner(System.in);//輸入10個分數        for(int i=0;i<=9;i++){            a[i]=sc.nextInt();        }       
     //排序 for(int i=1;i<a.length;i++){ for(int j=1;j<=a.length-i;j++){ if(a[j]>a[j-1]){ int temp=a[j]; a[j]=a[j-1]; a[j-1]=temp; } } } for(int i=0;i<a.length;i++){ System.out.println(a[i]); }
  
     //計算分數 int sum=0; double avg; for(int i=2;i<=a.length-3;i++){ sum+=a[i]; } avg=sum/(a.length-4)*1.0; //輸出 System.out.println("去掉兩個最高分:"+a[0]+","+a[1]+",去掉兩個最低分:"+a[a.length-1]+","+a[a.length-2]+",最終得分是:"+avg);

 

4.二分法

註:即一分為二的方法. 設[a,b]為R的閉區間. 逐次二分法就是造出如下的區間序列([an,bn]):a0=a,b0=b,且對任一自然數n,[an+1,bn+1]或者等於[an,cn],或者等於[cn,bn],其中cn表示[an,bn]的中點。

     int[] a=new int[]{11,12,13,14,15};//限定一個尋找的範圍        int low=0,high=4,mid=0;//設定數組最小、中間、最大數的下標                int value=15;//假設要查這個數        boolean zhaodao=false;//設定最後的判定條件        while(low<=high){            mid=(low+high)/2;            if(value>a[mid]){                low=mid+1;            }            else if(value<a[mid]){                high=mid-1;            }            else if(value==a[mid]){                zhaodao = true;                break;            }            else{                            }                }        if(zhaodao == false){        System.out.println("無法找到");        }        else{        System.out.println("找到了");        }   

 

java練習題:輸出100以內與7有關的數、百馬百擔、打分(去掉最高、最低分)、二分法尋找資料

聯繫我們

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