Proszę o pomoc w kodzie

0

Witam, zaczynam dopiero uczyć się programowania i robię zadanie z pewnej strony. Link do zadania https://pl.spoj.com/problems/PA05_POT/ .
To jest mój kod, zauważyłem, że np. dla liczby 111112^3 podaj mi złą cyfrę jedności i nie wiem dlaczego.

#include <iostream>

using namespace std;

int i=0,a,p,w,d;


int jednosci(int p, int w){
    if(p==0)
        return 0;
    else if(w==0)
        return 1;
    else if(p==1)
        return 1;
    else if(p==2)
        if(w%5==0)
            return 2;
        else if(w%4==0)
            return 6;
        else if(w%3==0)
            return 8;
        else if(w%2==0)
            return 4;
        else if(w%1==0)
            return 2;
    else if(p==3)
        if(w%5==0)
            return 3;
        else if(w%4==0)
            return 1;
        else if(w%3==0)
            return 7;
        else if(w%2==0)
            return 9;
        else if(w%1==0)
            return 3;
    else if(p==4)
        if(w%3==0)
            return 4;
        else if(w%2==0)
            return 6;
        else if(w%1==0)
            return 4;
    else if(p==5)
        return 5;
    else if(p==6)
        return 6;
    else if(p==7)
        if(w%5==0)
            return 7;
        else if(w%4==0)
            return 1;
        else if(w%3==0)
            return 3;
        else if(w%2==0)
            return 9;
        else if(w%1==0)
            return 7;
    else if(p==8)
        if(w%5==0)
            return 8;
        else if(w%4==0)
            return 6;
        else if(w%3==0)
            return 2;
        else if(w%2==0)
            return 4;
        else if(w%1==0)
            return 8;
    else if(p==9)
        if(w%3==0)
            return 9;
        else if(w%2==0)
            return 1;
        else if(w%1==0)
            return 9;
}

int main()
{
    cin>>d;
    for( i=0; i<d; i++ ){
        cin>>p>>w;
        a==p%10;
        cout<<jednosci(a,w);
    }
    return 0;
}

Z góry dziękuję za pomoc.

0

Nie Lubisz klamer? To Pisz w Haskellu:-D. Powstawiałem, ale co z tym dalej, nie wiem gdzie może być jeszcze błąd, dla 111112^3, przynajmniej liczy dobrze:)

#include <iostream>

using namespace std;

int i=0,a,p,w,d;

int jednosci(int p, int w){
	if (w == 1) return p;
	if (p == 0) return 0;
     if(p==1){
        return 1;
      }
    else if(p==2) {
        if(w%5==0)
            return 2;
        else if(w%4==0)
            return 6;
        else if(w%3==0)
            return 8;
        else if(w%2==0)
            return 4;
        else if(w%1==0)
            return 2;
    }
    else if(p==3) {
        if(w%5==0)
            return 3;
        else if(w%4==0)
            return 1;
        else if(w%3==0)
            return 7;
        else if(w%2==0)
            return 9;
        else if(w%1==0)
            return 3;
    }
    else if(p==4) {
        if(w%3==0)
            return 4;
        else if(w%2==0)
            return 6;
        else if(w%1==0)
            return 4;
     }
    
    else if(p==5) {
        return 5;
     }
    else if(p==6) {
        return 6;
     }
    else if(p==7) {
        if(w%5==0)
            return 7;
        else if(w%4==0)
            return 1;
        else if(w%3==0)
            return 3;
        else if(w%2==0)
            return 9;
        else if(w%1==0)
            return 7;
     }
    else if(p==8) {
         if(w%5==0)
            return 8;
        else if(w%4==0)
            return 6;
        else if(w%3==0)
            return 2;
        else if(w%2==0)
            return 4;
        else if(w%1==0)
            return 8;
     }
    else if(p==9) {
        if(w%3==0)
            return 9;
        else if(w%2==0)
            return 1;
        else if(w%1==0)
            return 9;
      }
}

int main()
{
    cin>>d;
    for( i=0; i<d; i++ ){
        cin>>p>>w;
		p=p%10;
        cout<<jednosci(p,w);
    }
    return 0;
}

0

Za dużo tu tych ifów. Rozpisz sobie nieco przypadków po kolei i zauważ jakąś regularność. Podpowiem, że mi funkcja liczenia wyniku zajęła 5 linijek kodu w tym jeden if :)

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