Timus 1149. Sinus Dances

Timus 1149. Sinus Dances 要求输出特定格式的字符串。


1149. Sinus Dances

Time Limit: 1.0 second
Memory Limit: 16 MB
Let  An = sin(1–sin(2+sin(3–sin(4+…sin( n))…)
Let  Sn = (…( A 1+ n) A 2+ n–1) A 3+…+2) An+1
For given  N print  SN

Input

One integer  N. 1 ≤  N ≤ 200

Output

Line containing  SN

Sample

input output
3
((sin(1)+3)sin(1–sin(2))+2)sin(1–sin(2+sin(3)))+1
Problem Author: Vladimir Gladkov 
Problem Source: Ural Collegiate Programming Contest, April 2001, Perm, Test Round

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;
  }
}

这道题目和解答都非常简单,就不解释了。


返回目录

你可能感兴趣的:(in)