Java程序设计基础练习50题(上)
Java程序设计基础练习50题(中)
package e201_06_01;
public class shuixianhua {
public static void main(String[] args) {
for (int i = 100; i <1000 ; i++) {
int firstNum = i/100;
int secondNum = i/10%10;
int thirdNum = i%10;
if(Math.pow(firstNum, 3) + Math.pow(secondNum, 3)+Math.pow(thirdNum, 3) == i){
System.out.println("The narcissus number is:" + i);
}
}
}
}
按照三角函数泰勒级数展开式计算正弦函数值: s i n x ( x ) = x − x 3 3 ! + x 5 5 ! − x 7 7 ! sinx(x)=x-\frac{x^3}{3!}+\frac{x^5}{5!}-\frac{x^7}{7!} sinx(x)=x−3!x3+5!x5−7!x7,直到最后一项的绝对值小于 1 0 − 6 10^{-6} 10−6。
package e201_06_02;
import java.util.Scanner;
public class taylor {
public static void main(String[] args) {
// TODO Auto-generated method stub
@SuppressWarnings("resource")
Scanner sca = new Scanner(System.in);
System.out.println("Please input radians:");
double x = sca.nextDouble();
int n=1;int s=1;double temp=x;double sum=0;
while( Math.abs(temp) > 1e-6) {
sum+=temp;s=-s;n+=2;
temp=s*Math.pow(x,n)/fact(n);
}
System.out.printf("sin(%f)=%f",x,sum);
}
private static double fact(int n) {
long t = 1;
for (int i = 2;i<=n;i++){
t *= i;
}
return t;
}
}
按照如下公式计算常数e的值: e = 1 + 1 + 1 2 + 1 3 ! + ⋯ + 1 n ! e=1+1+\frac{1}{2}+\frac{1}{3!}+\dots+\frac{1}{n!} e=1+1+21+3!1+⋯+n!1
package e201_06_03;
public class constante {
public static void main(String[] args) {
// TODO Auto-generated method stub
double temp=1;int n=1;double sum=1;
while( temp>1e-6) {
sum+=temp;n+=1;
temp=1/fact(n);
}
System.out.print("The constant e is:"+sum);
}
private static double fact(int n) {
long t = 1;
for (int i = 2;i<=n;i++){
t *= i;
}
return t;
}
}
输入18位的身份证号码,要求首先验证身份证号码合法性(仅利用校验位验证),如果合法则输出性别和出生日期。
说明:
(1)身份证号码的7~12位表示出生年月;第17位表示性别,奇数为男,偶数为女;最后一位为校验位
(2)校验算法:将前面的身份证号码17位数加权求和(系数分别7、9、10、5、8、4、2、1、6、3、7、9、10、5、8、4、2),然后除以11,根据余数找到对应的校验位(分别为1、0、X 、9、8、7、6、5、4、3、2),如果计算的校验位和输入的校验位一致说明身份证号码合法。比如身份证号码53010219200508011X,前17位加权求和:
(57)+(39)+(010)+(15)+(08)+(24)+(12)+(91)+(26)+(03)+(07)+(59)+(010)+(85)+(08)+(14)+(1*2=189
189%11 = 2
序号为2的校验位就是X,所以身份证号码合法。
package e201_06_04;
import java.util.Scanner;
public class identity {
public static void main(String[] args) {
// TODO Auto-generated method stub
@SuppressWarnings("resource")
Scanner sca = new Scanner(System.in);
System.out.println("please input ID number:");
String card = sca.nextLine();String sex;
if(!IdCard(card)) {
System.out.println(IdCard(card));
}else {
int s=Integer.parseInt(String.valueOf(card.charAt(16)));
if(s%2==1) {
sex = "man";
}else {
sex = "woman";
}
System.out.println(IdCard(card)+" birthday:"+card.substring(6, 14)+" sex:" +sex);
}
}
public static boolean IdCard(String card){
if(card == null || "".equals(card)){
return false;
}
String requal = "(^[1-9]\\d{5}(18|19|20)\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|(10|20|30|31))\\d{3}[0-9Xx]$)|"
+"(^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}$)";
boolean matches = card.matches(requal);
if(matches){
if (card.length() == 18){
try {
char[] charArray = card.toCharArray();
//1,将前面的身份证号码17位数分别乘以不同的系数,系数为此处的 加权因子
int[] idCardWi = {7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
int sum = 0;
// 2,将这17位数字 和系数相乘 的结果相加
for (int i = 0;i < idCardWi.length;i++){
int current = Integer.parseInt(String.valueOf(charArray[i]));
int count = current * idCardWi[i];
sum += count;
}
char idCardLast = charArray[17];
// 3,结果和 除以11,查看余数
int idCardMod = sum % 11;
// 4,这是除以 11后,可能产生的 11位余数对应的验证码(--对应下标),其中 X 代表罗马数字 10
String[] idCardY = {"1","0","X","9","8","7","6","5","4","3","2"};
if (idCardY[idCardMod].toUpperCase().equals(String.valueOf(idCardLast).toUpperCase())){
return true;
}else {
System.out.println("Illegal ID card:");
}
}catch (Exception e){
e.printStackTrace();
System.out.println("error:"+card);
return false;
}
}
}
return matches;
}
}
package e201_06_05;
import java.util.Scanner;
public class combination {
public static void main(String[] args) {
// TODO Auto-generated method stub
@SuppressWarnings("resource")
Scanner sca = new Scanner(System.in);
System.out.println("please input m and n");
int m=sca.nextInt();int n=sca.nextInt();
int C=(int) (fact(m)/(fact(n)*fact(m-n)));
System.out.println("Result is "+C);
}
private static double fact(int n) {
long t = 1;
for (int i = 2;i<=n;i++){
t *= i;
}
return t;
}
}
10个小孩围成一圈分糖果,老师分给第一个孩子10块,第二个小孩2块,后面依次分的糖果数量为10、2、8、22、16、4、10、6、14、20。然后所有的小孩同时将手中的糖果分一半给右边的小孩,糖果为奇数的可向老师要一块。问经过几次后,大家手中的糖果的块数将一样多,每个人有多少糖果。
package e201_06_06;
public class pointsCandy {
public static void main(String[] args) {
int[] arr = {10,2,8,22,16,4,10,6,14,20};
int count= 0;//计数器
while (!Same(arr)){
arr = distributeCandy(arr);
count++;
}
System.out.printf("After %d times, and the number of candies was %d",count,arr[0]);
}
//@return 返回分完后的数组
private static int[] distributeCandy(int[] arr) {
arr[0]= arr[9]/2+arr[0]/2;
for (int i=9; i>0;i--){
arr[i] = arr[i]/2+arr[i-1]/2;
}
for (int i=0; i<arr.length;i++){
if(arr[i]%2 != 0){
arr[i]+=1;
}
}
return arr;
}
//判断每个小朋友的糖果数是否相等
private static boolean Same(int[] arr) {
int count= 0;
boolean flag= false;
for (int i=0;i<arr.length-1; i++){
if(arr[i]== arr[i+1]){
count++;
}
}
if(count== arr.length-1){
flag = true;
}
return flag;
}
}
package e201_06_07;
import java.util.Arrays;
import java.util.Scanner;
public class cableckNumber {
public static void main(String[] args) {
@SuppressWarnings("resource")
Scanner sca = new Scanner(System.in);
System.out.println("please input a number:");
int num =sca.nextInt();
while(num != 6174){
num = sortNum(num);
}
}
private static int sortNum(int num){
int b1000 = num / 1000;
int b100 = num / 100%10;
int b10 = num % 100 / 10;
int b1 = num % 10;
int[] b = {b1,b10,b100,b1000};
Arrays.sort(b);
int max=b[3]*1000+b[2]*100+b[1]*10+b[0];
int min=b[0]*1000+b[1]*100+b[2]*10+b[3];
System.out.printf("%d - %d = %d \n",max,min,max-min);
return (max-min);
}
}
求s=a+aa+aaa+aaaa+aa…a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加有键盘控制。
package e201_07_01;
import java.util.Scanner;
public class andsa {
public static void main(String[] args) {
// TODO Auto-generated method stub
@SuppressWarnings("resource")
Scanner sca = new Scanner(System.in);
System.out.println("please inpue n and a");
int n = sca.nextInt();int a = sca.nextInt();
int sum=a;int temp=a;
for(int i=1;i<n;i++) {
temp=(int) (a*Math.pow(10,i))+temp;
sum+=temp;
}
System.out.println("S="+sum);
}
}
求a的算数平方根: 重复计算 x = ( x + a x ) / 2 x=(x+\frac{a}{x})/2 x=(x+xa)/2,直至相邻的两个解非常接近(比如差小于 1 0 − 6 10^{-6} 10−6)。
package e201_07_02;
import java.util.Scanner;
public class Newton {
public static void main(String[] args) {
// TODO Auto-generated method stub
@SuppressWarnings("resource")
Scanner sca = new Scanner(System.in);
System.out.print("please input a number:");
double a = sca.nextDouble();
double x = a/2;double temp = 0;
while (Math.abs(x-temp)>1e-6) {
temp=x;x=(x+a/x)/2;
}
System.out.printf("The arithmetic square root of %f is %f",a,x);
}
}
package e201_07_03;
import java.util.Scanner;
public class leastCommonMultiple {
public static void main(String[] args){
System.out.println("please input two numbers:");
Scanner sca = new Scanner(System.in);
int a = sca.nextInt();
int b = sca.nextInt();
int m = min(a, b);
int n = a * b / m;
System.out.println("least common multiple is"+n);
}
public static int min(int a, int b){
if(a < b){
int t = a;
a = b;
b = t;
}
while(b != 0){
if(a == b){
return a;
}else{
int k = a % b;
a = b;
b = k;
}
}
return a;
}
}
package e201_07_04;
import java.util.Scanner;
public class cyclotomy {
public static void main(String[] args) {
int i=0,s=6;
double k=3.0,len=1.0;
System.out.println("please input the number of cuts:");
@SuppressWarnings("resource")
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
while(i<=n){
s*=2;
len=2-Math.sqrt(4-len);
i++;
k*=2.0;
}
System.out.println(s+" edge type,Pi="+k*Math.sqrt(len));
}
}
一个富二代给他儿子的四年大学生活存一笔钱,富三代每月只能取3k作为下个月的生活费,采用的是整存零取的方式,年利率在1.71%,请问富二代需要一次性存入多少钱。
package e201_07_05;
public class rich {
public static void main(String[] args) {
double deposit = 0;
for (int i = 1; i <= 48; i++) {
deposit = deposit/(1+0.0171/12)+3000;
}
System.out.printf("%f yuan",deposit);
}
}
有一个三位数,个位数字比百位数字大,而百位数字又比十位数字大,并且各位数字之和等于各位数字相乘之积,求此三位数。
package e201_07_06;
public class threeDigit {
public static void main(String[] args) {
int i,j,k;
for(i=2;i<=9;i++) {
for(j=1;j<i;j++) {
for(k=0;k<j;k++) {
if((i+j+k)==i*j*k) {
System.out.println(i+j*100+k*10);
}
}
}
}
}
}
蜘蛛有8条腿,蜻蜓有6条腿和2对翅,蝉有6条腿和1对翅。三种虫子共18只,共有118条腿和20对翅。问每种虫子各几只?
package e201_07_07;
public class leg {
public static void main(String[] args) {
// TODO Auto-generated method stub
double a1=1,a2=8,a3=0,b1=1,b2=6,b3=2,c1=1,c2=6,c3=1,d1=18,d2=118,d3=20,D,D1,D2,D3;
D = a1*(b2*c3 - b3*c2) - a2*(b1*c3 - b3*c1) + a3*(b1*c2 - b2*c1);
D1 = d1*(b2*c3 - b3*c2) - d2*(b1*c3 - b3*c1) + d3*(b1*c2 - b2*c1);
D2 = a1*(d2*c3 - d3*c2) - a2*(d1*c3 - d3*c1) + a3*(d1*c2 - d2*c1);
D3 = a1*(b2*d3 - b3*d2) - a2*(b1*d3 - b3*d1) + a3*(b1*d2 - b2*d1);
System.out.printf("spider :%f,dragonfly :%f,cicada :%f",D1/D,D2/D,D3/D);
}
}
n个猴子围成一圈,从某个开始报数1-2-3-1-2-3-……报“3”的猴子就被淘汰,游戏一直进行到圈内只剩一只猴子它就是猴大王了。
package e201_07_08;
import java.util.Scanner;
public class monkey {
public static void main(String[] args) {
// TODO Auto-generated method stub
@SuppressWarnings("resource")
Scanner sca =new Scanner(System.in);
int n,t,j;
System.out.println("please input numbers of monkey:");
n=sca.nextInt();
int[] array=new int[n];
for(int i=0;i<n;i++){
array[i]=1;
}
t=-1;
for(int i=1;i<=n;i++) {
j = 1;
while (j <= 3) {
t = (t + 1) % n;
if (array[t] == 1) {
j++;
}
}
if(i==n){
System.out.print(t + 1);
}
array[t] = 0;
}
}
}
n个台阶,上楼可以一步上1阶,也可以一步上2阶,一共有多少种上楼的方法
package e201_07_09;
import java.util.Scanner;
public class step {
public static void main(String[] args){
@SuppressWarnings("resource")
Scanner sca = new Scanner(System.in);
System.out.print("Please input steps:");
int n = sca.nextInt();
System.out.println(getstep(n));
}
private static int getstep(int n){
if(n < 0)
return -1;
if(n <= 2)
return n;
return getstep(n-1)+getstep(n-2);
}
}