JAVA编程基础实验

第3题


import java.util.Scanner;
public class Main {
public static void main(String args[]) {
Scanner reader=new Scanner(System.in);  
System.out.println("输入姓名(回车确认):");
String name=reader.next(); //从键盘为name赋值
System.out.println("输入年龄(回车确认):");
byte age=reader.nextByte(); //从键盘为age赋值
System.out.println("输入身高(回车确认):");
float height=reader.nextFloat(); //从键盘为height赋值
System.out.println("--基本信息--");
System.out.println("姓名:"+name); 
System.out.println("年龄:"+age);
System.out.println("身高:"+height);
   }
}

第4题

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		while(in.hasNext())
		{
			int n = in.nextInt();
			if(n%3==0&&n%8==0)
				System.out.println("yes");
			else
				System.out.println("no");
		}
		
		in.close();
	}
}

第5题

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		double r = in.nextDouble();
		if(r>0)
			System.out.println(3.14*r*r);
		else
			System.out.println("圆的半径要大于0!");
		in.close();
	}
}

第6题

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		double r = in.nextDouble();
		if(r>0)
			System.out.println(3.14*r*r*r*4/3.0);
		else
			System.out.println("球的半径要大于0!");
		in.close();
	}
}

第7题

import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner in =new Scanner (System.in);
		while(in.hasNext())
		{
		double r,s,h;
		r=in.nextDouble();
		h=in.nextDouble();
		if(r>0&&h>0)
		{
			s=r*h/2;
			System.out.println(s);
		}
			else
			System.out.println("直角三角形的高和边长要大于0!");		
	 }
	}
}

第8题

public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
int sum=0;
for (int i = 1000; i<10000; i++) {
int k=i/1000;
int m=(i/100)%10;
int n=(i/10)%10;
int l=i%10;
if (k==l&&m==n) {
sum+=i;
}
}
System.out.println(sum);
}
}

第9题

import java.util.Scanner;
// TODO Auto-generated method stub
public class Main {
public static void main(String[] args) {
	{
		Scanner in=new Scanner(System.in);
	 long  f1=1,f2=1;
	 int n=in.nextInt();
	System.out.println(sum(n,f1,f2));
	}
}
public static long sum(int num, long f1,long f2)
{
 long f,k=f1+f2;
 int i;
 for(i=3;i<=num;i++)
  {f=f1+f2;
   k=k*f;
   f1=f2;
   f2=f;
   }
 return k;  
 }
}

第10题

import java.util.Scanner;
// TODO Auto-generated method stub
public class Main {
public static void main(String[] args) {
	{
		Scanner sc=new Scanner(System.in);
	 long sum=1;
	 long n=sc.nextInt();
	 for(int i=1;i<=n;i++)
	 {
		 sum*=i;
	 }
	System.out.println("sum="+sum);
	}
}
}

第11题

import java.util.Scanner;
// TODO Auto-generated method stub
public class Main {
public static void main(String[] args) {
	{
		Scanner sc=new Scanner(System.in);
	 long sum=0;
	 long n=sc.nextInt();
	 for(int i=0;i<=n*n-n;i++)
	 {
		 sum+=(n+i)/2;
	 }
	System.out.println("sum="+sum);
	}
}
}

第12题

import java.util.Scanner;
// TODO Auto-generated method stub
public class Main {
public static void main(String[] args) {
	{
		Scanner sc=new Scanner(System.in);
	 long sum=0;
	 long n=sc.nextInt();
	 for(int i=0;i<=n*2-n;i++)
	 {
		 sum+=(n+i)*(n+i);
	 }
	System.out.println("sum="+sum);
	}
}
}

第13题

import java.util.Scanner;
// TODO Auto-generated method stub
public class Main {
public static void main(String[] args) {
	{
	 Scanner sc=new Scanner(System.in);
	 long sum=1;
	 long n=sc.nextInt();
	 for(int i=0;i<=n*2-n;i++)
	 {
		 sum*=(n+i);
	 }
	System.out.println("sum="+sum);
	}
}
}

第14题

import java.util.Scanner;
// TODO Auto-generated method stub
public class Main {
public static void main(String[] args) {
	{
	 Scanner sc=new Scanner(System.in);
	 long sum=0;
	 long k=sc.nextInt();
	 long n=sc.nextInt();
	 for(int i=0;i<=n-k;i++)
	 {
		 sum+=(k+i);
	 }
	System.out.println("sum="+sum);
	}
}
}

第15题

import java.util.Scanner;
// TODO Auto-generated method stub
public class Main {
public static void main(String[] args) {
	{
	 Scanner sc=new Scanner(System.in);
	 long sum=1;
	 long k=sc.nextInt();
	 long n=sc.nextInt();
	 for(int i=0;i<=n-k;i++)
	 {
		 sum*=(k+i);
	 }
	System.out.println("product="+sum);
	}
}
}

第16题 编程序求[200,M]中的所有素数之和

请编Java程序求[200,M]中的所有素数之和。
其中200<M<1000。

输入格式
输入1个整数M。
输出格式
[200,M]中的所有素数之和。

样例1输入:

300

样例1输出:

4048
import java.util.Scanner;
// TODO Auto-generated method stub
public class Main {
public static void main(String[] args) {
	{
	 Scanner sc=new Scanner(System.in);
	 int sum=0;
	 int m=sc.nextInt();
	 for(int i=200;i<=m;i++)
	 {
		 for(int j=2;j<i/2;j++)
		 {
			 if(i%j==0)
			 break;
			 if(j==i/2-1)
			 {
				 sum+=i;
			 }
		 } 
	 }
	System.out.println(sum);
	}
}
}

第17题 编程:统计大小写字母个数(字符串)

请编写程序:输入字符串,统计大小写字母个数,然后输出该数.

输入格式
输入一个字符串。
输出格式
输出一个整数。

样例输入

abc123AB

样例输出

5
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner in = new Scanner(System.in);
		while(in.hasNext())
		{
			String s = in.next();
			int sum = 0;
			char[] c = s.toCharArray();
			for(int i = 0;i< c.length;i++)
			{
				if((c[i]>='A'&&c[i]<='Z')||(c[i]>='a'&&c[i]<='z'))
				sum++;
			}
			System.out.println(sum);
		}
		in.close();
	}

}

第18题 编程序求[200,M]上的所有闰年之和(10分)

请编程序求[200,M]中的所有闰年之和。
其中200<M<1000。
由历法可知,4年设一闰,但每100年少一个闰年,即能被4整除但不能被100整除的年份为闰年;每400年又增加一个闰年,即能被400整除的年份也为闰年。

输入格式
输入1个整数M。
输出格式
[200,M]中的所有闰年之和。

样例1输入:

208

样例1输出:

412
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner in = new Scanner(System.in);
		while(in.hasNext())
		{
			int n = in.nextInt();
			int sum = 0;
			for(int i = 200;i<=n;i++)
			{
				if((i%100!=0&&i%4==0)||i%400==0)
				{
					sum+=i;
				}
			}
			System.out.println(sum);
		}
		in.close();
	}

}

第19题 编程:求n个实数中的最大数

