2020-09-02(方法的练习,递归的思想)

tech2022-08-06  99

1.计算从1到某个数以内所有奇数的和。 2.计算从1到某个数以内所有能被3或者17整除的数的和。 3.计算1到某个数以内能被7或者3整除但不能同时被这两者整除的数的个数。 4.计算1到某个数以内能被7整除但不是偶数的数的个数。 5.求2〜某个数之内的素数。【素数 : 只能被1或本身整除的数】 6.判断某个年份是否是闰年。 7.已知有一个数列:f(0) = 1,f(1) = 4,f(n+2) = 2 * f(n+1) + f(n),其中n是大于0的整数,求f(n)的值(提示:使用递归) 8.求2+22+222+2222。 9.使用递归实现10 的阶乘。 10.求某个三位数以内的水仙花数: 水仙花数:一个数各个位上的立方之和,等于本身 例如: 153 = 1(3) + 5(3)+3(3) = 1+125+27 = 153 package workdemo; import java.util.Scanner; /** * 计算1~n的奇数和 * @author WDD * */ public class Demo1 { public static void main(String[] args) { System.out.println("*******计算1~n的奇数和*********"); Scanner sc = new Scanner(System.in); System.out.println("请输入n的值:"); int n = sc.nextInt(); //当没有static时,采用这种方法 new一个对象,由对象调用方法 //Demo1 a = new Demo1(); //int num = a.num(n); int num = num(n); System.out.println("1~"+n+"的奇数和为:"+num); sc.close(); } //创建一个方法计算1~n的奇数的和 public static int num(int n) { int s = 0; for (int i = 1; i <= n; i++) { if(i%2 != 0) { s += i; } } return s; } } package workdemo; import java.util.Scanner; /** * 计算从1到某个数以内所有能被3或者17整除的数的和。 * @author WDD * */ public class Demo2 { public static void main(String[] args) { System.out.println("计算从1~n以内所有能被3或者17整除的数的和"); Scanner sc = new Scanner(System.in); System.out.println("请输入n:"); int n = sc.nextInt(); //当没有static时 //Demo2 b = new Demo2(); //int num = b.num(n); int num = num(n); System.out.println("1~"+n+"以内所有能被3或者17整除的数的和为:"+num); sc.close(); } public static int num(int n) { int a = 0;// 定义一个变量,记录结果 for (int i = 1; i <= n; i++) { if(i%3 == 0 || i%17 ==0) { a += i; } } return a; } } package workdemo; import java.util.Scanner; /** * 计算1到某个数以内能被7或者3整除但不能同时被这两者整除的数的个数。 * @author WDD * */ public class Demo3 { public static void main(String[] args) { System.out.println("计算1~n能被7或者3整除但不能同时被这两者整除的数的个数。"); Scanner sc = new Scanner(System.in); System.out.println("请输入n的值:"); int n = sc.nextInt(); //没有static时: //Demo3 a = new Demo3(); //int number = a.number(n); int number = number(n); System.out.println("1~n能被7或者3整除但不能同时被这两者整除的数的个数为:"+number); sc.close(); } public static int number(int a) { int count = 0;//定义一个变量,记录满足条件的值的个数 for (int i = 1; i <= a; i++) { if ((i%3 == 0 || i%7 == 0) && i%21 != 0) { count++; /*if (i%3 == 0 && i%7 == 0) { count--; }*/ } } return count; } } package workdemo; import java.util.Scanner; /** * 计算1到某个数以内能被7整除但不是偶数的数的个数。 * @author WDD * */ public class Demo4 { public static void main(String[] args) { System.out.println("计算1~n能被7整除但不是偶数的数的个数。"); Scanner sc = new Scanner(System.in); System.out.println("请输入n的值:"); int n = sc.nextInt(); int s = num(n); System.out.println("1~n能被7整除但不是偶数的数的个数为:" + s); //当没有static时: //Demo4 a = new Demo4(); //int s = a.num(n); sc.close(); } public static int num(int a) { int count = 0; //记录满足要求的数的个数 for (int i = 1; i <= a; i++) { if (i%7 == 0 && i%2 != 0) { count++; } } return count; } } package workdemo; import java.util.Scanner; /** * 求2〜某个数之内的素数。素数 : 只能被1或本身整除的数 * @author WDD * */ public class Demo5 { public static void main(String[] args) { System.out.println("求2〜n之内的素数。"); Scanner sc = new Scanner(System.in); System.out.println("请输入n的值:"); int n = sc.nextInt(); primeNumber(n); sc.close(); } public static void primeNumber(int a) { for (int i = 2; i <= a; i++) { int count = 0; for (int j = 2; j < i; j++) { if (i%j == 0) { count++; break; } } if (count == 0) { System.out.print(i+" "); } } } } package workdemo; import java.util.Scanner; /** * 判断某个年份是否是闰年。 * @author WDD * */ public class Demo6 { public static void main(String[] args) { System.out.println("判定输入的年份是不是闰年:"); Scanner sc = new Scanner(System.in); System.out.println("请输入要判定的年份:"); int year = sc.nextInt(); //没有static的写法: //Demo6 a = new Demo6(); //a.leapYear(year); leapYear(year); sc.close(); } public static void leapYear(int a) { if ((a%4==0&&a%100!=0)||a%400==0) { System.out.println(a+"是闰年。"); } else { System.out.println(a+"不是闰年。"); } } } package workdemo; import java.util.Scanner; /** * 已知有一个数列:f(0) = 1,f(1) = 4,f(n+2) = 2 * f(n+1) + f(n), * 其中n是大于0的整数,求f(n)的值(提示:使用递归) * @author WDD * 递归的思想!!! */ public class Demo7 { public static void main(String[] args) { System.out.println("求f(n)的值:"); Scanner sc = new Scanner(System.in); System.out.println("请输入n的值:"); int a = sc.nextInt(); //当没有static时: //Demo7 b = new Demo7(); //int c = b.f(a); int c = f(a); System.out.println("f("+a+")=" + c); sc.close(); } public static int f(int n) { if (n == 0) { return 1; } else if (n == 1) { return 4; } else { int s = 2*f(n-1) + f(n-2); return s; } } } package workdemo; /** * 求2+22+222+2222 * @author WDD * 分析:f(1)=2 * f(2)=22=10*f(1)+2 * f(3)=222=10*f(2)+2 * ... * f(n)=10*f(n-1)+2 * 说到底还是递归..... */ public class Demo8 { public static void main(String[] args) { int sum = 0; for (int i = 1; i <= 4; i++) { sum += f(i); } System.out.println("2+22+222+2222="+sum); } public static int f(int a) { if(a == 1) { return 2; } else { int s = 10*f(a-1)+2; return s; } } } package workdemo; import java.util.Scanner; /** * 使用递归实现10 的阶乘--->实现输入的数的阶乘 * @author WDD * */ public class Demo9 { public static void main(String[] args) { System.out.println("********实现n的阶乘*******"); Scanner sc = new Scanner(System.in); System.out.println("请输入n的值:"); int a = sc.nextInt(); //没有static: //Demo9 a = new Demo9(); //System.out.println(a+"!="+a.factorial(a)); //int s = factorial(a); System.out.println(a+"!="+factorial(a)); sc.close(); } public static int factorial(int a) { if (a == 1) { return 1; } else { int n = a * factorial(a-1); return n; } } } package workdemo; /** * 求某个三位数以内的水仙花数: * 水仙花数:一个数各个位上的立方之和,等于本身 * 例如: 153 = 1(3) + 5(3)+3(3) = 1+125+27 = 153 * @author WDD * */ public class Demo10 { public static void main(String[] args) { for (int i = 100; i < 1000; i++) { int x = i/100; int y = i/10%10; int z = i%10; if (x*x*x+y*y*y+z*z*z == i) { System.out.print(i+ " "); } } } }

 

最新回复(0)