按照题目要求的格式进行递归即可。
将Sn和An作为两个部分,分别进行递归。
参考代码:
public class Main { public static int N; public static String An(int n, int m) { if(n == m) return "sin(" + n + ")"; char fh; if(n % 2 == 0) { fh = '+'; } else { fh = '-'; } return "sin(" + n + fh + An(n + 1, m) + ")"; } public static String Sn(int n) { if(n == 1) return An(1, 1) + "+" + N; return "(" + Sn(n - 1) + ")" + An(1, n) + "+" + ((N + 1) - n); } public static void main(String[] args) { // TODO Auto-generated method stub Scanner in = new Scanner(System.in); N = in.nextInt(); System.out.println(Sn(N)); in.close(); } }
0.0分
1 人评分