请编写程序:输入n个实数(double型),求这n个实数中的最大数,然后输出该数.

输入格式
n
n个用空格分隔开的实数。
输出格式
输出一个实数。

样例输入

4
2.9 20 8.1 9.4

样例输出

20.0
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner in = new Scanner(System.in);
		while(in.hasNext())
		{
			int n = in.nextInt();
			double max = in.nextDouble();
			for(int i = 1;i<n;i++)
			{
				max = Math.max(max, in.nextDouble());
			}
			System.out.println(max);
		}
		in.close();
	}

}

第20题 编程:求n个整数之积

请编写程序:输入n个整数(int型),求这n个整数之积,然后输出该数.
其中:1<=n<=15,每个整数的绝对值<=12。
输入格式
n
n个用空格分隔开的整数。
输出格式
输出一个积(整数)。

样例输入

4
2 1 8 9

样例输出

144
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner in = new Scanner(System.in);
		while(in.hasNext())
		{
			int n = in.nextInt();
			long s = in.nextInt();
			for(int i = 1;i<n;i++)
			{
				s *=in.nextInt();
			}
			System.out.println(s);
		}
		in.close();
	}

}

第21题

import java.util.Scanner;
// TODO Auto-generated method stub
public class Main {
	public static int fun(int n)
	 { 
	   int bw,sw,gw;
	   bw=n/100;sw=(n-bw*100)/10;gw=n%10;
	  if(n==+(bw+sw+gw)+bw*bw*bw+sw*sw*sw+gw*gw*gw) return 1;
	  else return 0;
	 }
	  
public static void main(String[] args) {
{
	   Scanner sc=new Scanner(System.in);
	   int n,flag;
	   n=sc.nextInt();
	   flag=fun(n);
	   if(flag!=0)
	System.out.println("yes");
	   else
	System.out.println("no");
	 }
 }
}

第22题 编写程序:斐波那契图形(10分)

编写一个求斐波那契数列的递归函数,输入n值,使用该递归函数,输出如样例输出的斐波那契数列。
输入:
一个整型数n
输出:
题目可能有多组不同的测试数据,对于每组输入数据,
按题目的要求输出相应的斐波那契图形。
注意输出换行前不能有多余的空格。
样例输入:

6

样例输出:

0
0 1 1
0 1 1 2 3
0 1 1 2 3 5 8
0 1 1 2 3 5 8 13 21
0 1 1 2 3 5 8 13 21 34 55
import java.util.Scanner;
// TODO Auto-generated method stub
public class Main {
public static void main(String[] args) {
	{
	 Scanner sc=new Scanner(System.in);
	 while(sc.hasNext())
	 {
	 int n=sc.nextInt();
	 int a[]=new int[10000000];
	 a[0]=1;a[1]=1;
	 for(int i=2;i<1+(n-1)*2;i++)
	 {
		 a[i]=a[i-1]+a[i-2];
	 }
	 System.out.println(0);
	 for(int i=2;i<=n;i++)
	 {
		 System.out.print(0+" ");
		 for(int j=0;j<(i-1)*2-1;j++)
		 {
			 System.out.print(a[j]+" ");
		 }
		 System.out.println(a[(i-1)*2-1]);	 
	 }
	 }
	}
}
}

第23题 编程:求奇数的倒数之和值

Time Limit : 1.0 s Memory Limit : 32 MB

问题描述:
编写程序求1/1+1/3+…+1/(2n+1)的和值,其中整数n从键盘输入(0=<n<1000)。

输入格式
整数n
输出格式
1/1+1/3+…+1/(2n+1)的和值

样例输入

1
2

样例输出

Sum is 1.3333333333333333
Sum is 1.5333333333333332
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner in = new Scanner(System.in);
		while(in.hasNext())
		{
			int n = in.nextInt();
			double sum = 0;
			for(int i = 1;i<=2*n+1;i=i+2)
			{
				sum+=1.0/i;
			}
			System.out.println("Sum is "+sum);
		}
		in.close();
	}

}

第24题

import java.util.*;
public class Main {
  public static void main(String[] args) {
    Scanner in=new Scanner(System.in);
    int t=in.nextInt();
    int z=f(t);//类内调用方法f
    System.out.println(z);
    double w=f1(t);
    System.out.println(w);
  }
//y=f(x)=x*x 整数x(叫形参)
  private static int f(int x){
//========此处添加代码=========
int y=x*x;


//========添加代码结束=========
    return y; 
  }
//y=f1(x)=x*x*x 整数x
  public static double f1(double x){
    return x*x*x; //返回表达式的值
  }
}

第25题

import java.util.*;
public class Main {
  public static void main(String[] args) {
    Scanner in=new Scanner(System.in);
    double t=in.nextDouble();
    double z=f(t);//类内调用方法f
    System.out.println(z);
  }
//球体积y=f(r) 实数r(叫形参)
  private static double f(double r){
//========此处添加代码=========
double y=4/3.0*r*r*r*3.14;


//========添加代码结束=========
    return y; 
  }
}


第26题

public class Main
{
  public static void main(String args[])
   {
     int k,s=0,mc=0;
    /***********FOUND***********/
     for (k=500;k>0;k--) 
      {
       if (k%3 == 0)
        { 
   /***********FOUND***********/
          s+=k;
          mc++; 
        }
    /***********FOUND***********/
       if (mc==10)
         break; 
     }
   System.out.println("Sum is "+s);
  }
}

第27题 编写程序求球的半径和体积

题目描述:
输入球的中心点和球上某一点的坐标,计算球的半径和体积
输入:
球的中心点和球上某一点的坐标,以如下形式输入:x0 y0 z0 x1 y1 z1
输出:
输入可能有多组,对于每组输入,输出球的半径和体积,并且结果保留三位小数
样例输入:

0 0 0 1 1 1 

样例输出:

1.732 21.766
import java.util.Scanner;
// TODO Auto-generated method stub
public class Main {
public static void main(String[] args) {
	{
	 Scanner sc=new Scanner(System.in);
	 while(sc.hasNext())
	 {
	 double bj,tj;
	 double x0=sc.nextDouble(),y0=sc.nextDouble(),z0=sc.nextDouble();
	 double x1=sc.nextDouble(),y1=sc.nextDouble(),z1=sc.nextDouble();
	 bj=Math.sqrt((x0-x1)*(x0-x1)+(y0-y1)*(y0-y1)+(z0-z1)*(z0-z1));
	 tj=bj*bj*bj*3.14159*4/3.0;
	 System.out.printf("%.3f",bj);	  
	 System.out.print(" ");	  
	 System.out.printf("%.3f",tj);	  
	 }
	}
}
}

第28题 编写程序求最小和

题目描述:
给定一个正整数a,以及另外的5个正整数,问题是:这5个整数中,小于a的整数的和是多少?
输入:
输入一行,只包括6个小于100的正整数,其中第一个正整数就是a。
输出:
可能有多组测试数据,对于每组数据,
输出一行,给出一个正整数,是5个数中小于a的数的和。
样例输入:

10 1 2 3 4 11

样例输出:

