Професионална програма
Loading...
Joro_Paspalev avatar Joro_Paspalev 20 Точки

Задача 5. C# Messages от More Exercise Introduction

Здравейте, може ли малко помощ на тази задача :

1.Messages

Write a program, which emulates typing an SMS, following this guide:

1

2

abc

3

def

4

ghi

5

jkl

6

mno

7

pqrs

8

tuv

9

wxyz

 

0

space

 

Following the guide, 2 becomes “a”, 22 becomes “b” and so on.

Examples

Input

Output

 

Input

Output

 

Input

Output

5

44

33

555

555

666

hello

9

44

33

999

0

8

44

33

777

33

hey there

7

6

33

33

8

0

6

33

meet me

Hints

  • A native approach would be to just put all the possible combinations of digits in a giant switch statement.
  • A cleverer approach would be to come up with a mathematical formula, which converts a number to its alphabet representation:

Digit

2

3

4

5

6

7

8

9

  • Index
  • 0 1 2
  • 3 4 5
  • 6 7 8
  • 9 11 12
  • 13 14 15
  • 16 17 18 19
  • 20 21 22
  • 23 24 25 26
  • Letter
  • 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
  • Let’s take the number 222 (c) for example. Our algorithm would look like this:
    • Find the number of digits the number has “e.g. 222 è 3 digits
    • Find the main digit of the number “e.g.  222 è 2
    • Find the offset of the number. To do that, you can use the formula: (main digit - 2) * 3
    • If the main digit is 8 or 9, we need to add 1 to the offset, since the digits 7 and 9 have 4 letters each
    • Finally, find the letter index (a è 0, c è 2, etc.). To do that, we can use the following formula: (offset + digit length - 1).
    • After we’ve found the letter index, we can just add that to the ASCII code of the lowercase letter “a” (97)

И трите нулеви тестове минават, но след това се чупи някъде. Моля някой за малко съвет? 

Направил съм я по стандартния начин с Switch case конструкция, като входните данни от конзолата първо се четяха като целочислени тип Integer, сега ги направих да са String, но отново същия резултат в Judge 20/100 точки.

Ето и кода: https://pastebin.com/tQtUbLey - Вариант със string

https://pastebin.com/jd1gR5nG - Вариант с Integer

 

0
Module: C# Advanced
IvaYorgova avatar IvaYorgova 3 Точки

Ето и от мен едно решение, дава 100/100.

 

using System;

namespace Messages2
{
    class Program
    {
        static void Main(string[] args)
        {
            int count = int.Parse(Console.ReadLine());
            string word = string.Empty;

            for (int i = 1; i <= count; i++)
            {
                string number = Console.ReadLine();
                int mainDigit = int.Parse(number[0].ToString());

                if (mainDigit == 2)
                {
                    if (number.Length == 1)
                    {
                        word += (char)97;
                    }
                    else if (number.Length == 2)
                    {
                        word += (char)98;
                    }
                    else if (number.Length == 3)
                    {
                        word += (char)99;
                    }
                }
                else if (mainDigit == 3)
                {
                    if (number.Length == 1)
                    {
                        word += (char)100;
                    }
                    else if (number.Length == 2)
                    {
                        word += (char)101;
                    }
                    else if (number.Length == 3)
                    {
                        word += (char)102;
                    }
                }
                else if (mainDigit == 4)
                {
                    if (number.Length == 1)
                    {
                        word += (char)103;
                    }
                    else if (number.Length == 2)
                    {
                        word += (char)104;
                    }
                    else if (number.Length == 3)
                    {
                        word += (char)105;
                    }
                }
                else if (mainDigit == 5)
                {
                    if (number.Length == 1)
                    {
                        word += (char)106;
                    }
                    else if (number.Length == 2)
                    {
                        word += (char)107;
                    }
                    else if (number.Length == 3)
                    {
                        word += (char)108;
                    }
                }
                else if (mainDigit == 6)
                {
                    if (number.Length == 1)
                    {
                        word += (char)109;
                    }
                    else if (number.Length == 2)
                    {
                        word += (char)110;
                    }
                    else if (number.Length == 3)
                    {
                        word += (char)111;
                    }
                }
                else if (mainDigit == 7)
                {
                    if (number.Length == 1)
                    {
                        word += (char)112;
                    }
                    else if (number.Length == 2)
                    {
                        word += (char)113;
                    }
                    else if (number.Length == 3)
                    {
                        word += (char)114;
                    }
                    else if (number.Length == 4)
                    {
                        word += (char)115;
                    }
                }
                else if (mainDigit == 8)
                {
                    if (number.Length == 1)
                    {
                        word += (char)116;
                    }
                    else if (number.Length == 2)
                    {
                        word += (char)117;
                    }
                    else if (number.Length == 3)
                    {
                        word += (char)118;
                    }
                }
                else if (mainDigit == 9)
                {
                    if (number.Length == 1)
                    {
                        word += (char)119;
                    }
                    else if (number.Length == 2)
                    {
                        word += (char)120;
                    }
                    else if (number.Length == 3)
                    {
                        word += (char)121;
                    }
                    else if (number.Length == 4)
                    {
                        word += (char)122;
                    }
                }
                else if (mainDigit == 0)
                {
                    word += " ";
                }
            }
            Console.WriteLine(word);
        }
    }
}

 

0
Joro_Paspalev avatar Joro_Paspalev 20 Точки

Много добро решение за упражняване на вложени конструкции.

Браво!

0