>  기사  >  백엔드 개발  >  C/C++로 모듈러 방정식을 푸는 프로그램을 작성하시나요?

C/C++로 모듈러 방정식을 푸는 프로그램을 작성하시나요?

WBOY
WBOY앞으로
2023-09-12 14:21:031179검색

C/C++로 모듈러 방정식을 푸는 프로그램을 작성하시나요?

여기서 모듈 방정식과 관련된 흥미로운 문제를 볼 수 있습니다. A와 B라는 두 개의 값이 있다고 가정해 보겠습니다. (A mod X) = B가 성립하도록 변수 X가 취할 수 있는 가능한 값의 수를 찾아야 합니다.

A는 26이고 B는 2라고 가정합니다. 따라서 X의 선호 값은 {3, 4, 6, 8, 12, 24}이므로 개수는 6입니다. 이것이 답입니다. 더 잘 이해하기 위해 알고리즘을 살펴보겠습니다.

Algorithm

possibleWayCount(a, b) −

begin
   if a = b, then there are infinite solutions
   if a < b, then there are no solutions
   otherwise div_count := find_div(a, b)
   return div_count
end

find_div(a, b) -

begin
   n := a &ndash; b
   div_count := 0
   for i in range 1 to square root of n, do
      if n mode i is 0, then
         if i > b, then
            increase div_count by 1
         end if
         if n / i is not same as i and (n / i) > b, then
            increase div_count by 1
         end if
      end if
   done
end

Example

의 중국어 번역은 다음과 같습니다.

Example

#include <iostream>
#include <cmath>
using namespace std;
int findDivisors(int A, int B) {
   int N = (A - B);
   int div_count = 0;
   for (int i = 1; i <= sqrt(N); i++) {
      if ((N % i) == 0) {
         if (i > B)
            div_count++;
         if ((N / i) != i && (N / i) > B) //ignore if it is already counted
            div_count++;
      }
   }
   return div_count;
}
int possibleWayCount(int A, int B) {
   if (A == B) //if they are same, there are infinity solutions
      return -1;
   if (A < B) //if A < B, then there are two possible solutions
      return 0;
   int div_count = 0;
   div_count = findDivisors(A, B);
   return div_count;
}
void possibleWay(int A, int B) {
   int sol = possibleWayCount(A, B);
   if (sol == -1)
      cout << "For A: " << A << " and B: " << B << ", X can take infinite values greater than " << A;
   else
      cout << "For A: " << A << " and B: " << B << ", X can take " << sol << " values";
}
int main() {
   int A = 26, B = 2;
   possibleWay(A, B);
}

Output

For A: 26 and B: 2, X can take 6 values

위 내용은 C/C++로 모듈러 방정식을 푸는 프로그램을 작성하시나요?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 tutorialspoint.com에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제