10
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner in = new Scanner(System.in);
		while(in.hasNext())
		{
			int a = in.nextInt();
			int sum = 0;
			for(int i = 0;i<5;i++)
			{
				int b = in.nextInt();
				if(b<a)
					sum+=b;
			}
			System.out.println(sum);
		}
		in.close();
	}

}

第29题 编写程序输出对称平方数

题目描述:
打印所有不超过n(0<=n<256)的,其平方具有对称性质的数。
如11*11=121
输入:
无任何输入数据
输出:
输出具有题目要求的性质的数。如果输出数据不止一个,各个数据之间以回车隔开。

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int n = 0,m = 0;
		int[] cc = new int[200];
		for (int i = 0;i<4;i++)
		{
			cc[m]=i;
			m++;
		}
		for(int i = 4;i<=256;i++)
		{
			String s = String.valueOf(i*i);
			char [] c = s.toCharArray();
			int x,y;
			for(x = 0,y = c.length-1;x<c.length/2;x++,y--)
			{
				if(c[x]!=c[y])
					break;
			}
			if(x>=c.length/2)
			{
				cc[m]=i;
				m++;
			}
		}
		for(int i = 0;i<m-1;i++)
		{
			System.out.println(cc[i]);
		}
		System.out.print(cc[m-1]);
	}

}

第30题 编写程序:递推数列(10分)

题目描述:
给定a0,a1,以及an=pa(n-1) + qa(n-2)中的p,q。这里n >= 2。 求第k个数对10000的模。

输入:

输入包括5个整数:a0、a1、p、q、k。

输出:

第k个数a(k)对10000的模。

样例输入:

20 1 1 14 5

样例输出:

8359
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		while(in.hasNext())
		{
			int a[] = new int [100000];
			a[0] = in.nextInt();
			a[1] = in.nextInt();
			int p = in.nextInt();
			int q = in.nextInt();
			int k = in.nextInt();
			for(int i = 2;i<=k;i++)
			{
				a[i] = a[i-1]*p+a[i-2]*q;
				a[i]%=10000;
			}
			System.out.println(a[k]);
		 }	
		in.close();
	}
}

第31题 编写程序求各位数字之和(10分)

题目描述:
对于给定的正整数 n,计算其十进制形式下所有位置数字之和,并计算其平方的各位数字之和。

输入:

每行输入数据包括一个正整数n(0<n<40000),如果n=0 表示输入结束,并不用计算。

输出:

对于每个输入数据,计算其各位数字之和,以及其平方值的数字之和,输出在一行中,之间用一个空格分隔,但行末不要有空格。

样例输入:

4
12
97
39999
0

样例输出:

4 7
3 9
16 22
39 36
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int sum1,sum2,sum3;
		while(true)
		{
			sum1=0;
			sum2=0;
			sum3=0;
			int n = in.nextInt();
			if(n==0) break;
			String str=String.valueOf(n);
			char[] ss =str.toCharArray();
			for(int i = 0;i<ss.length;i++)
			{
				
				sum1+=(int)ss[i]-48;
			}
			sum2=n*n;
			String str1=String.valueOf(sum2);
			char[] ss1 =str1.toCharArray();
			for(int j = 0;j<ss1.length;j++)
			{
				
				sum3+=(int)ss1[j]-48;
			}
			System.out.println(sum1+" "+sum3);
		}
		in.close();
	}
}

第32题 与7无关的数平方和

时间限制:1 秒 内存限制:32 兆
题目描述:
一个正整数,如果它能被7整除,或者它的十进制表示法中某个位数上的数字为7,
则称其为与7相关的数.现求所有小于等于n(n<100)的与7无关的正整数的平方和。
输入:
案例可能有多组。对于每个测试案例输入为一行,正整数n,(n<100)
输出:
对于每个测试案例输出一行,输出小于等于n的与7无关的正整数的平方和。
样例输入:

21

样例输出:

2336
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		while(in.hasNext())
		{
			int n = in.nextInt();
			int sum = 0; 
			for(int i = 0;i<=n;i++)
			{
				if(i%7==0) continue;
				String str=String.valueOf(i);
				char[] ss =str.toCharArray();
				int j = 0;
				for(j = 0;j<ss.length;j++)
				{
					if(ss[j]=='7') break;
				}
				if(j<ss.length)
					continue;
				sum+=i*i;
			}
			System.out.println(sum);
		 }	
		in.close();
	}
}

第33题 编程题:最大公约数

时间限制:1 秒内存限制:32 兆
题目描述:
输入两个正整数,求其最大公约数。
输入:
测试数据有多组,每组输入两个正整数。
输出:
对于每组输入,请输出其最大公约数。
样例输入:

49 14

样例输出:

7
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		while(in.hasNext())
		{
			int n = in.nextInt();
			int m = in.nextInt();
			int t = Math.min(m, n);
			for(int i = t;i>0;i--)
			{
				if(n%i==0&&m%i==0)
				{
					System.out.println(i);
					break;
				}
			}
		}	
		in.close();
	}
}

第34题 编程题:求n个整数的平均值

给出n个整数,找出这n个数的平均值。

将有多组数据,组数预先未知。

输入格式
每组数据第一行为整数n,表示数的个数。
每组数据第二行有n个数,为给定的n个数,每个数的绝对值都小于10000。
输出格式
每组数据输出一行,表示n个整数的平均值(有小数)
样例输入

5
1 3 -2 4 5
3
1 2 4

样例输出

2.2
2.3333333333333335

数据规模与约定
1 <= n <= 10000。

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		while(in.hasNext())
		{
			int n = in.nextInt();
			double sum = 0;
			for(int i = 0; i<n;i++)
			{
				sum+=in.nextInt();
			}
				sum/=n;
			System.out.println(sum);
		}	
		in.close();
	}
}

第35题 编程题:输出字符串中的大写字母

输入n个字符串,输出字符串中的大写字母。

将有多组数据,组数预先未知。

输入格式
每组数据第一行为整数n,表示字符串的个数。
每组数据第二行开始有n个字符串,每个字符串的长度都小于10000。
输出格式
每组数据输出n行,每行为对应字符串中的大写字母
样例输入

2
123Rtii
erYY09
1
fg*&KK

样例输出

R
YY
KK

数据规模与约定
1 <= n <= 10000。

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		while(in.hasNext())
		{
			int n = in.nextInt();
			for(int i = 0; i<n;i++)
			{
				String str = in.next();
				char[] ss =str.toCharArray();
				for(int j = 0;j<ss.length;j++)
				{
					if(ss[j]<='Z'&&ss[j]>='A')
					{
						System.out.print(ss[j]);
					}
				}
				System.out.println();
			}
		}	
		in.close();
	}
}

第36题 填写代码. 字符串合并的例子

import java.util.Scanner;
public class Main {
static String[] strArray = new String[100];
static int n;// 要进行操作的字符串个数
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// 从键盘获取n
n = Integer.parseInt(sc.nextLine());
for (int i = 0; i < n; i++) {
// 从键盘获取字符串
strArray[i]= sc.nextLine();
}
System.out.println("你输入的字符串如下:");
for (int i = 0; i < n; i++) {
System.out.println(strArray[i]);
}
String st2 = stringAdd();// 字符串数组合并为一个字符串
System.out.println(st2);
}
// 此方法的作用是将字符串数组转换成一个字符串
public static String stringAdd() {
String string = "";
for (int i = 0; i < n; i++) {
string = string + strArray[i];
}
return string;
// 将这个字符串返回
}
}

