文章标题

来源:互联网 发布:十大网络公关公司 编辑:程序博客网 时间:2024/06/06 03:28

//五个小朋友排成一队。问第一个多大了,第一个说比第二个大两岁,问第二个,第二个说比第三个大两岁,以此类推。
//问第五个小朋友几岁了,第五个小朋友说3岁了。问第一个小朋友几岁?
class children
{
public static void main(String[] args)
{
int old = 3;
int first = old;
for( int i=4; i>=1 ; i– ) //从后往前走,i=4是因为first已经初始化为old ,已经走过最后一个小朋友了
{
first += 2;
}
System.out.println(“第一个小朋友的年龄是:” + first);
}
}

//*************************************************

//1-1000的能被2,3,5整除的数分别放在三个数组,计算每隔数组的和
// 定义三个数组,如果有能满足三个条件的就放三次
class number
{
public static void main(String[] args)
{
int a[] = new int[500]; //2
int b[] = new int[500]; //5
int c[] = new int[500]; //7

    int as = 0;                        //分别为a,b,c的数组下标,每成功记录一个数,as/bs/cs  ++.    int bs = 0;    int cs = 0;    for( int sum=1; sum<=1000; sum++ )    {        if( sum%2 ==0 )                        //先满足2,再满足3,再满足5        {            a[as] = sum;                             as++;        }        else if( sum%3 == 0 )        {            b[bs] = sum;            bs++;        }        else if( sum%5 == 0 )        {            c[cs] = sum;            cs++;        }        /*        if( sum%2 == 0 )                          //如果一个数为30,那30归属c[]。因为先满足5,再满足3,最后满足2.        {            if( sum%3 == 0 )            {                if( sum%5 == 0 )                {                    c[cs] = sum;                    cs++ ;                    continue;                }                b[bs] = sum;                bs++;                continue;            }            a[as] = sum;            as++;        }        */    }    int maxa = 0;    int maxb = 0;    int maxc = 0;    for( int j=0; j<500; j++ )     //分别输出a,b,c的和    {        maxa += a[j];        System.out.println("所有能整除2的数相加:" + maxa);        System.out.println("----------------------------------");        maxb += b[j];        System.out.println("所有能整除3的数相加:" + maxb);        System.out.println("----------------------------------");        maxc += c[j];        System.out.println("所有能整除5的数相加:" + maxc);        System.out.println("----------------------------------");    }}

}
//******************************************************

//一张纸厚度是0.07毫米,假设这张纸可以限次对折,问对折几次可以超过珠峰?8848米

class paper
{
public static void main(String[] args)
{
double t= 0.07*0.001; //原厚度
double meter = 8848;
double thick = t; //总厚度

    for(int i=0; ; i++)          //对折次数    {        if( thick > meter )        {            System.out.println("对折的次数是:"+i);            break;        }        thick *= 2;               //对折    }}

}

//***************************************************

// 国际象棋64格。堆米。问第64个格上会放多少粒米?
//第一格堆1粒,第二格堆2,第三格堆4,第四格堆16…64格为 2的63次方

class rice
{
public static void main(String[] args)
{
int tag = 2;
double s = 1;
for( int i=1; i<64; i++) //
{
s *= tag;
}
System.out.println(s);
}
}

//******************************************************

//一个班级有15个人,这15个人都有自己的成绩,放在数组中然后求他们的平均成绩。

import java.util.*;
class score
{
public static void main(String[] args)
{
Scanner scan = new Scanner( System.in ); //键盘输入成绩
int classmates[] = new int[15];
int max = 0;
//键盘输入成绩
for(int i=0; i<15; i++)
{
classmates[i] = scan.nextInt();
max += classmates[i];
}

    /*     //int classmates[] = {60,60,60,60,60,60,60,60,60,60,60,60,60,70,50};  //这个是直接定义成绩的方法    for( int i=0; i<classmates.length; i++)     //长度为.length    {        max += classmates[i];    }    */    System.out.println("输入完毕--------------------------------------");    System.out.println("这个班的15个学生的平均成绩为:" + max/15);}

}

原创粉丝点击