首页 > 代码库 > URAL 1149 Sinus Dances

URAL 1149 Sinus Dances

看起来挺乱,写起来挺简单的递归

 1 import java.util.Scanner; 2  3 public class P1149 4 { 5  6     private static String getA(int n, int k) 7     { 8         if (k == n) 9             return String.format("sin(%d)", k);10         else11             return String.format("sin(%d%s%s)", k, k % 2 == 0 ? "+" : "-", getA(n, k + 1));12     }13 14     private static String getS(int n, int k)15     {16         if (k == 1)17             return String.format("%s+%d", getA(1, 1), n);18         return String.format("(%s)%s", getS(n, k - 1), String.format("%s+%d", getA(k, 1), n + 1 - k));19     }20 21     public static void main(String[] args)22     {23         try (Scanner cin = new Scanner(System.in))24         {25             while (cin.hasNext())26             {27                 int n = cin.nextInt();28                 System.out.println(getS(n, n));29             }30         }31     }32 }

 

URAL 1149 Sinus Dances