第37题 编程:n个整数的逆序输出(10分)

请编写程序:对输入的n个整数进行逆序并输出.

输入格式

n

n个用空格分隔开的整数。

输出格式

逆序(n个用一个空格分隔开的整数)。

样例输入

3
1 3 2

样例输出

2 3 1

注意: 2 3 1 中间只有一个空格, 尾部1的后面一个空格

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int[] a = new int [10000];
		int n = in.nextInt();
		for(int i = 0;i<n;i++)
		{
			a[i]=in.nextInt();
		}
		for(int i = n-1;i>=0;i--)
		{
			System.out.print(a[i]+" ");
		}
		in.close();
	}
}

第38题 编程:计算M以内最大的10个能被3整除的自然数之和

问题描述:
题目:计算并输出M以内最大的10个能被3整除的自然数之和,其中100<=M<=5000。
测试数据可能有多组,组数未知。

输入格式
自然数n
输出格式
M以内最大的10个能被3整除的自然数之和

样例输入

270
120

样例输出

Sum is 2565
Sum is 1065
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		while(in.hasNext())
		{
			int sum = 0;
			int n = in.nextInt();
			int j = 0;
			for(int i = n;i>0;i--)
			{
				if(i%3==0)
				{
					sum+=i;
					j++;
				}
				if(j==10)
					break;
			}
			System.out.println("Sum is "+sum);
		}
		in.close();
	}
}

第39题 编程:数组奇偶位对换,并输出奇数下标的元素值

请编写程序:对输入的整数数组对换下标相邻的奇数位和偶数位,并输出下标为奇数(1,3,5,…)的元素值.

输入格式
n
n个用空格分隔开的整数。
输出格式
输出若干个整数(每行一个数)。

样例输入

5
5 1 2 3 4

样例输出

2
4
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int[] a = new int [10000];
		int n = in.nextInt();
		for(int i = 0;i < n; i++)
		{
			a[i] = in.nextInt();
		}
		for(int i = 1;i < n;i++)
		{
			if(i%2==0)
				System.out.println(a[i]);
		}
		if(n%2==0)
			System.out.println(a[n-1]);
		in.close();
	}
}

第40题 程序运行结果,数组变化和输出

public class Main {
	private static void printArray(int[] arr){
		for(int i = 0;i<arr.length;i++)
			System.out.print(arr[i]+",");
		System.out.println("\n");
	}
	private static void changeValue(int value){
		value*=2;
	}
	private static void changeValue(int[] arr){
		for(int i = 0;i<arr.length;i++)
			arr[i]*=2;
	}
	public static void main(String[] args) {
		int[] arr={1,2,3,4,5};
		changeValue(arr[0]);
		printArray(arr);
		changeValue(arr);
		printArray(arr);
	}
}

第41题 编程求数组最大值及其下标

对于给定整数数组a[],寻找其中最大值,并返回下标。
输入格式
  整数数组a[],数组元素个数小于1等于100。输出数据分作两行:第一行只有一个数,表示数组元素个数;第二行为数组的各个元素。
输出格式
  输出最大值,及其下标
样例输入

3
33 2 1

样例输出

33 0
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		int[] a = new int [100];
		Scanner in = new Scanner(System.in);
		int n = 0,m = 0;
		int k = in.nextInt(); 
		for(int i = 0;i<k;i++)
		{
			a[i] = in.nextInt();
		}
		n = a[0];
		for(int j = 1;j < k;j++)
		{
			n = Math.max(n,a[j]);
			if(n==a[j])
				m=j;
		}
		System.out.println(n+" "+m);
		in.close();
	}
}

第42题 编程找x在某数组中的下标

时间限制:1 秒内存限制:32 兆
输入一个数n,然后输入n个数值各不相同,再输入一个值x,输出这个值在这个数组中的下标(从0开始,若不在数组中则输出-1)。
输入:
测试数据有多组,输入n(1<=n<=200),接着输入n个数,然后输入x。
输出:
对于每组输入,请输出结果。
样例输入:

2
1 3
0

样例输出:

-1
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		int[] a = new int [200];
		Scanner in = new Scanner(System.in);
		while(in.hasNext())
		{
			int i = 0;
			int n = in.nextInt();
			for(i = 0;i < n; i++)
			{
				a[i] = in.nextInt();
			}
			int x = in.nextInt();
			for(i = 0;i < n;i++)
			{
				if(a[i]==x)
				{
					System.out.println(i);
					break;
				}
			}
			if(i==n)
			{
				System.out.println("-1");
			}
		}
		in.close();
	}
}

第43题 编程实现最大最小数的互换

时间限制:1 秒内存限制:32 兆
题目描述:
输入一个数n,然后输入n个数值各不相同,调换数组中最大和最小的两个数,然后输出。
输入:
测试数据有多组,输入n(1<=n<=20),接着输入n个数。
输出:
对于每组输入,输出交换后的结果。
样例输入:

2
1 3

样例输出:

3 1
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		int[] a = new int [20];
		Scanner in = new Scanner(System.in);
		while(in.hasNext())
		{
			int i = 0,max = 0,min = 0;
			int j = 0,k = 0;
			int n = in.nextInt();
			a[0] = in.nextInt();
			max = a[0];
			min = a[0];
			for(i = 1;i < n; i++)
			{
				a[i] = in.nextInt();
				max = Math.max(max, a[i]);
				if(max==a[i]) j = i;
				min = Math.min(min, a[i]);
				if(min==a[i]) k = i;
			}
			a[j] = a[k];
			a[k] =max;
			for(i = 0;i < n-1;i++)
				System.out.print(a[i]+" ");
			System.out.println(a[n-1]);
		}
		in.close();
	}
}

第44题 编写. 计算某日是星期几(

下面程序计算某日是星期几。
此题的上机步骤是:

  1. 建立一个Java项目,名称可以按题号取名;
  2. 建立一个类, 类的名称为Main。这一点非常重要;
  3. 按要求编写代码;
  4. 提交代码,注意题号要一致。

输入:
以空格分隔的日期,形式为YYYY MM DD
输出:
星期=n, 这里n是数字
The Main class is end.

样例输入:

1990 2 12

样例输出:

星期=1
The Main class is end.
import java.util.Scanner;
import java.util.Calendar;

public class Main {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int YY = 0, MM = 0, DD = 0;
		YY = in.nextInt();
		MM = in.nextInt();
		DD = in.nextInt();
		Calendar calendar=Calendar.getInstance();
	    calendar.set(YY,MM-1, DD);
	    System.out.println("星期="+(calendar.get(Calendar.DAY_OF_WEEK)-1));
	    System.out.println("The Main class is end.");
		in.close();
	}
}

第45题 编程求若干个整数之最大值,最小值,和

给出n个整数,找出这n个数的最大值,最小值,和。

输入格式
第一行为整数n,表示数的个数。

第二行有n个数,为给定的n个数,每个数的绝对值都小于10000。

