9. Determinati ultima cifra a lui.np, cu n si p citite de la tastatura
Obs. Pt. ca n si p pot fi foarte mari, NU puteti folosi powinn)!
Ex. n=1973 p=89 Raspuns 1 (ultima cifra a lui 197389)
Răspunsuri la întrebare
Răspuns:
#include <iostream>
using namespace std;
int main()
{
int n,p,exp;
cout<<"n="; cin>>n; n=n%10;
cout<<"p="; cin>>p;
switch (n)
{case 0: cout<<"0"; break;
case 1: cout<<"1"; break;
case 2:
{exp=p%4;
switch (exp)
{
case 0: cout<<"6";break;
case 1: cout<<"2";break;
case 2: cout<<"4";break;
case 3: cout<<"8";break;
}break;
}
case 3:{exp=p%4;
switch (exp)
{
case 0: cout<<"1";break;
case 1: cout<<"3";break;
case 2: cout<<"9";break;
case 3: cout<<"7";break;
}
break;
}
case 4: {exp=p%2;
switch (exp)
{
case 0: cout<<"6";break;
case 1: cout<<"4";break;
}
break;
}
case 5: cout<<"5"; break;
case 6: cout<<"6"; break;
case 7: {exp=p%4;
switch (exp)
{
case 0: cout<<"1";break;
case 1: cout<<"7";break;
case 2: cout<<"9";break;
case 3: cout<<"3";break;
}
break;
}
case 8: {exp=p%4;
switch (exp)
{
case 0: cout<<"6";break;
case 1: cout<<"8";break;
case 2: cout<<"4";break;
case 3: cout<<"2";break;
}
break;
}
case 9: {exp=p%2;
switch (exp)
{
case 0: cout<<"1";break;
case 1: cout<<"9";break;
}
break;
}
}
cout<<endl;
return 0;
}
Explicație:
programul afiseaza ultima cifra a lui n la puterea p, calculata folosind regulile de la matematica. In caz ca doreai altceva, scrie clar ce anume doreai. Eu asta am subinteles din acel pow...