Admitere informatica

Rezolvarea problemelor de matematica prin realizarea unor programe.
MDCristi
utilizator
utilizator
Mesaje: 35
Membru din: 21 Oct 2012, 21:45

Mesaj de MDCristi » 24 Mai 2015, 22:24

Salut din nou! Am facut o incercare si la punctul b, de data asta am scris codul in engleza (comentarii si nume de functii, variable). Ce parere aveti?

Cod: Selectaţi tot

#include <iostream>
#include <string>
#include <cmath>

char invert&#40;char digit&#41;
&#123;
    if&#40;digit == '1'&#41;
        return '2';
    else
        return '1';
&#125;

char _getDigit&#40;int index, int k, std&#58;&#58;string base&#41;
&#123;
    // We have reached the base string &#40;1221&#41;
    if&#40;k == 1&#41;
    &#123;
        // -1 since string index starts at 0
        return base&#91;index - 1&#93;;
    &#125;

    const int subSequenceLength = pow&#40;4.0, k - 1&#41;;

    // how many subdivisions of the main sequence it fully covers
    int repetition = index / subSequenceLength ;
    int innerI; // this is n'

    if&#40;index % subSequenceLength == 0&#41;
    &#123;
        innerI = 4; // it is the last digit i the last fully coverd subsequence
    &#125;
    else
    &#123;
        innerI = index % subSequenceLength;
        // we increment it since we have one sequence that we haven't fully
        // coverd.
        repetition++;
    &#125;

    char result = _getDigit&#40;innerI, k - 1, base&#41;;
    if&#40;repetition == 2 || repetition == 3&#41;
        return invert&#40;result&#41;;
    return result;
&#125;

char getDigit&#40;int index, std&#58;&#58;string base&#41;
&#123;
    int k = 1;
    for&#40;int i = 4; i < index; k++, i *= 4&#41;
        ;

    return _getDigit&#40;index, k, base&#41;;
&#125;

int main&#40;&#41;
&#123;
    int digitIndex;
    std&#58;&#58;cout << "Insert n&#58; ";
    std&#58;&#58;cin >> digitIndex;
    std&#58;&#58;cout << std&#58;&#58;endl;

    std&#58;&#58;cout << "The digit is " << getDigit&#40;digitIndex, "1221"&#41; << std&#58;&#58;endl;

    return 0;
&#125;
[/code][/quote]

Scrie răspuns