输出格式
输出三行,每行一个整数。第一行表示这些数中的最大值,第二行表示这些数中的最小值,第三行表示这些数的和。
样例输入

5
1 3 -2 4 5

样例输出

5
-2
11

数据规模与约定
1 <= n <= 10000。

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		int[] a = new int [10000];
		Scanner in = new Scanner(System.in);
		int max = 0,min = 0,sum = 0;
		int n = in.nextInt();
		a[0] = in.nextInt();
		max = a[0];
		min = a[0];
		sum = a[0];
		for(int i = 1; i < n; i++)
		{
			a[i] = in.nextInt();
			max = Math.max(max,a[i]);
			min = Math.min(min, a[i]);
			sum +=a[i]; 
		}
		System.out.println(max+"\n"+min+"\n"+sum);
		in.close();
	}
}

第46题 编程:求Excel表中矩形数量

小明经常使用Excel表,有一天大脑冒出一个想法:一个Excel表区域中有多少个矩形?例如下图中的深灰色区域:

这个图在Excel表的区域是左上角为B3,右下角为D4。这个图中包含有18个矩形。
如果小明告诉你Excel表区域的左上角和右下角,请你帮他计算这个区域中矩形的数量。

输入格式
题目包含多组输入,输入包括两个坐标(左上角,右下角)。
输出格式
矩形的个数

样例输入

B3 D4

样例输出

18

数据规模与约定
A <= 第1个坐标 <= Z
1 <= 第2个坐标 <= 30

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		while(in.hasNext())
		{
			String s1 = in.next();
			char[] ss1 = s1.toCharArray();
			String s2 = in.next();
			char[] ss2 = s2.toCharArray();
			int j = 0,k = 0;
			if(ss1.length==2)
				j = ss1[1];
			else
				j = ss1[1]*10+ss1[2];
			if(ss2.length==2)
				k = ss2[1];
			else
				k = ss2[1]*10+ss2[2];
			int m = k-j+1;
			int n = ss2[0]-ss1[0]+1;
			System.out.println(m*(m+1)*n*(n+1)/4);
		}
		
		in.close();
	}
}

第47题

import java.util.Scanner;
public class Main {
    public static void main(String args[]) {
        // TODO Auto-generated method stub
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        Point pt = new Point(1, n);// 创建对象
        System.out.println("pt.x=" + pt.x);// 取
        pt.x = 5;// 修改
        System.out.println("pt.x=" + pt.x);
        pt.move(3, 3);// 移动
        System.out.println("pt.x=" + pt.x);
        System.out.println("pt.y=" + pt.y);
        Point pt2 = new Point();// 声明对象并new
        System.out.println("pt2.x=" + pt2.x);
        pt2 = new Point(9, 2);
        System.out.println("pt2.x=" + pt2.x);
    }
}
class Point {
    int x, y;// 成员变量,属性
    Point() {// 无参构造方法
        x = 0;
        y = 0;
    }
    public Point(int ix, int iy) {// 有参构造方法
        x = ix;
        y = iy;
    }
    void move(int ix, int iy) {// 方法
        x += ix;// x=x+ix
        y += iy;
    }
}

第48题

public class Main {
    public static void main (String args[ ]) {
       System.out.println("教学活动从教室开始");//命令行窗口输出"教学活动从教室开始"
       Teacher zhang = new Teacher();
       Student jiang = new Student();//创建对象
       zhang.introduceSelf();
       jiang.introduceSelf(); //调用它的方法
    }
}
class Teacher {
  void introduceSelf() {
	  System.out.println("我是李老师");    //命令行窗口输出"我是李老师"
   }
}
 
class Student {
  void introduceSelf() {
	  System.out.println("我是学生,名字是:奖励");//命令行窗口输出"我是学生,名字是:奖励"
   }	
}

第49题

public class Main {
 
public static void main(String[] args) {
Point Point1 = new Point(3, 4);
System.out.println("Point1:" + "(" +
Point1.x + "," + Point1.y + ")");
 
Point Point2 = Point1.getPoint();
System.out.println("Point2:" + "(" +
Point2.x + "," + Point2.y + ")");
 
Point  Point3  = new Point(5, 6);
 Point1.setPoint(Point3);
System.out.println("Point1:" + "(" +
 Point1.x + "," +  Point1.y + ")");
}
}
 
class Point {
int x, y;
 
public Point(int x, int y) {
this.x = x;
this.y = y;
}
 
public Point getPoint() {
Point tempPoint = new Point(x, y);
return tempPoint;
}
 
public void setPoint(Point point) {
this.x = point.x;
this.y = point.y;
}
 
}

第50题

public class Main {
 
public static void main(String[] args) {
// TODO Auto-generated method stub
Point origin = new Point(10, 10);
origin.getPoint();
origin.setPoint(20, 20);
origin.getPoint();
}
 
}
 
class Point {
private int x;
private int y;
 
public Point(int x, int y) {
this.x = x;
this.y = y;
}
 
public void setPoint(int x1, int y1) {
x = x1;
y = y1;
}
 
public void getPoint() {
System.out.println("Point x: " + x + ",y: " + y);
}
 
}

第51题

import java.util.*;
public class Main {
 public static void main(String args[]) {
  Scanner reader = new Scanner(System.in);
  String name=reader.nextLine();
  String ID=reader.nextLine();
  int salary= reader.nextInt();
  int year = reader.nextInt();
  int month=reader.nextInt();
  int day=reader.nextInt();  
Employee e = new Employee(name,ID,salary,year,month,day);
e.raiseSalary(5);
System.out.println("姓名:"+e. getName()+ " 身份证:" +e.getID()+" 工资:"+ e.getSalary());
  System.out.println("The Main class is end.");
 }
}
 
class Employee {
  private String name;//私有域,姓名
  private String ID;//私有域,身份证
  private double salary;//私有域,工资
  private Date hireDay; //私有域,录用日期
  //构造方法
  public Employee(String n, String i, double s, int year, int month, int day)
  {
     name = n;//参数,(局部)变量
     ID = i;
     salary = s;//参数, (局部)变量
     GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
     // GregorianCalendar uses 0 for January(1月份是0)
     hireDay = calendar.getTime();
  }
 
public String getName() {
return name;
}
public String getID() {
return ID;
}
public double getSalary() {
return salary;
}
public Date getHireDay() {
return hireDay;
}
//"涨工资"计算
public void raiseSalary(double byPercent)
{
  double raise = salary * byPercent / 100;
  salary += raise;
}
 
}

第52题

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
System.out.println(Plus.add(5));//显示5+2的结果
Scanner reader=new Scanner(System.in);  
double y=reader.nextDouble();
System.out.println(Plus.add(y));//显示1+y的结果
Plus aPlus=new Plus(3,4);
System.out.println(aPlus.add());//显示3+4的结果
}
}
class Plus{
static int a=1,b=2;//域
int x,y;
public Plus() {
x=0;y=0;
}
public Plus(int x, int y) {
this.x = x;
this.y = y;
}
public static int add(int x){
return(x+b);
}
public int  add(){
return(x+y);
}
public static double add(double y){
return(a+y);
}
public static int add(int x,int y){
return (x+y);
}
}

