Sine之舞

就是写两个递归调用函数。只要注意递归的出口和递归公式就好了

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		String s = "";
		// s = a(s, n, 1);
		String result = s(s, n, 1);
		System.out.println(result);

	}

	public static String a(String s, int n, int i) {//函数an
		if (n == 0) {
			s = "";
			return s;
		}
		if (i == n) {
			s += "sin(" + n;
			for (int k = 0; k < n; k++) {
				s += ")";
			}
			return s;
		} else {
			if (i % 2 == 0) {
				s += "sin(" + i + "+";
			} else {
				s += "sin(" + i + "-";
			}

			i++;
			s = a(s, n, i);
			return s;
		}

	}

	public static String s(String str, int n, int i) {//函数sn
		
		if (n == 0) {
			return str;
		}
		
		if(1 < n){
			str = "("+a(str,i, 1)+"+"+n+")";
			i++;
			n--;
			str = s(str, n, i);
			return str;
		}else{//针对n=1的情况
			str =  a(str, i, 1)+"+"+n;
			i++;
			n--;
			return str;
		}
		

	}

}


你可能感兴趣的:(蓝桥杯)