水仙花、大小写转化问题、正三角形的外接圆、数乌龟、摸底

1、水仙花数

描述

请判断一个数是不是水仙花数。

其中水仙花数定义各个位数立方和等于它本身的三位数。输入有多组测试数据,每组测试数据以包含一个整数n(100<=n<1000)

public class Demo2 {public static void main(String[] args) {

int x=154;
System.out.println(method(x));
}
public static boolean method(int x){
String str=x+"";
int [] arr=new int[str.length()];
for(int i=0;i<str.length();i++){
char ch=str.charAt(i);
arr[i]=Integer.parseInt(ch+"");
}
if(arr[0]*arr[0]*arr[0]+arr[1]*arr[1]*arr[1]+arr[2]*arr[2]*arr[2]==x){
return true;
}
return false;
}
}

2、大小写互换

描述:现在给出了一个只包含大小写字母的字符串,不含空格和换行,要求把其中的大写换成小写,小写换成大写,然后输出互换后的字符串。

输入:第一行只有一个整数m(m<=10),表示测试数据组数。

接下来的m行,每行有一个字符串(长度不超过100)。

输出:输出互换后的字符串,每组输出占一行。

public class Demo3 {

public static void main(String[] args) {
String str="abcDEF";
System.out.println(method(str));
}
public static String method(String str){
String s="";
for(int i=0;i<str.length();i++){
char ch=str.charAt(i);
if(ch>='a'&&ch<='z'){
ch=(char)(ch-32);
}else if(ch>='A'&&ch<='Z'){
ch=(char)(ch+32);
}
s=s+ch;
}
return s;
}
}

3、正三角形的外接圆面积

描述:给你正三角形的边长,pi=3.1415926 ,求正三角形的外接圆面积。

输入:只有一组测试数据 第一行输入一个整数n(1<n<1000)表示接下来要输入n个边长m(1.0<=m<1000.0)

输出:输出每个正三角形的外接圆面积,保留两位小数,每个面积单独占一行。

public class Demo03 {

public static void main(String[] args) { //客户端

System.out.println(round2(getArea(13)));

System.out.println(round(3.1415926,3));
System.out.println(round(3.7485926f,4));
}
//求正三角形外接圆面积
public static double getArea(int m){
//已知正三角形外接圆面积公式:PI*m*m/3
double s=Math.PI*m*m/(3*1.0);
return s;
}

//对一个double类型进行四舍五入保留两位小数的操作
public static double round2(double d){
return (Math.round(d*100))/100.0; //系统提供的round方法
}
//=============round 方法double类型的数据小数保留n位
public static double round(double d,int n){
//"1"+n0
String str="1";
while(n--!=0){
str+="0";
}
int x=Integer.parseInt(str);
return (Math.round(d*x))/(x*1.0);
}

//=============round 方法float类型的数据小数保留n位
public static double round(float d,int n){
String str="1";
while(n--!=0){
str+="0";
}
int x=Integer.parseInt(str);
return (Math.round(d*x))/(x*1.0);
}
}

4、数乌龟

描述:有一只母乌龟 ,它每年年初生一头小母乌龟 。每头小母乌龟 从第四个年头开始,每年年初生一头小母乌龟 。请你计算第n年是共有多少只母乌龟 (第一年是有一头母乌龟)

输入:输入多组测试数据,每组测试数据占一行,输入一个整数n(0<n<56)n含义如题所示,n=0是表示输入结束

输出:每组输出数据占一行,输出第在第n年的时候母乌龟的数量。


public class Demo1 {

public static void main(String[] args) {
System.out.println(method(5));
}
public static int method(int x){
if(x<=4){
return x;}
int[] a=new int[x];
a[0]=1;
a[1]=2;
a[2]=3;
a[3]=4;
for(int i=4;i<x;i++){
a[i]=a[i-1]+a[i-3];
}
return a[x-1];
}
}

5、摸底

描述:HEIHEI该上初中了,老师想摸一下同学们的底看看同学们的学习情况,在小学毕业时考了7个科目,现在老师要求班里的每个同学算出他7个科目中最高的成绩、最低的成绩,中间排名的成绩,这三个成绩的平均值,结果保留两位小数。然后同学们把算出的平均值写在纸上交给老师。例如HEIHEI的成绩是 65 78 45 89 90 73 80,那么HEIEHI就应该算出45+90+78的平均值,71.00.

输入:第一行给出一个数n,表示总共有几个同学。(1<=n<=60)

输出:输出占一行,每行输出同学的平均成绩。

import java.util.Arrays;

public class Demo4 {
public static void main(String[] args){
int[] arr={45 ,65 ,73 ,78 ,80 ,89 ,90};
System.out.println(method(arr));
}
public static String method(int[] arr){
Arrays.sort(arr);
double d=(arr[0]+arr[arr.length-1]+arr[(arr.length-1)/2])/3.0;
return "最高分"+arr[arr.length-1]+"最低分"+arr[0]+"平均分"+d;
}

原文地址:https://www.cnblogs.com/w-xibao/p/7977794.html