第53题


import java.util.Scanner;
public class Main
{
   public static void main(String[] args)
   {
      Employee[] staff = new Employee[3];

      staff[0] = new Employee("Tom", 40000);
      staff[1] = new Employee("Dick", 60000);
      staff[2] = new Employee("Harry", 65000);
      Scanner reader=new Scanner(System.in);
      for (Employee e: staff)
      {
    	int n=reader.nextInt();
    	e.setId(n);
    	System.out.println("name=" + e.getName() + ",id=" + e.getId() + ",salary="
               + e.getSalary());
      }
      int n = Employee.getNextId(); // calls static method
      System.out.println("Next available id=" + n);
      System.out.println("The Main class is end.");
 
   }
}
class Employee
{
   private static int nextId = 1;
   private String name;
   private double salary;
   private int id;
   public Employee(String n, double s)
   {
      name = n;
      salary = s;
      id = 0;
   }
   public String getName()
   {
      return name;
   }
   public double getSalary()
   {
      return salary;
   }
   public int getId()
   {
      return id;
   }
   public void setId(int n)
   {
      id = nextId; // set id to next available id
      nextId+=n;
   }
   public static int getNextId() //静态方法
   {
      return nextId; // returns static field
   }
}

第55题编写程序:字符串对比(10分)

给定两个仅由大写字母或小写字母组成的字符串(长度介于1到10之间),它们之间的关系是以下4中情况之一:
  1:两个字符串长度不等。比如 Beijing 和 Hebei
  2:两个字符串不仅长度相等,而且相应位置上的字符完全一致(区分大小写),比如 Beijing 和 Beijing
  3:两个字符串长度相等,相应位置上的字符仅在不区分大小写的前提下才能达到完全一致(也就是说,它并不满足情况2)。比如 beijing 和 BEIjing
  4:两个字符串长度相等,但是即使是不区分大小写也不能使这两个字符串一致。比如 Beijing 和 Nanjing
  编程判断输入的两个字符串之间的关系属于这四类中的哪一类,给出所属的类的编号。
输入格式
  包括两行,每行都是一个字符串
输出格式
  仅有一个数字,表明这两个字符串的关系编号
样例输入

BEIjing
beiJing

样例输出

3
import java.util.Scanner;
public class Main
{
   public static void main(String[] args)
   {
	   Scanner in = new Scanner(System.in);
	   while(in.hasNext())
	   {
		   String str1 = in.next();
		   String str2 = in.next();
		   char[] s1 = str1.toCharArray();
		   char[] s2 = str2.toCharArray();
		   boolean p = true;
		   int i = 0;
		   if(s1.length!=s2.length)
			   System.out.println("1");
		   else
		   {
			   for(i = 0; i < s1.length; i++)
			   {
				   if((s1[i]!=s2[i])&&(s1[i]+32!=(int)s2[i])&&((int)s1[i]!=s2[i]+32))
				   {
					   System.out.println("4");
					   break;
				   }
				   else if((s1[i]+32==s2[i])||(s1[i]==s2[i]+32))
				   {
					   p = false;
				   }
			   }
			   if(i==s1.length&&p)
			   {
				   System.out.println("2");
			   }
			   else if(i==s1.length&&!p)
			   {
				   System.out.println("3");
			   }
		   }
	   }
	   in.close();
   }
}

第57题

import java.util.Scanner;
class Point {
public int x, y;//域(成员属性)
//无参构造器(方法)
public Point() {
x = 0;
y = 0;
}
// 有参构造器(方法)
public Point (int x ,int y) {
this.x = x;
this.y = y;
}
//成员方法
protected void move(int ix,int iy) {
x += ix;
y += iy;
}
}
class Son extends Point{
private int x,y;//隐藏父亲的x,y
//无参
public Son(){
this.x=0;
this.y=0;
}
 
//有参
public Son(int x,int y){
this.x=x;
this.y=y;
}
public int getX() {
return x;
}
 
public int getY() {
return y;
}
}
class Son1 extends Point {
public Son1(){
//this.x=0;
//this.y=0;
super();//调用父类的构造器(方法)Point()
}
//有参
public Son1(int x,int y){
//this.x=x;
//this.y=y;
super(x,y);//调用父类的构造器(方法)Point(int x,int y)
}
public void move(int ix,int iy){
super.move(ix, iy);//调用父类的move(...)
}
}
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Point pt=new Point();//声明对象变量,创建(实例化)
pt.x=1;//pt中x赋值1
System.out.println("pt.x="+pt.x+"\npt.y="+pt.y);
Son son=new Son(1,2);
Son1 son1=new Son1(); 
son1.x=1;
son1.move(2, 3);//继承
System.out.println("son1.x="+ son1.x+"\nson1.y="+ son1.y);
son.move(2, 3);//继承
System.out.println("son.x="+ son.getX());//显示son中x的值
}
}

第58题

class Bank {
	   int savedMoney;
	   int year;
	   double interest;
	   double interestRate = 0.29;
	   public double computerInterest() {
	      interest=year*interestRate*savedMoney;
	      return  interest;//返回利息
	   }
	   public void setInterestRate(double rate) {
	      interestRate = rate;
	   }
	}
	class ConstructionBank extends Bank {
	   double year;
	   public double computerInterest() {
	      super.year=(int)year;
	      double r = year-(int)year;
	      int day=(int)(r*1000);
	      double yearInterest = super.computerInterest(); //super调用隐藏的computerInterest()方法
	      double dayInterest = day*0.0001*savedMoney;
	      interest= yearInterest+dayInterest;
	      System.out.printf("%d元存在建设银行%d年零%d天的利息:%f元\n",
	                         savedMoney,super.year,day,interest);
	      return interest;
	   }
	}
	class BankOfDalian extends Bank {  //继承
	   double year;
	   public double computerInterest() {
	      super.year=(int)year;
	      double r = year-(int)year;
	      int day=(int)(r*1000);
	      double yearInterest = super.computerInterest();// super调用隐藏的computerInterest()方法
	      double dayInterest = day*0.00012*savedMoney;
	      interest= yearInterest+dayInterest;
	      System.out.printf("%d元存在大连银行%d年零%d天的利息:%f元\n",
	                         savedMoney,super.year,day,interest);
	      return interest;
	   }
	}
	public class Main {
	   public static void main(String args[]) {
	      int amount=8000;
	      ConstructionBank bank1 = new ConstructionBank();
	      bank1.savedMoney = amount;
	      bank1.year = 8.236;
	      bank1.setInterestRate(0.035);
	      double interest1 = bank1.computerInterest();//计算利息
	      BankOfDalian bank2 = new BankOfDalian();
	      bank2.savedMoney = amount;
	      bank2.year = 8.236;
	      bank2.setInterestRate(0.035);
	      double interest2=bank2.computerInterest();//计算利息
	      System.out.printf("两个银行利息相差%f元\n",interest2-interest1);
	   }
	}

第59题

