一些练习。

  1. 设计一个函数,使用以下无穷级数计算sinx的值

import java.util.Scanner;

public class Test03 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        double d1 = sc.nextDouble();
        double d = Math.asin(d1);
        System.out.println(d);
        double i = Math.sin(d);
        double j = mySin(d,1e-6);
        System.out.println(i);
        System.out.println(j);
    }
    public static double mySin(double x,double limit){
        double result = 0.0;
        double i1 = 1;
        double i2 = 1.0;
        boolean flag = false;
        for(double i = 1;i<Integer.MAX_VALUE;i++){
            i1 *=i;
            if((Math.pow(x,(double) i)/i1 > limit) && (i%2 != 0)){
                if(!flag) {
                    i2 = 1.0;
                    flag = true;
                }else {
                    i2 = -1.0;
                    flag = false;
                }

                result+=Math.pow(x,(double) i)/i1*i2;

            }
            if(Math.pow(x,(double) i)/i1 < limit){
                break;
            }
        }
        return result;
    }
}
  1. 写一个函数stringCopy将一个字符串复制到另一个字符串。

public class Test04 {
    public static void main(String[] args) {
        //1.    写一个函数stringCopy将一个字符串复制到另一个字符串。
        Scanner sc = new Scanner(System.in);
        String str = sc.next();
        String str1 = stringCopy(str);
        System.out.println(str1);
    }

    private static String stringCopy(String str) {
        String str1 = "";
        char[] c = str.toCharArray();
        for (char c1 : c) {
            str1 =  str1+c1 ;
        }
        return str1;
    }
}
  1. 设计一个支持整型、实型和字符型的气泡排序的函数模板

import java.util.Arrays;

public class Test05 {
    public static void main(String[] args) {
        //1.	设计一个支持整型、实型和字符型的气泡排序的函数模板
        int[] a = {4,5,6,8,7,9,2,1,3};
        String[] b = {"abc","acc","aaa","bbb"};
        //Arrays.sort(b);
        Student[] c = {new Student("zhangsan",80,"shuxue"),new Student("lisi",75,"yuwen"),new Student("wangwu",90,"yingyu")};
        maoPaoSort(a);
        System.out.println(Arrays.toString(a));
        System.out.println(Arrays.toString(b));
        maoPaoSort(b);
        System.out.println(Arrays.toString(b));
        maoPaoSort(c);
    }

    public static void maoPaoSort(int[] in){
        int k = 0;
        for(int i = 0;i<in.length;i++){
            for(int j = 0;j<in.length-1-k;j++){
                if(in[j+1]<in[j]){
                    int temp = in[j+1];
                    in[j+1] = in[j];
                    in[j] = temp;
                }
            }
            k++;
        }
    }
    public static void maoPaoSort(String[] str){
        int k= 0;
        for(int d = 0;d<str.length;d++) {
            for (int i = 0; i < str.length-k-1; i++) {
                char[] c = str[i].toCharArray();
                char[] c1 = str[i + 1].toCharArray();
                for (int j = 0; j < c.length && j < c1.length; j++) {
                    if (c[j] > c1[j]) {
                        String temp = str[i + 1];
                        str[i + 1] = str[i];
                        str[i] = temp;
                        break;
                    } else if (c[j] == c1[j]) {
                        continue;
                    } else {
                        break;
                    }
                }
                if(c.length > c1.length){
                    String temp = str[i + 1];
                    str[i + 1] = str[i];
                    str[i] = temp;
                }
            }
            k++;
        }
    }
    public static void maoPaoSort(Student[] stu){
        //根据某个值排序,和int的冒泡是一样的。
    }


}
  1. 设计一函数求两个正整数的最大公约数。

import java.util.Scanner;

public class Test06 {
    public static void main(String[] args) {
        //1.	设计一函数求两个正整数的最大公约数。
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        System.out.println(theMaxGongYueShu(a, b));
    }

    private static int theMaxGongYueShu(int a, int b) {
        int c = 0;
        for(int i = 1;i<=Math.max(a,b);i++){
            if (a % i == 0 && b % i == 0 ){
                c = Math.max(i,c);
            }
        }
        return c;
    }
}
  1. 设计一个用于整型数的二分查找的递归函数。

import java.util.Arrays;
import java.util.Scanner;

public class Test07 {
    public static void main(String[] args) {
        //1.	设计一个用于整型数的二分查找的递归函数。
        int [] a ={5,4,8,7,3,6,2,1,9};
        Scanner sc = new Scanner(System.in);
        int target = sc.nextInt();
        Arrays.sort(a);
        System.out.println(ercheck(target, a,0,a.length-1));
    }

