Zadanie ze SPOJ - pomoc w skróceniu kodu

0

Hej,
prosiłbym o pomoc w kwestii skrócenia mojego kodu.
Program dotyczy zadania na SPOJ (http://pl.spoj.com/problems/JZAPKAB/). Moja wersja została zaakceptowana, jednak nie wydaje mi się aby bazowanie na kilku(nastu) instrukcjach warunkowych było optymalne.

 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SPOJ
{
    class Program
    {
        static void Main(string[] args)
        {
            short value;
            short get = 0;
            string txt = Console.ReadLine();
            for (byte b = 0; b < txt.Length; b++)
            {
                if (txt[b].Equals('a'))
                {
                    value = 1;
                    get += value;
                }
                else if (txt[b].Equals('b'))
                {
                    value = 2;
                    get += value;
                }
                else if (txt[b].Equals('c'))
                {
                    value = 3;
                    get += value;
                }
                else if (txt[b].Equals('d'))
                {
                    value = 4;
                    get += value;
                }
                else if (txt[b].Equals('e'))
                {
                    value = 5;
                    get += value;
                }
                else if (txt[b].Equals('f'))
                {
                    value = 6;
                    get += value;
                }
                else if (txt[b].Equals('g'))
                {
                    value = 7;
                    get += value;
                }
                else if (txt[b].Equals('h'))
                {
                    value = 8;
                    get += value;
                }
                else if (txt[b].Equals('i'))
                {
                    value = 9;
                    get += value;
                }
                else if (txt[b].Equals('k'))
                {
                    value = 10;
                    get += value;
                }
                else if (txt[b].Equals('l'))
                {
                    value = 20;
                    get += value;
                }
                else if (txt[b].Equals('m'))
                {
                    value = 30;
                    get += value;
                }
                else if (txt[b].Equals('n'))
                {
                    value = 40;
                    get += value;
                }
                else if (txt[b].Equals('o'))
                {
                    value = 50;
                    get += value;
                }
                else if (txt[b].Equals('p'))
                {
                    value = 60;
                    get += value;
                }
                else if (txt[b].Equals('q'))
                {
                    value = 70;
                    get += value;
                }
                else if (txt[b].Equals('r'))
                {
                    value = 80;
                    get += value;
                }
                else if (txt[b].Equals('s'))
                {
                    value = 90;
                    get += value;
                }
                else if (txt[b].Equals('t'))
                {
                    value = 100;
                    get += value;
                }
                else if (txt[b].Equals('v'))
                {
                    value = 200;
                    get += value;
                }
                else if (txt[b].Equals('x'))
                {
                    value = 300;
                    get += value;
                }
                else if (txt[b].Equals('y'))
                {
                    value = 400;
                    get += value;
                }
                else if (txt[b].Equals('z'))
                {
                    value = 500;
                    get += value;
                }
            }
            Console.Write(get);
        }
    }
}

Dzięki za pomoc ;)

0

Jak dla mnie to niepotrzebnie używasz zmiennej value na sztywno możesz dać

else if (txt[b].Equals('z')){
 get += 500;
}

i zamiast tylu if & else, użyłbym switch()
lepiej by to wyglądało xD ale to już kwestia gustu

dodanie znacznika <code class="csharp"> - fp

0

http://stackoverflow.com/questions/1598070/c-sharp-equivalent-of-c-mapstring-double

Możesz spróbować korzystać z czegoś takiego. tzn z value zrobić takie dictionary, i potem wywoływać get+= value[txt[b]]. Ale trochę to na siłę może wyglądać.

0

Ale kombinujecie ;) Przeciez wystarczy 1 tablica wartosci i skorzystanie z wlasnosci ascii

#include <iostream>

using namespace std;

int main() {

    int values[26];

    for(int i=0;i<26;i++) {
        if(i < 10) {
            values[i] = i+1;
            values[i+10] = (i+1)*10;
            i < 3 ? values[i+23] = (i+3)*100 : 0;
        }
    }

    values[22] = 200;

    string word;
    cin >> word;

    int result = 0;
    for(int i=0;i<word.length();i++) {
        result += values[toupper(word[i])-65];
    }

    cout << result << '\n';

    return 0;
}
0
using System;
 
namespace SPOJ
  {
   class Test
     {
      static void Main(string[] args)
        {
         int[] pow={1,10,100};
         int sum=0;
         string txt=Console.ReadLine();
         for(int i=0;i<txt.Length;++i)
           {
            int value=txt[i]-'a';
            if(value>=23) --value;
            if(value>=21) --value;
            if(value>=10) --value;
            sum+=(1+value%9)*pow[value/9];
           }
         Console.Write(sum);
        }
     }
  }

Lub wręcz tak:

using System;
 
namespace SPOJ
  {
   class Test
     {
      static void Main(string[] args)
        {
         int[] tb={ 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 0, 200, 0, 300, 400, 500 };
         int sum=0;
         string txt=Console.ReadLine();
         for(int i=0;i<txt.Length;++i) sum+=tb[txt[i]-'a'];
         Console.Write(sum);
        }
     }
  }
0
using System;
 
namespace SPOJ
{
    class Program
    {
        private static readonly ushort [] x = {1,2,3,4,5,6,7,8,9,10,20,30,40,50,60,70,80,90,100,200,300,400,500};
        static void Main(string[] args)
        {
            string tab = Console.ReadLine();
            int i, s = 0;
            foreach(char c in tab)
		        s = s + x[c>119 ? c-100 : c<106 ? c-97 : c==118 ? 19 : c-98];
            Console.Write(s);
        }
    }
}
0

Program nie może obsługiwać liter W,J,U etc

1 użytkowników online, w tym zalogowanych: 0, gości: 1