import java.util.Scanner;
public class Main
{
   public static void main(String[] args)
   {
      Student[] aStudent = new Student[3];
      Scanner reader=new Scanner(System.in);
      aStudent[0] = new Student("Tom",reader.nextLine());
      aStudent[1] = new Student("Dick",reader.nextLine());
      aStudent[2] = new Student("Harry",reader.nextLine());
      for(Student e: aStudent)
      {
    System.out.println("name=" + e.getName() + ",id=" + e.getDescription());
      }
      System.out.println("The Main class is end.");
   }
}
abstract class Person {
private String name;
public String getName() {
return name;
}
public Person(String n){
name=n;
}
public abstract String getDescription(); 
}
class Student extends Person
{
private String major;
public Student(String n, String m)
{
super(n);
major = m;
}
@Override
public String getDescription(){
return "a student majoring in " + major;
}
}

第60题

public class Main{
    public void tryChange(OnlyTest form){
           int t = form.getX();     //获取对象form的成员变量x的值
           form.setX(t*2);           //改变对象form的成员变量x的值
        }
        public void showDiffer(){
          OnlyTest  actual = new OnlyTest();
          actual.setX(100);
          System.out.println("调用tryChange方法之前,x=" + actual.getX() );
          //测试是否能改变actual的成员变量值
          tryChange(actual);
          System.out.println("调用tryChange方法之后,x=" + actual.getX() );
        }
        public static void main(String args[]){
          Main  va = new Main();
          va.showDiffer();
        }
}
class OnlyTest{
    private int x = 0;
    //设置成员变量x的值
    public void setX(int ix){
      x = ix;
    }
    //获取成员变量x的值
    public int getX(){
      return x;
    }
  }

第61题

class Main {
    private int  x = 0;
    //定义一个无参数的构造方法,它必须和类同名
    public Main(){
 System.out.println("这是无参数的构造方法");
 setX(100);         //为成员变量赋值
    }
    //设置成员变量x的值
    public void setX(int x){
    	this.x = x;    
    }
    //获取成员变量x的值
    public int getX(){
        return x;
    }   
    public static void main(String args[]){
        Main oa = new Main();   //隐式调用无参数的构造方法
        System.out.println("x=" + oa.getX());       //输出成员变量x的值
    }
}

第62题

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		BaseColors bC=new RainbowColorsImp();//需要使用实现类进行实例化
                Scanner reader=new Scanner(System.in);
		int n=reader.nextInt();
		String colorName=bC.getColorName(n);//获取值为n=1,2,..,7的颜色名称.
		System.out.println(colorName+"色");//显示颜色名称
		System.out.println("The Main class is end.");
	}
}
interface BaseColors {
	//这里都是静态公共常量
	  int RED = 1, BLUE = 4;	//红、蓝
	  //这是一个抽象的公共方法
	  String getColorName(int color);//获取颜色名称
}
interface RGBColors extends BaseColors {
	  //这是静态常量
	  int  GREEN = 2;//增加 绿色
	}
class RGBColorsImp implements RGBColors{
	@Override
	public String getColorName(int color) {
		switch (color){
		case 1:
			return "红";
		case 2:
			return "绿";
		case 4:
			return "蓝";
		default:
			return "未知";
		}
	}
}
//彩虹七种颜色:红色、黄色、绿色、蓝色、粉色、棕色、紫色
//red,yellow,green,blue,pink,brown,purple
interface RainbowColors extends RGBColors { 
//新增加了4个成员常量
int YELLOW = 3, PINK = 5, BROWN = 6, PURPLE =7;
}
//彩虹七种颜色:红色、黄色、绿色、蓝色、粉色、棕色、紫色
class RainbowColorsImp extends RGBColorsImp implements RainbowColors{
	@Override
	public String getColorName(int color) {
		switch (color){
		case 3:
			return "黄";
		case 5:
			return "粉";
		case 6:
			return "棕";
		case 7:
			return "紫";
		default:
			return super.getColorName(color);
		}
	}
}

第63题

public class Main {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Baseclass b=new Baseclass();
        int n=b.getColorValue();
        System.out.println("n="+n);
    }
}
//定义接口BaseColors
interface BaseColors {
      //这里都是静态公共常量
      int RED = 1, GREEN = 2, BLUE = 4;    
      //这是一个抽象的公共方法
      int getColorValue(int color);
}
//接口RainbowColors继承了BaseColors
interface RainbowColors
extends BaseColors {
    //新增加了4个成员常量
      int YELLOW = 3, ORANGE = 5,
              INDIGO = 6, VIOLET =7;
//还自动继承了父接口的3个成员常量和1个抽象方法      
      
}
//接口PrintColors继承了BaseColors
interface PrintColors
extends BaseColors {
    //增加3个成员常量
      int  YELLOW = 8, CYAN = 16, MAGENTA = 32;
    //覆盖了父接口的成员方法,仍然是抽象的
      int  getColorValue(int color);
    //还可以重载,和其它方法名相同,但参数有差异
      int getColorValue();
}
//接口PrintColors继承了BaseColors
interface LotsOfColors extends RainbowColors, PrintColors {
      //这是多重继承,增加3个成员常量
       int FUCHSIA = 17, VERMILION = 43, CHARTREUSE = RED+90; 
    }

class Baseclass implements LotsOfColors {
@Override
public int getColorValue() {
// TODO Auto-generated method stub
return 0;
}
@Override
public int getColorValue(int color) {
// TODO Auto-generated method stub
return 0;
}
}

第64题

import java.util.*;
public class Main extends Ancestor{ //定义一个子类
  private int defVar = 100;        //将父类的defVar隐藏起来
  public int getValue(){
     return defVar;
  }
  public static void main(String args[]){
    Main oa = new Main();
    System.out.println("oa.defVar="+oa.defVar);
    System.out.println("oa.getValue()="+oa.getValue());
    oa.proShow();
    oa.pubShow();
  }
}
class Ancestor{
  private     int priVar = 1;
               int defVar = 2;
  protected  int proVar = 3;
  public      int pubVar = 4;
  private  void priShow(){
     System.out.println("This is a private method");
  }
  void defShow(){
     System.out.println("This is a default method");
  }
  protected void proShow(){
     System.out.println("This is a protected method,defVar="+defVar);
  }
  public void pubShow(){
     System.out.println("This is a public method,pubVar="+pubVar);
  }
}

第65题

import java.util.*;
public class Main
{
   public static void main(String[] args)
   {
      Scanner reader=new Scanner(System.in);
      long n=reader.nextLong();
      Calc a=new Calc();
      boolean b=a.judge(n);
      if(b)
     System.out.println("yes");
      else
     System.out.println("no");
      System.out.println("The Main class is end.");
   }
}
 
class Calc {
public Calc(){
}
public boolean judge(long n){
/*-----请编写代码-----*/
String s=String.valueOf(n);

char[] ch = s.toCharArray();

int i = 0,j = 0;
for(i = 0,j = ch.length-1;i < ch.length / 2;i++,j--)
{
	if(ch[i]!=ch[j])
		break;
}
if(i >= ch.length/2)
	return true;
else 
	return false;
 
 
 
/*-----编写的代码结束-----*/
}
}

第66题

