第十四周.

桃扇骨 2022-10-18 04:55 300阅读 0赞

回溯法

  • A、菱形图案
    • 代码
  • B、牛妹的蛋糕
    • 代码
  • C、尼科彻斯定理
    • 代码
  • D、ABC + DEF = GHI
    • 代码
  • E、油田问题
    • 代码
  • F、马的遍历问题
    • 代码

A、菱形图案

题目描述

  • KiKi学习了循环,BoBo老师给他出了一系列打印图案的练习,该任务是打印用“*”组成的菱形图案。

输入

  • 多组输入,一个整数(2~20)。

输出

  • 针对每行输入,输出用“”组成的菱形,每个“”后面有一个空格。每输出一个菱形的后面需要空一行。

样例输入 Copy

  1. 2
  2. 3
  3. 4

样例输出 Copy

  1. *
  2. * *
  3. * * *
  4. * *
  5. *
  6. *
  7. * *
  8. * * *
  9. * * * *
  10. * * *
  11. * *
  12. *
  13. *
  14. * *
  15. * * *
  16. * * * *
  17. * * * * *
  18. * * * *
  19. * * *
  20. * *
  21. *

链接:菱形图案

代码

  1. import java.util.Scanner;
  2. public class Main {
  3. public static void solve(int n){
  4. for(int i=0;i<n;i++){ //上三角
  5. for(int j=0;j<n-i;j++)
  6. System.out.print(" ");
  7. for(int k=0;k<=i;k++)
  8. System.out.print("* ");
  9. System.out.println();
  10. }
  11. }
  12. public static void solve1(int n){
  13. System.out.println();
  14. for(int i=n;i>0;i--){ //下三角
  15. for(int j=0;j<=n-i;j++)
  16. System.out.print(" ");
  17. for(int k=i;k>0;k--)
  18. System.out.print("* ");
  19. System.out.println();
  20. }
  21. }
  22. public static void main(String[] args) {
  23. // TODO Auto-generated method stub
  24. Scanner sc = new Scanner(System.in);
  25. while(sc.hasNext()){
  26. int n = sc.nextInt();
  27. solve(n);
  28. int m = n;
  29. while(m>=0){
  30. System.out.print("* ");
  31. m--;
  32. }
  33. solve1(n);
  34. System.out.println();
  35. }
  36. }
  37. }

B、牛妹的蛋糕

题目描述

  • 众所周知,牛妹非常喜欢吃蛋糕。

    第一天牛妹吃掉蛋糕总数三分之一多一个,第二天又将剩下的蛋糕吃掉三分之一多一个,以后每天吃掉前一天剩下的三分之一多一个,到第n天准备吃的时候只剩下一个蛋糕。

    牛妹想知道第一天开始吃的时候蛋糕一共有多少呢?

输入

  • 输入n,0<n< 30。

输出

  • 输出第一天蛋糕的数量。

样例输入 Copy

  1. 2
  2. 4

样例输出 Copy

  1. 3
  2. 10

链接:牛妹的蛋糕

代码

  1. import java.util.Scanner;
  2. public class Main {
  3. public static void main(String[] args) {
  4. Scanner sc = new Scanner(System.in);
  5. while(sc.hasNext()) {
  6. int n = sc.nextInt();
  7. int cbb[] = new int[105];
  8. //第一天一个蛋糕
  9. cbb[1] = 1;
  10. for(int i=2;i<cbb.length;i++) {
  11. //下一天的蛋糕数
  12. cbb[i] = ((cbb[i-1]+1)*3)/2;
  13. }
  14. //输出第n天的蛋糕数
  15. System.out.println(cbb[n]);
  16. }
  17. }
  18. }

C、尼科彻斯定理

题目描述

  • 验证尼科彻斯定理,即:任何一个整数m的立方都可以写成m个连续奇数之和。

例如:

  1. 1^3=1
  2. 2^3=3+5
  3. 3^3=7+9+11
  4. 4^3=13+15+17+19

