Java基础练习题——数组和循环练习题

Java基础练习题——数组和循环练习题

1.       编写一个九九乘法法
importjava.util.*;
publicclass A
{
         public static void main(String[] args) 
         {       
                  for(int i=1;i<10;i++){//双重循环打印
                          for(intj=1;j<=i;j++){
                                   System.out.print(+j+"*"+i+"="+(i*j)+"\t");
                          }
                          System.out.println();
                  }
                  
         }
}
 
2.       百钱买百鸡,有100元钱,要去买100只鸡,公鸡5元一只,母鸡3元一只,小鸡1元3只,问公,母,小鸡各买多少只
提示: a+b+c=100  只
       5a+3b+1/3c=100钱
importjava.util.*;
publicclass A
{
         public static void main(String[] args) 
         {       
                  int i=0;
                  int a,b,c;
                  for(a=0;a<20;a++){//a是公鸡
                          for(b=0;b<34;b++){          //b是母鸡
                                   c=100-a-b;
                                   if(5*a+b/3.+3*c==100){//判断是否满足
                                            i++;
                                            System.out.printf("第%d种方法\n",i);
                                            System.out.println("公鸡买"+a+"只\n"+"母鸡买"+b+"只\n"+"小鸡买"+c+"只");
                                            System.out.println();
                                   }
                          }
                  }
                  
                                   
         }
}
 
 
 
3.       有1020个西瓜,第一天卖掉总数的一半后又多卖出两个,以后每天卖剩下的一半多两个,问几天以后能卖完
import java.util.*;
public class A
{
         public static void main(String[] args) 
         {       
                  int num=1020,day=0;
                  while(num>0){\\判断是否卖完
                          num=num/2-2;
                          day++;
                  }
                  
                  System.out.println(+day+"天以后能卖完");
         }
}
 
4.       猴子吃桃问题: 猴子第一天摘下若干个桃子,当即吃了一半,还不过瘾,又多吃了一个,第二天早上又将剩下的桃子吃了一半,又多吃一个,以后每天都吃了前一天剩下的一半零一个。到第10天早上想再吃时,见只剩下一个桃子。求第一天共摘了多少个?
import java.util.*;
public class A
{
         public static void main(String[] args) 
         {       
                  int num=1;
                  for(int i=1;i<10;i++)//循环乘九次
{
         num=(num+1)*2;
}
                  
                  System.out.println("第一天共摘了"+num+"个桃子");
         }
}
 
5.       鸡兔同笼。鸡兔一共有50只,脚一共有160只,问鸡和兔各多少只?要求鸡兔至少一样一只
importjava.util.*;
publicclass A
{
         public static void main(String[] args) 
         {       
                  int a=0,b=0;
                                            for(a=1;a<50;a++){//穷举出所有可能
                                            {
for(b=1;b<50;b++){
                                                     {       
if((2*a+4*b==160)&&(a+b==50))
                                                              System.out.println("鸡有"+a+"只\n"+"兔有"+b+"只\n");
                                                              break;
                                                     }
                                                             
                                   }
                  
         }
}
 
 
 
6.       有个人想知道,一年之内一对兔子能繁殖多少对?于是就筑了一道围墙把一对兔子关在里面。已知一对兔子每个月可以生一对小兔子,而一对兔子从出生后第3个月起每月生一对小兔子。假如一年内没有发生死亡现象,那么,一对兔子一年内(12个月)能繁殖成多少对?  分析:兔子的规律为数列,1,1,2,3,5,8,13,21
importjava.util.*;
publicclass A
{
         public static void main(String[] args) 
         {       
                  int sum=0;
                  int i,a=1,b=1;
                  for(i=3;i<=12;i++){           //后一个数等于前两个数之和
                          sum=a+b;
                          a=b;
                          b=sum;
                  }
                  System.out.println("一年内能繁殖"+sum+"对");
         }
}
 
 
7.       求300-400之间的素数
importjava.util.*;
publicclass A
{
         public static void main(String[] args) 
         {       
                  int f=0;
                  for(int i=300;i<401;i++){
                          for(intj=2;j                                   if(i%j==0){                 //判断是否是素数                                   
f=0;
                                            break;
                                   }else 
f=1;
                          }
                          if(f==1){
                          System.out.println(i);
                          }
                  }       
         }
}
 
 
 
8.       完成数组的排序(冒泡,插入,选择,快排,堆排均可)
//  选择排序
import java.util.*;
public class A
{
         publicstatic void main(String[] args) 
         {       
                  int[]a={10,20,11,35,6,89};
                  intx;
                  for(inti=0;i<5;i++){//冒泡法
                          for(intj=i;j<5-i;j++){
                                   {if(a[j]>a[j+1]){
                                            x=a[j];
                                            a[j]=a[j+1];
                                            a[j+1]=x;}
                                   }
                          }
                          System.out.print(a[i]+"");
                  }
                  System.out.println();
         }
}
 
 

你可能感兴趣的:(Java基础练习题——数组和循环练习题)