public class Main {
public static void main(String[] args) {
try{
int i = 0;
i = 3 / i;// 将产生算术运算异常。
} catch (ArithmeticException e)// 找到了匹配的异常,执行该catch块。
{
System.out.println("发生了ArithmeticException异常");
}catch(ArrayIndexOutOfBoundsException e)// 不是匹配的异常,则不会再捕获
// 异常,如果发生数组下标(索引)超出范围所产生的异常,将执行该catch块。
{
System.out.println("发生了ArrayIndexOutOfBoundsException异常");
} catch (Exception e)
// 前两个catch块均无法捕获try块中发生的异常时,才会执行该catch块。
{
System.out.println("发生了异常");
}finally {
{
System.out.println("执行Finally");
}
}
}
}

第67题

import java.util.*;
public class Main {
    public static void main(String[] args) {
        // 键盘输入数n作为分母,做除法
        Scanner in = new Scanner(System.in);
        int y = 0;
        int n = 0;
        try{
            n = in.nextInt();
            y = (3 / n);
        } catch (InputMismatchException e) {//输入数据不匹配
           //  TODO Auto-generated catch block
             e.printStackTrace();//显示堆栈数据
             System.out.println("请输入整数!");
        } catch (ArithmeticException e) {
            // TODO Auto-generated catch block
            System.out.println("请输入非零整数!");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            System.out.println("出现了未知情况!");
        }
        System.out.println("程序正常结束!");
    }
}

第68题

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String st=in.nextLine();// 键盘输入字符串作为提示信息
        try {
            throw new MyException(st);
        } catch (MyException e) {
            // TODO Auto-generated catch block
            System.out.println(st);
        }
    }
}
// 子类,继承Exception
class MyException extends Exception{
    public MyException() {
        super();
        // TODO Auto-generated constructor stub
    }
    public MyException(String s) {
        super(s);
        // TODO Auto-generated constructor stub
    }
}

第69题

import java.util.*;
public class Main {
public static void main(String[] args) {
Show aShow =new Show();//显示元素
ArrayList<String> list1=new ArrayList<String>();
list1.ensureCapacity(200);
Scanner scanner=new Scanner(System.in);
list1.add("abc");
list1.add("123");
int i=scanner.nextInt();
list1.add(Integer.toString(i));//转换为字符串再加入
aShow.showList(list1);
Object[] ob=list1.toArray();//转换为字符串
String st1=(String)ob[0];//取出下标为0的元素
System.out.println("下标为0的元素为:"+st1);
String st2=list1.get(2);//取出下标为2的元素
System.out.println("下标为2的元素为:"+st2);
list1.remove(1);//删除下标为1的元素
aShow.showList(list1);
ArrayList<String> arrayList1 = new ArrayList();
arrayList1.add(0, "The" ); 
arrayList1.add( 1, "fox" );
arrayList1.add( 1, "jumps" );
arrayList1.addAll(1,list1);//将list1加入arrayList1
aShow.showList(arrayList1);
}
}
//显示元素的类
class Show{
public void showList(ArrayList<String> s) {
Iterator<String> iter=s.iterator();
while(iter.hasNext()){
String st=iter.next();
System.out.print(st+"  ");
}
System.out.println();
}
}

第70题

import java.util.*;

public class Main {
	public static void main(String[] args) {
		Show aShow = new Show();// 显示元素
		ArrayList<Integer> list1 = new ArrayList<Integer>();
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		list1.ensureCapacity(n);
		while (n-- != 0) {// 输入n个数
			int k = in.nextInt();
			list1.add(k);// 加入元素
		}
		Collections.sort(list1);// 排序
		aShow.showList(list1);
	}
}

// 显示元素的类
class Show {
	public void showList(ArrayList<Integer> list1) {
		Iterator<Integer> iter = list1.iterator();
		while (iter.hasNext()) {
			int st = iter.next();
			System.out.print(st + "  ");
		}
		System.out.println();
	}
}

第71题

import java.util.*;

public class Main {

public static void main(String[] args) {

Scanner reader=new Scanner(System.in);

int n=reader.nextInt();

ArrayList<Integer> a=new ArrayList<Integer>();

for(int i=0;i<n;i++)

a.add(reader.nextInt());

HashCodeSum x=new HashCodeSum();

System.out.println(x.sum(a));//显示结果

        System.out.println("The Main class is end.");

}

}

class HashCodeSum{

int sum(ArrayList<Integer> a){

int sum=0;

/*-----请编写代码-----*/
for(int i = 0;i < a.size();i++)
{
	sum+=a.get(i);
}

/*-----编写的代码结束-----*/

return (sum);

}

}

第72题

import java.util.*;

public class Main extends Thread {

static String s;

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

s = scanner.nextLine();

Main t = new Main();

t.start();

}

public void run() {

System.out.println(s);
}
}

第73题

import java.util.*;

public class Main implements Runnable{

static String s;

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

s = scanner.nextLine();

Thread t=new Thread(new Main());

t.start();

}

@Override

public void run() {

// TODO Auto-generated method stub

System.out.println(s);

}

}

第74题 删除字符串中多余空格(10分)

时间限制:1 秒 内存限制:32 兆
题目描述:
输入一个由若干单词组成的字符串(长度小于等于2000),然后删除字符串的首尾空格,如果串中有多于一个以上空格,则只保留一个空格。
输入:
测试数据有多组,每组输入一个字符串。
输出:
对于每组输入,删除字符串中多余空格,然后输出。

样例输入:

3
word  many word 
abcd     xyz 
xyz

样例输出:

word many word
abcd xyz
xyz
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		in.nextLine();
		for (int i = 0; i < n; i++) {
			char[] c = new char[2005];
			String s = in.nextLine();
			String s1 = s.trim();
			c = s1.toCharArray();
			String ans = "";
			for (int j = 0; j < c.length - 1; j++) {
				if (c[j] != ' ') {
					ans += c[j];
				} else if (c[j + 1] != ' ') {
					ans += c[j];
				}
			}
			ans += c[c.length - 1];
			System.out.println(ans);
		}
		in.close();
	}
}

第75题 字符串中单词首字母大写

时间限制:1 秒 内存限制:32 兆
题目描述:
输入一个由若干单词组成的字符串(长度小于等于2000),字符串的首尾没有空格,串中单词之间只有一个空格。
请将字符串中单词的首字母转换为大写,如果本身是大写,不要转换。
输入:
测试数据有多组,每组输入一个字符串。
输出:
对于每组输入,删除字符串中多余空格,然后输出。

样例输入:

3
Have Many words
abcd 123 Xyz
xyZ

样例输出:

Have Many Words
Abcd 123 Xyz
XyZ
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		in.nextLine();
		for (int i = 0; i < n; i++) {
			char[] c = new char[2005];
			String s = in.nextLine();
			c = s.toCharArray();
			String ans = "";
			if(c[0]>='a'&&c[0]<='z') {
				c[0]-=32;
			}	
			ans+=c[0];
			for (int j = 1; j < c.length; j++) {
				if(c[j-1]==' ') {
					if(c[j]>='a'&&c[j]<='z')
						c[j]-=32;
					ans+=c[j];
				}
				else {
					ans+=c[j];
				}
			}
			System.out.println(ans);
		}
		in.close();
	}
}
  • 10
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值