输入

  • 多组输入,输入一个整数。

输出

  • 输出分解后的字符串。

样例输入 Copy

  1. 6

样例输出 Copy

  1. 31+33+35+37+39+41

链接:戳我!戳我!

代码

  1. import java.util.Scanner;
  2. public class Main {
  3. public static void main(String[] args) {
  4. // TODO Auto-generated method stub
  5. Scanner sc = new Scanner(System.in);
  6. while(sc.hasNext()){
  7. int n = sc.nextInt();
  8. if(n==1)
  9. System.out.println(1);
  10. int m = (int) Math.pow(n, 3);//m = n^3
  11. int count = 0;
  12. for(int i=1;i<(m/2);i+=2){ //最小的奇数(1除外)小于n^3的一半
  13. int a = i;
  14. count = i;//第一个是i,总共是n个奇数相加
  15. for(int j=1;j<n;j++){
  16. a+=2;
  17. count+=a;//n个奇数和
  18. }
  19. if(count!=m)
  20. continue;
  21. else{
  22. for(int k=0;k<n;k++){
  23. if(k==n-1)
  24. System.out.println(i+2*k);
  25. else
  26. System.out.print((i+2*k)+"+");
  27. }
  28. }
  29. }
  30. System.out.println();
  31. }
  32. }
  33. }

D、ABC + DEF = GHI

题目描述

  • 用1, 2, 3…9 这九个数字组成一个数学公式,满足:ABC + DEF = GHI,每个数字只能出现一次,编写程序输出所有的组合。

输入

输出

  • 输出所有的 ABC + DEF = GHI, 每行一条数据,格式为ABC+DEF=GHI
    输出结果按照ABC升序排列,如果ABC相同,则按照DEF升序排列。

链接: ABC + DEF = GHI

代码

  1. import java.util.Scanner;
  2. public class Main {
  3. static int a[] = new int[15];
  4. static int b[] = new int[15];
  5. static void solve(int n){
  6. if(n==10){
  7. int x = a[1]*100+a[2]*10+a[3];
  8. int y = a[4]*100+a[5]*10+a[6];
  9. int z = a[7]*100+a[8]*10+a[9];
  10. if(x+y==z)
  11. System.out.println(x+"+"+y+"="+z);
  12. return;
  13. }
  14. for(int i=1;i<10;i++){
  15. if(b[i]==0){
  16. a[n] = i;
  17. b[i] = 1;
  18. solve(n+1);
  19. b[i] = 0;
  20. }
  21. }
  22. }
  23. public static void main(String[] args) {
  24. // TODO Auto-generated method stub
  25. for(int i=0;i<a.length;i++){
  26. a[i] = 0;
  27. b[i] = 0;
  28. }
  29. solve(1);
  30. }
  31. }

E、油田问题

题目描述

  • 输入一个m行n列的字符矩阵,统计字符“@”组成多少个八连块。如果两个字符“@”所在的格子相邻(横、竖或者对角线方向),即属于同一个八连块。

输入

  • 多组输入 输入行数m,以及列数n。 然后输入*和@ 1<=n,m<=100

输出

  • 联通块个数

样例输入 Copy

  1. 5 5
  2. ****@
  3. *@@*@
  4. *@**@
  5. @@@*@
  6. @@**@

样例输出 Copy

  1. 2

链接:油田问题