    private static int ercheck(int target, int[] a,int left,int right) {
//使用二分查找前要先排序
        if(left<=right) {
            int mid = (left + right) / 2;
            if (a[mid] == target) return mid;
            if (a[mid] < target) return ercheck(target, a, mid + 1, right);
            if (a[mid] > target) return ercheck(target, a, left, mid - 1);
        }
        return -1;
    }
}
  1. 设计一个支持整型、实型和字符型数据的快速排序的函数模板。

import java.lang.reflect.AnnotatedArrayType;
import java.util.Arrays;

public class Qsort {
    public static void main(String[] args) {
        int[] a ={1,5,7,8,9,4,6,3,2,5};
        myQsort(a,0,a.length-1);
        System.out.println(Arrays.toString(a));
    }

    private static void myQsort(int[] a,int left,int right) {
          if(left > right) return;
          int i = left;
          int j = right;
          int prior = a[i];
          while(i<j) {
              while (i < j && a[j] >= prior) {
                  j--;
              }
              while (i < j && a[i] <= prior) {
                  i++;
              }
              if (i < j) {
                  int temp = a[i];
                  a[i] = a[j];
                  a[j] = temp;
              }
          }
          a[left] = a[i];
          a[i] = prior;
          myQsort(a, left, j - 1);
          myQsort(a, j + 1, right);

    }
}
  1. 编写一函数int count(),使得第一次调用时返回1,第二次调用时返回2。即返回当前的调用次数。

public class Test08 {
    static int i = 0;
    public static void main(String[] args) {
        //1.	编写一函数int count(),使得第一次调用时返回1,第二次调用时返回2。即返回当前的调用次数。
        int n = 10;
        while (n-- != 0){
            System.out.print(count());
        }
    }

    private static int count() {
       return ++i;
    }
}

构建二叉树,给出了前序和中序,要得到后序遍历序列。

1.首先要通过前序和后序去构建一棵树,再用递归去遍历这棵树得到相应的后序序列。

来自leetcode官方解释,然后自己重写了一遍。

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Test06{
    static  Map<Integer,Integer> map;
    public static void main(String[] args){
        int[] preorder = {1,2,3,4,5,6,7};
        int[] inorder = {3,2,4,1,6,5,7};
        TreeNode node = buildTree(preorder,inorder);
        houxu(node);
    }

    private static void houxu(TreeNode node) {
        if(node != null){
            houxu(node.leftNode);
            houxu(node.rightNode);
            System.out.print(node.val+"\t");

        }
    }

    public static TreeNode buildTree(int[] preorder, int[] inorder) {
        int n = preorder.length;
        // 构造哈希映射,帮助我们快速定位根节点
        map = new HashMap<>(n);
        for(int i = 0;i<n;i++){
            map.put(inorder[i],i);
        }
        return myBuildTree(preorder,0,n-1,inorder,0,n-1);
    }

    private static TreeNode myBuildTree(int[] preorder, int preorderLeft, int preorderRight, int[] inorder, int inorderLeft, int inorderRight) {
        if(preorderLeft > preorderRight) return null;
        // 前序遍历中的第一个节点就是根节点
        int preNodeRoot = preorderLeft;
        // 在中序遍历中定位根节点
        int inNodeRoot =  map.get(preorder[preNodeRoot]);
        // 先把根节点建立出来
        TreeNode root = new TreeNode(preorder[preNodeRoot]);
        // 得到左子树中的节点数目
        int size_left_subtree = inNodeRoot-inorderLeft;
        // 递归地构造左子树,并连接到根节点
        // 先序遍历中「从 左边界+1 开始的 size_left_subtree」个元素就对应了中序遍历中「从 左边界 开始到 根节点定位-1」的元素
        root.leftNode = myBuildTree(preorder,preorderLeft + 1, preorderLeft + size_left_subtree, inorder,inorderLeft, inNodeRoot-1);
        // 递归地构造右子树,并连接到根节点
        // 先序遍历中「从 左边界+1+左子树节点数目 开始到 右边界」的元素就对应了中序遍历中「从 根节点定位+1 到 右边界」的元素
        root.rightNode = myBuildTree(preorder, preorderLeft + size_left_subtree + 1, preorderRight, inorder, inNodeRoot + 1, inorderRight);
        return root;
    }
}
class TreeNode {
    int val;
    TreeNode leftNode;
    TreeNode rightNode;

    public TreeNode() {
    }

    ;

    public TreeNode(int val) {
        this.val = val;
    }

    public TreeNode(int val, TreeNode leftNode, TreeNode rightNode) {
        this.val = val;
        this.rightNode = rightNode;
        this.leftNode = leftNode;
    }
}

  • 9
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值