zoukankan      html  css  js  c++  java
  • 期末考试算法复习2

    题目如下:

    1、台阶问题

    2、未名湖租鞋问题

    3、购票问题

    4、Sine之舞

    5、s01串

    6、猴子分苹果

    7、汉诺塔问题

    8、跳马问题

    1、台阶问题

    n阶台阶,上楼时可以一步一个台阶也可以一步两个台阶,编程计算共有多少种不同的走法。

     1 public class 台阶问题 {
     2     
     3     public static int f(int n) {
     4         if(n==1 || n==2) {
     5             return n;
     6         } else {
     7             return f(n-1) + f(n-2);
     8         }
     9     }
    10     
    11     public static void main(String[] args) {
    12         
    13         System.out.println(f(1));
    14         System.out.println(f(2));
    15         System.out.println(f(3));
    16         System.out.println(f(4));
    17         
    18     }
    19     
    20 }

    2、未名湖租鞋问题

    每年冬天,北大未名湖上都是滑冰的好地方。北大体育组准备了许多冰鞋,可是人太多了,每天下午收工后,常常一双冰鞋都不剩。

    每天早上,租鞋窗口都会排起长龙,假设有还鞋的m个,有需要租鞋的n个。现在的问题是,这些人有多少种排法,可以避免出现体育组没有冰鞋可租的尴尬场面。(两个同样需求的人(比如都是租鞋或都是还鞋)交换位置是同一种排法)

    输入格式

      两个整数,表示m和n

    输出格式

      一个整数,表示队伍的排法的方案数。

    样例输入

    3 2

    样例输出

    5

     1 public class 未名湖租鞋问题 {
     2 
     3     public static int f(int m, int n) {
     4         // m: 还鞋 n: 租鞋
     5         if (m < n || m == 0) {
     6             return 0;
     7         } else if (n == 0) {
     8             return 1;
     9         } else {
    10             return f(m-1, n) + f(m, n-1);
    11         }
    12     }
    13 
    14     public static void main(String[] args) {
    15 
    16         System.out.println(f(3, 2));
    17 
    18     }
    19 
    20 }

    3、购票问题

    有2n个人排队购一件价为0.5元的商品,每人限购一件,其中一半人拿一张1元人民币,另一半人拿一张0.5元的人民币,要使售货员在售货中,不发生找钱困难,

    问这2n个人应该如何排队?找出所有排队的方案。(假设售货员一开始就没有准备零钱)

    输入格式

    整数n

    输出格式

    手持0.5和1元的依次次序

    样例输入

    2

    样例输出

    0.5   0.5   1   1

    0.5   1    0.5  1

    4、Sine之舞

    问题描述

      最近FJ为他的奶牛们开设了数学分析课,FJ知道若要学好这门课,必须有一个好的三角函数基本功。所以他准备和奶牛们做一个“Sine之舞”的游戏,寓教于乐,提高奶牛们的计算能力。
      不妨设An=sin(1–sin(2+sin(3–sin(4+...sin(n))...)
      Sn=(...(A1+n)A2+n-1)A3+...+2)An+1
      FJ想让奶牛们计算Sn的值,请你帮助FJ打印出Sn的完整表达式,以方便奶牛们做题。

    输入格式

      仅有一个数:N<201。

    输出格式

      请输出相应的表达式Sn,以一个换行符结束。输出中不得含有多余的空格或换行、回车符。

    样例输入

    3

    样例输出

    ((sin(1)+3)sin(1–sin(2))+2)sin(1–sin(2+sin(3)))+1

    #include <stdio.h>
    
    void printA(int n, int k)
    {
        if (n == k)
            printf("sin(%d)", n);
        else
        {
            printf("sin(%d", n);
            printf(n % 2 == 0 ? "+" : "-");
            printA(n + 1, k);
            printf(")");
        }
    }
    
    void printS(int n, int k)
    {
        if (n == 1)
        {
            printA(1, n);
            printf("+%d", k - n);
        }
        else
        {
            printf("(");
            printS(n - 1, k);
            printf(")");
            printA(1, n);
            printf("+%d", k - n);
        }
    }
    
    int main()
    {
        int N;
        scanf("%d", &N);
        printS(N, N + 1);
        printf("
    ");
        return 0;
    }

    5、s01串

    问题描述

      s01串初始为"0"
      按以下方式变换
      0变1,1变01

    输入格式

      1个整数(0~19)

    输出格式

      n次变换后s01串

    样例输入

    3

    样例输出

    101

    数据规模和约定

      0~19

     1 public class s01串 {
     2     public static int n;
     3     public static String s = "0";
     4     
     5     public static void f() {
     6         String temp = "";
     7         char[] chs = s.toCharArray();
     8         for(int i=0; i<chs.length; i++) {
     9             if(chs[i]=='0') {
    10                 temp+="1";
    11             }
    12             if(chs[i]=='1') {
    13                 temp+="01";
    14             }
    15         }
    16         
    17         s = temp;
    18     }
    19     
    20     public static void main(String[] args) {
    21         
    22         System.out.println("Input: ");
    23         Scanner input = new Scanner(System.in);
    24         n = input.nextInt();
    25         
    26         for(int i=0; i<n; i++) {
    27             f();
    28         }
    29         System.out.println(s);
    30         
    31     }
    32     
    33 }

    6、猴子分苹果

    秋天到了,n只猴子采摘了一大堆苹果放到山洞里,约定第二天平分。这些猴子很崇拜猴王孙悟空,所以都想给他留一些苹果。

    第一只猴子悄悄来到山洞,把苹果平均分成n份,把剩下的m个苹果吃了,然后藏起来一份,最后把剩下的苹果重新合在一起。

    这些猴子依次悄悄来到山洞,都做同样的操作,恰好每次都剩下了m个苹果。第二天,这些猴子来到山洞,把剩下的苹果分成n分,巧了,还是剩下了m个。问,原来这些猴子至少采了多少个苹果。

    输入格式

            两个整数,n m

    输出格式

            一个整数,表示原来苹果的数目

    样例输入

    5 1

    样例输出

    15621

     1 import java.util.Scanner;
     2 
     3 public class 猴子分苹果 {
     4 
     5     public static void main(String[] args) {
     6         System.out.println();
     7         Scanner input = new Scanner(System.in);
     8         int n = input.nextInt();
     9         int m = input.nextInt();
    10         System.out.println(Math.pow(n, n + 1) - (n - 1)*m);
    11     }
    12 
    13 }

    7、汉诺塔问题

     1 #include <stdio.h>
     2 
     3 void hanoi (int n,char a,char b,char c) 
     4  {
     5     if(n>0)          /*0阶的汉诺塔问题当作停止条件*/
     6     {
     7      hanoi(n-1,a,c,b);
     8      printf("移动%d从%c盘 TO %c盘
    ",n,a,b);
     9      hanoi(n-1,c,b,a);
    10     } 
    11  }
    12 
    13 int main()
    14 {
    15     int n;
    16     printf("请输入圆盘的数目n(小于8):
    ");
    17     scanf("%d",&n); 
    18     hanoi(n,'A','B','C');
    19     return 0;
    20 }

    8、跳马问题

    在n×m棋盘上有一中国象棋中的马:

      1. 马走日字;
      2. 马只能往右走。

        请你找出一条可行路径,使得马可以从棋盘的左下角(1,1)走到右上角(n,m)。

    输入:9 5

    输出:(1,1)->(3,2)->(5,1)->(6,3)->(7,1)->(8,3)->(9,5)

     1 #include<iostream>
     2 #include<cstdlib>
     3 using namespace std;
     4 
     5 
     6 int i=0;
     7 int x,y,n;
     8 int *px=new int[64];
     9 int *py=new int[64];
    10 
    11     
    12 void print(int i){
    13     int j;
    14     for(j=0;j<i;j++)
    15     {
    16     cout<<"("<<px[j]<<","<<py[j]<<")->";
    17     }
    18         
    19 }
    20     
    21 int find(int x,int y,int n) {  
    22     if(x>n||y>n||x<1||y<1) {
    23           return 0; 
    24     } else  
    25         return 1; 
    26     } 
    27     
    28 void place(int x ,int y ,int n,int i)
    29 {
    30     int f=1;
    31     if(x==n&&y==n)
    32     {
    33          print(i);
    34          cout<<"("<<x<<","<<y<<")"<<endl;
    35          f=0;
    36     }
    37     px[i]=x;
    38     py[i]=y;
    39 
    40     if(find(x,y,n)==1&& f!=0)
    41     {
    42          place(x+2 ,y+1,n,i+1);
    43         place(x+2 ,y-1,n,i+1);
    44         place(x+1 ,y+2,n,i+1);
    45          place(x+1 ,y-2,n,i+1 );
    46     }
    47 }
    48 
    49 int main()
    50 {
    51 
    52     place(1,1,8,0);
    53 
    54     return 0;
    55 }
    56     
  • 相关阅读:
    Oracle Core 学习笔记二 Transactions 和 Consistency 说明
    Oracle AUTO_SPACE_ADVISOR_JOB 说明
    Windows 下 ftp 上传文件 脚本
    Oracle 11g 中 Direct path reads 特性 说明
    Linux 使用 wget 下载 Oracle 软件说明
    Oracle 10g read by other session 等待 说明
    Oracle 11g RAC INS06006 Passwordless SSH connectivity not set up between the following node(s) 解决方法
    SecureCRT 工具 上传下载数据 与 ASCII、Xmodem、Ymodem 、Zmodem 说明
    Oracle RAC root.sh 报错 Timed out waiting for the CRS stack to start 解决方法
    Oracle RESETLOGS 和 NORESETLOGS 区别说明
  • 原文地址:https://www.cnblogs.com/wyb666/p/11032405.html
Copyright © 2011-2022 走看看