Advertisement
dimov

Untitled

Jan 2nd, 2013
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.91 KB | None | 0 0
  1. /* Problem 2 – A-nacci
  2. The A-nacci sequence (read "ei nachi"), often called "ei-nachi, aman ot teya zadachi" (read it however you like) is a sequence similar to the Fibonacci sequence – each element is formed by the sum of the previous two, but with a little different rules for the elements.
  3. The elements in the A-nacci sequence are the capital letters from the English alphabet. Each letter has a code, determined by its position in the alphabet – A has the code 1, B has the code 2, …, Z has the code 26. Here are all the elements that can be in an A-nacci sequence, along with their codes:
  4. A   B   C   D   E   F   G   H   I   J   K   L   M   N   O   P   Q   R   S   T   U   V   W   X   Y   Z
  5. 1   2   3   4   5   6   7   8   9   10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26
  6. The first two elements in the sequence can be any two of the letters above. Every next element has a code equal to the sum of the codes of the previous two elements.
  7. For example, if A and B are the first two elements, the third element will be C (code(A) = 1, code(B) = 2, 1 + 2 = 3, code(C) = 3). Analogically, the fourth element's code will be determined by the sum of the codes of B and C, so the fourth element will be E.
  8. If the sum of two codes is larger than 26, then that sum is taken by its modulus by 26 (you know this as the % operator in C#). For example, if the sum is 27, then the code will be (27 by modulus 26) = 1, which is the code of A. Another example – if the first two elements are Y and D, then the sum of their codes is 25 + 4 = 29, which is larger than 26, so the code of the next element will be (29 by modulus 26) = 3, which is the code of C.
  9. The A-nacci figure consists of lines of sequential elements from an A-nacci sequence, printed out similarly to the letter A (but without the dash in the middle). The first line contains exactly one element – the first element of an A-nacci sequence. The second line contains the second and third elements of the sequence, concatenated (that is, not separated by anything). Each of the next lines contains exactly two elements – the next elements of the sequence, separated by a certain number of whitespaces. The number of whitespaces separating the elements on the third line, fourth line and so on, are as follows:
  10. • The third line has exactly one whitespace between the two elements
  11. • The fourth line has exactly two whitespaces between the two elements
  12. • …
  13. • The Nth line has exactly N-2 whitespaces between the two elements
  14. Write a program, which, by given the first two elements (letters) of the A-nacci sequence and the number of lines in the A-nacci form, prints an A-nacci form on the console.
  15. Input
  16. The input data should be read from the console.
  17. The first two lines will contain the values of the first two elements of the A-nacci sequence – each element will be a capital English letter on a separate line.
  18. On the third line of the input there will be the number L – the number of lines in the A-nacci form.
  19. The input data will always be valid and in the format described. There is no need to check it explicitly.
  20. Output
  21. The output data should be printed on the console.
  22. The output should contain exactly L lines. The first line should contain exactly one capital English letter. The second line (if L>1) should contain exactly two capital English letters. The third line should contain two capital English letters, separated by a single whitespace (" ") and so on. There shouldn't be any whitespaces after the second (i.e. last) letter on a line.
  23. Constraints
  24. • 1 ≤ L ≤ 42.
  25. • All elements of the A-nacci sequence are characters, which are capital letters from the English alphabet.
  26. • Allowed working time for your program: 0.1 seconds. Allowed memory: 16 MB.
  27.  */
  28. using System;
  29.  
  30. class Anacci
  31. {
  32.     static void Main()
  33.     {
  34.         int a = char.Parse(Console.ReadLine());
  35.         int b = char.Parse(Console.ReadLine());
  36.         int p = int.Parse(Console.ReadLine());
  37.         Console.WriteLine((char)a);
  38.  
  39.         if (a + b - 128 > 26)
  40.         {
  41.             a = (a + b - 128 - 26) + 64;
  42.         }
  43.         else
  44.         {
  45.             a = a + b - 64;
  46.         }
  47.  
  48.  
  49.  
  50.         if (p>1)
  51.         {
  52.             Console.WriteLine("{0}{1}", (char)b,(char)a);
  53.  
  54.             a = a ^ b;
  55.             b = a ^ b;
  56.             a = a ^ b;
  57.  
  58.             string alpha = null;
  59.             char check = ' ';
  60.  
  61.  
  62.             for (int i = 2; i < p; i++)
  63.             {
  64.                 if (a + b - 128 > 26)
  65.                 {
  66.                     a = (a + b - 128 - 26) + 64;
  67.                 }
  68.                 else
  69.                 {
  70.                     a = a + b - 64;
  71.                 }
  72.  
  73.                 if (a + b - 128 > 26)
  74.                 {
  75.                     b = (a + b - 128 - 26) + 64;
  76.                 }
  77.                 else
  78.                 {
  79.                     b = a + b - 64;
  80.                 }
  81.                 alpha += check;
  82.                 Console.WriteLine("{0}{1}{2}",(char)a,alpha,(char)b);
  83.  
  84.  
  85.  
  86.             }
  87.         }
  88.  
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement