Timus 1149. Sinus Dances 要求输出特定格式的字符串。
input | output |
---|---|
3 |
((sin(1)+3)sin(1–sin(2))+2)sin(1–sin(2+sin(3)))+1 |
Tags: problem for beginners
C # 语言解答如下:
using System; // http://acm.timus.ru/problem.aspx?space=1&num=1149 static class Timus { const int MAX = 200; static char[] msg = "sin()+-".ToCharArray(); static char[] A = new char[MAX * 10]; static char[] S = new char[MAX * 5 * MAX]; static void Main() { Console.WriteLine(S, 0, MakeS(int.Parse(Console.ReadLine()))); } static int MakeS(int n) { var count = 0; while (count < n - 1) S[count++] = msg[3]; // '(' for (int k, i = 1; i <= n; i++, count++) { var N = (n - i + 1).ToString().ToCharArray(); Array.Copy(A, 0, S, count, k = MakeA(i)); // Ai S[count += k] = msg[5]; // '+' Array.Copy(N, 0, S, ++count, N.Length); // n - i + 1 S[count += N.Length] = msg[4]; // ')' } return --count; // 去掉最后 ')' } static int MakeA(int n) { var count = 0; for (var i = 1; i <= n; i++, count++) { var N = i.ToString().ToCharArray(); Array.Copy(msg, 0, A, count, 4); // "sin(" Array.Copy(N, 0, A, count += 4, N.Length); // i A[count += N.Length] = msg[(i % 2 == 0) ? 5 : 6]; // '+' or '-' } count--; // 覆盖最后 '+' or '-' for (var i = 0; i < n; i++) A[count++] = msg[4]; // ')' return count; } }
这道题目和解答都非常简单,就不解释了。