Home >Backend Development >C++ >Translate the following into Chinese: C++ Divide a number into two divisible parts
In this question we are given a string that can be interpreted as a number. Now we have to divide this string into two parts such that the first part is divisible by A and the second part is divisible by B (given our two integers). For example -
Input : str = "123", a = 12, b = 3 Output : YES 12 3 "12" is divisible by a and "3" is divisible by b. Input : str = "1200", a = 4, b = 3 Output : YES 12 00 Input : str = "125", a = 12, b = 3 Output : NO
Now, in this problem, we will do some pre-computation to make our program faster and then it will be able to work under higher constraints.
In this method we will run two loops in the string, first loop from start to end and second loop from end to start. Now, at each point, we take modulo the integer formed by an in the first loop and b in the second loop, and then we can find the answer.
#include <bits/stdc++.h> using namespace std; void divisionOfString(string &str, int a, int b){ int n = str.length(); vector<int> mod_a(n+1, 0); // mod_a[0] = (str[0] - '0')%a; for (int i=1; i<n; i++) // front loop for calculating the mod of integer with a mod_a[i] = ((mod_a[i-1]*10)%a + (str[i]-'0'))%a; vector<int> mod_b(n+1, 0); mod_b[n-1] = (str[n-1] - '0')%b; int power10 = 10; // as we have assigned answer to last index for (int i= n-2; i>=0; i--){ // end loop for calculating the mod of integer with b mod_b[i] = (mod_b[i+1] + (str[i]-'0')*power10)%b; power10 = (power10 * 10) % b; } for (int i=0; i<n-1; i++){ // finding the division point if (mod_a[i] != 0) // we can skip through all the positions where mod_a is not zero continue; if (mod_b[i+1] == 0){ // now if the next index of mod_b is also zero so that is our division point cout << "YES\n"; /*******Printing the partitions formed**********/ for (int k=0; k<=i; k++) cout << str[k]; cout << " "; for (int k=i+1; k < n; k++) cout << str[k]; return; } } cout << "NO\n"; // else we print NO } // Driver code int main(){ string str = "123"; // given string int a = 12, b = 3; divisionOfString(str, a, b); return 0; }
YES 12 3
In this method, we now calculate the remainder of the number formed by each division. Our first number should be divisible by a, so we run a forward loop and store the mod of that number with a. For b, we run a backward loop and now store the mod because we know that if the mod of an at any position is zero, and the mod of b at the next index is zero, this will be our answer, so we print it .
In this tutorial we solved a problem of dividing a number into two divisible parts. We also learned the C program for this problem and the complete method (general) to solve it. We can write the same program in other languages such as C, java, python and other languages. We hope you found this tutorial helpful.
The above is the detailed content of Translate the following into Chinese: C++ Divide a number into two divisible parts. For more information, please follow other related articles on the PHP Chinese website!