Computer >> 컴퓨터 >  >> 프로그램 작성 >> C++

C++를 사용하여 N 계승의 합에 대한 단위 자릿수를 찾습니다.

<시간/>

여기서는 N 계승의 합에서 단위 자릿수를 구하는 방법을 살펴보겠습니다. 따라서 N이 3이면 합계를 얻은 후 1을 얻습니다! + 2! + 3! =9이면 결과가 되고 N =4이면 1이 됩니다! + 2! + 3! + 4! =33. 그래서 단위 자리는 3입니다. 이것을 분명히 보면 N> 5의 계승으로 단위 자리는 0이므로 5 이후에는 단위 자리를 변경하는 데 기여하지 않습니다. N =4 이상이면 3이 됩니다. 우리는 단위 장소에 대한 차트를 만들 수 있으며 프로그램에서 사용할 것입니다.

C++를 사용하여 N 계승의 합에 대한 단위 자릿수를 찾습니다.

#include<iostream>
#include<cmath>
using namespace std;
double getUnitPlace(int n) {
   int placeVal[5] = {-1, 1, 3, 9, 3};
   if(n > 4){
      n = 4;
   }
   return placeVal[n];
}
int main() {
   for(int i = 1; i<10; i++){
      cout << "Unit place value of sum of factorials when N = "<<i<<" is: " << getUnitPlace(i) << endl;
   }
}

출력

Unit place value of sum of factorials when N = 1 is: 1
Unit place value of sum of factorials when N = 2 is: 3
Unit place value of sum of factorials when N = 3 is: 9
Unit place value of sum of factorials when N = 4 is: 3
Unit place value of sum of factorials when N = 5 is: 3
Unit place value of sum of factorials when N = 6 is: 3
Unit place value of sum of factorials when N = 7 is: 3
Unit place value of sum of factorials when N = 8 is: 3
Unit place value of sum of factorials when N = 9 is: 3