解题思路:
先通过递归求An,再通过递归求Sn
注意事项:
设一个全局变量
参考代码:
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Scanner;
public class Main {
static int n;
static int i = n;
static Scanner sc = new Scanner(new InputStreamReader(System.in));
static OutputStreamWriter out = new OutputStreamWriter(System.out);
public static void main(String[] args) throws Exception {
Main.n = sc.nextInt();
Main.i = Main.n;
out.write(dfs(n));
out.flush();
}
public static String bfs(int n){ //求An
if(n==1) return "sin(1";
if((n&1)==0) return bfs(n-1)+"-sin("+n;
else return bfs(n-1)+"+sin("+n;
}
public static String dfs(int n){ //求Sn
if(n==1){
String s = bfs(1)+")";
return s+"+"+i;
}
String s = bfs(n);
String s1 = bfs(n);
while(s.contains("(")){
s = s.substring(s.indexOf("(")+1, s.length());
s1 += ")";
}
return "("+dfs(n-1)+")"+s1+"+"+ --Main.i;
}
}
0.0分
1 人评分