代码

  1. import java.util.Scanner;
  2. public class Main {
  3. static String str[] = new String[105];
  4. static char ch[][] = new char[105][105];
  5. static int idx[][] = new int[105][105];
  6. static int m,n;
  7. static int count;
  8. public static void solve(int x,int y,int id) {
  9. if(x<0||x>=m||y<0||y>=n)//超出数组,遍历完毕
  10. return ;
  11. if(idx[x][y]>0||ch[x][y]!='@')//都已经判断完成,或者没有油
  12. return ;
  13. idx[x][y] = id;
  14. for(int dx=-1;dx<=1;dx++)//遍历八个方向
  15. for(int dy=-1;dy<=1;dy++) {
  16. if(dx!=0||dy!=0)
  17. solve(x+dx,y+dy,id);
  18. }
  19. }
  20. public static void main(String[] args) {
  21. Scanner sc = new Scanner(System.in);
  22. while (sc.hasNext()) {
  23. m = sc.nextInt();//行
  24. n = sc.nextInt();//列
  25. count = 0;
  26. for (int i = 0; i < m; i++)
  27. str[i] = sc.next();
  28. for (int i = 0; i < m; i++)
  29. for (int j = 0; j < n; j++) {
  30. idx[i][j] = 0;
  31. ch[i] = str[i].toCharArray();//获取字符
  32. }
  33. for (int i = 0; i < m; i++)
  34. for (int j = 0; j < n; j++) {
  35. if(idx[i][j]==0&&ch[i][j]=='@')//没有搜过并且有油
  36. solve(i,j,++count);//遍历其他八个方向
  37. }
  38. System.out.println(count);
  39. }
  40. }
  41. }

F、马的遍历问题

题目描述

  • 在5*4的棋盘中,马只能走斜“日”字。马从位置(x, y)处出发,把棋盘的每一格都走一次,且只走一次,请找出所有路径。

输入

  • x,y,表示马的初始位置。

输出

  • 将每一格都走一次的路径总数,如果不存在该路径则输出“No solution!”。

样例输入 Copy

  1. 1 1
  2. 2 2

样例输出 Copy

  1. 32
  2. No solution!

链接:马的遍历问题

代码

  1. import java.util.Scanner;
  2. public class Main {
  3. static int over[][] = new int[105][105];//这个位置是否走过
  4. static int count;//记录有几条路径
  5. public static void solve(int x,int y,int step){
  6. if(step==20){
  7. count++;
  8. return;
  9. }
  10. //方向引导数组
  11. int dx[] = { 1,2, 2, 1,-1,-2,-2,-1};
  12. int dy[] = { 2,1,-1,-2,-2,-1, 1, 2};
  13. int DX,DY;
  14. for(int i=0;i<=7;i++){
  15. DX = x+dx[i];
  16. DY = y+dy[i];
  17. if(cut(DX,DY)){
  18. over[x][y] = step;
  19. solve(DX,DY,step+1);
  20. over[x][y] = 0;
  21. }
  22. }
  23. }
  24. //剪枝函数
  25. public static boolean cut(int x,int y){
  26. //超出棋盘的位置或者被走过的位置
  27. if(x>=1 && x<=5 && y>=1 && y<=4 && over[x][y]==0)
  28. return true;
  29. else
  30. return false;
  31. }
  32. public static void main(String[] args) {
  33. // TODO Auto-generated method stub
  34. Scanner sc = new Scanner(System.in);
  35. while(sc.hasNext()){
  36. int x = sc.nextInt();
  37. int y = sc.nextInt();
  38. for(int i=1;i<5;i++)
  39. for(int j=1;j<5;j++)
  40. over[i][j] = 0;
  41. count = 0;
  42. solve(x,y,1);
  43. if(count==0)
  44. System.out.println("No solution!");
  45. else
  46. System.out.println(count);
  47. }
  48. }
  49. }

【下周开始考试了,要赶快准备英语口语课毛概了,六级感觉可能过不了了,哭唧唧~啊,好难啊,还要搞课设】
句子君:

“曾经发生过的事情不可能忘记,只不过是想不起而已。
–宫崎骏 《千与千寻》”

发表评论

表情:
评论列表 (有 0 条评论,300人围观)

还没有评论,来说两句吧...

相关阅读

    相关 四周

    [第四周课程总结&试验报告(二)][Link 1]   一.写一个名为Rectangle的类表示矩形。其属性包括宽width、高height和颜色color,width和h