Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > C/C++: Для новичков > облегчение кода простейшей программы


Автор: jurgal 15.12.2007, 21:24
вот код программки которая строит пирамиду: 

Код

int main()
{
    int h;
    cout << "Input high of pyramid: ";
    cin >> h;
    cout << endl;
    int b = 1;
    for(int i=1;i<=h;i++){
        for(int c=h-i;c>0;c--)
            cout << ' ';
        for(int s = b;s>0;s--)
            cout << 'X';
        b+=2;
        cout << '\n';
    }
    return 0;
}

подскажите пожалуйста как можно обойтись двумя циклами =) , а не тремя

Автор: archimed7592 15.12.2007, 21:35
Код

    for(int i=1;i<=h;i++){
        for (int j = 0; j < h - i + b; ++j)
            cout << (j < h - i ? ' ' : 'X');
/*        for(int c=h-i;c>0;c--)
            cout << ' ';
        for(int s = b;s>0;s--)
            cout << 'X';*/
        b+=2;
        cout << '\n';
    }

Автор: jurgal 16.12.2007, 02:36
archimed7592, 
спасибо =) 

вот ещё одну написал, но думаю и её можно упростить: 

Код

int main()
{
    int pound1,shilling1, pence1,pound2,shilling2,pence2;
    char q;
    do
    {
        
        cout << "Input first amount:";
        cin >> pound1 >> q >> shilling1 >> q >> pence1 ;
        cout << "Input second amount:";
        cin >> pound2 >> q >> shilling2 >> q >> pence2; 
        pound1 +=pound2;
        shilling1 += shilling2;
        pence1 +=  pence2;

        if(pence1 > 11)
        {
            if(pence1%12 == 0) {
                shilling1 = (pence1/12) + shilling1;
                pence1 = pence1 - (pence1/12)*12;
            }
            else 
            {
                shilling1 = pence1%12 + shilling1;
                pence1 = pence1 - (pence1%12)*12;
            }
        }
        if(shilling1 > 19)
        {
            pound1 = shilling1%20 + pound1;
            shilling1 = shilling1 - (shilling1%20)*20;
        }
        cout << endl << "Total: " << pound1 << '.' << shilling1 << '.' << pence1 << endl;
        cout << "again?" << endl;
        cin >> q;
    }while(q!='n');
    return 0;
}

сложение двух сумм состоящих из  фунтов, шиллингов и пенсов. 

Автор: bsa 17.12.2007, 15:14
jurgal, напиши две функции для конвертации фунтов, шиллингов, пенсов в пенсы и обратно. Затем будешь пользоваться простым сложением.

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)