Divide un número grande en dos partes que difieren en k

Dado un gran número positivo N . La tarea es dividir N en dos números ‘A’ y ‘B’ de manera que la diferencia entre ellos sea K (1 <= K <= 10 100 ), es decir, A – B = K. 

Ejemplos: 

Input : N = 10, K = 2
Output : A = 6 B = 4

Input : N = 20, K = 4
Output : A = 12 B = 8

Deje que los dos números requeridos sean ‘A’ y ‘B’. Entonces, sabemos que la suma de ‘A’ y ‘B’ terminará en N. 
Entonces, una ecuación se convirtió en 
A + B = N 
Y también, queremos que la diferencia entre ‘A’ y ‘B’ sea igual a ‘K’. 
Entonces, otra ecuación se convierte en 
A – B = K 
Al sumar ambas ecuaciones, obtenemos 
2*A = N + K
Entonces, A = (N + K)/2 
Entonces podemos encontrar B por, B = (N – A )
Ahora, para manejar el Big Integer, tenemos que almacenar Integers en una array de caracteres y definir una función para realizar la operación en ellos.

A continuación se muestra la implementación en C de este enfoque:  

C++

// C++ program to Divide a Big
// Number into two parts
#include <bits/stdc++.h>
using namespace std;
#define MAX 100
 
// Function to adds two Numbers
// represented as array of character.
void add(char v1[], char v2[])
{
    int i, d, c = 0;
 
    // length of string
    int l1 = strlen(v1);
    int l2 = strlen(v2);
 
    // initializing extra character
    // position to 0
    for (i = l1; i < l2; i++)
        v1[i] = '0';
 
    for (i = l2; i < l1; i++)
        v2[i] = '0';
 
    // Adding each element of character
    // and storing the carry.
    for (i = 0; i < l1 || i < l2; i++) {
        d = (v1[i] - '0') + (v2[i] - '0') + c;
        c = d / 10;
        d %= 10;
        v1[i] = '0' + d;
    }
 
    // If remainder remains.
    while (c) {
        v1[i] = '0' + (c % 10);
        c /= 10;
        i++;
    }
 
    v1[i] = '\0';
    v2[l2] = '\0';
}
 
// Function to subtracts two numbers
// represented by string.
void subs(char v1[], char v2[])
{
    int i, d, c = 0;
 
    // Finding the length of the string.
    int l1 = strlen(v1);
    int l2 = strlen(v2);
 
    // initializing extra character position to 0.
    for (i = l2; i < l1; i++)
        v2[i] = '0';
 
    // Subtracting each element of character.
    for (i = 0; i < l1; i++) {
        d = (v1[i] - '0' - c) - (v2[i] - '0');
 
        if (d < 0) {
            d += 10;
            c = 1;
        }
        else
            c = 0;
 
        v1[i] = '0' + d;
    }
 
    v2[l2] = '\0';
    i = l1 - 1;
 
    while (i > 0 && v1[i] == '0')
        i--;
 
    v1[i + 1] = '\0';
}
 
// Function divides a number represented by
// character array a constant.
int divi(char v[], int q)
{
    int i, l = strlen(v);
 
    int c = 0, d;
 
    // Dividing each character element by constant.
    for (i = l - 1; i >= 0; i--) {
        d = c * 10 + (v[i] - '0');
        c = d % q;
        d /= q;
        v[i] = '0' + d;
    }
 
    i = l - 1;
 
    while (i > 0 && v[i] == '0')
        i--;
 
    v[i + 1] = '\0';
    return c;
}
 
// Function to reverses the character array.
void rev(char v[])
{
    int l = strlen(v);
    int i;
    char cc;
 
    // Reversing the array.
    for (i = 0; i < l - 1 - i; i++) {
        cc = v[i];
        v[i] = v[l - 1 - i];
        v[l - i - 1] = cc;
    }
}
 
// Wrapper Function
void divideWithDiffK(char a[], char k[])
{
 
    // Reversing the character array.
    rev(a);
    rev(k);
 
    // Adding the each element of both array
    // and storing the sum in array a[].
    add(a, k);
 
    // Dividing the array a[] by 2.
    divi(a, 2);
 
    // Reversing the character array to get output.
    rev(a);
    cout <<" "<< a;
 
    // Subtracting each element of array
    // i.e calculating a = a - b
    rev(a);
    subs(a, k);
 
    // Reversing the character array to get output.
    rev(a);
    cout <<" "<< a;
}
 
// Driven Program
int main()
{
    char a[MAX] = "100", k[MAX] = "20";
    divideWithDiffK(a, k);
    return 0;
}
// this code is contributed by shivanisinghss2110

C

// C program to Divide a Big
// Number into two parts
#include <stdio.h>
#include <string.h>
#define MAX 100
 
// Function to adds two Numbers
// represented as array of character.
void add(char v1[], char v2[])
{
    int i, d, c = 0;
 
    // length of string
    int l1 = strlen(v1);
    int l2 = strlen(v2);
 
    // initializing extra character
    // position to 0
    for (i = l1; i < l2; i++)
        v1[i] = '0';
 
    for (i = l2; i < l1; i++)
        v2[i] = '0';
 
    // Adding each element of character
    // and storing the carry.
    for (i = 0; i < l1 || i < l2; i++) {
        d = (v1[i] - '0') + (v2[i] - '0') + c;
        c = d / 10;
        d %= 10;
        v1[i] = '0' + d;
    }
 
    // If remainder remains.
    while (c) {
        v1[i] = '0' + (c % 10);
        c /= 10;
        i++;
    }
 
    v1[i] = '\0';
    v2[l2] = '\0';
}
 
// Function to subtracts two numbers
// represented by string.
void subs(char v1[], char v2[])
{
    int i, d, c = 0;
 
    // Finding the length of the string.
    int l1 = strlen(v1);
    int l2 = strlen(v2);
 
    // initializing extra character position to 0.
    for (i = l2; i < l1; i++)
        v2[i] = '0';
 
    // Subtracting each element of character.
    for (i = 0; i < l1; i++) {
        d = (v1[i] - '0' - c) - (v2[i] - '0');
 
        if (d < 0) {
            d += 10;
            c = 1;
        }
        else
            c = 0;
 
        v1[i] = '0' + d;
    }
 
    v2[l2] = '\0';
    i = l1 - 1;
 
    while (i > 0 && v1[i] == '0')
        i--;
 
    v1[i + 1] = '\0';
}
 
// Function divides a number represented by
// character array a constant.
int divi(char v[], int q)
{
    int i, l = strlen(v);
 
    int c = 0, d;
 
    // Dividing each character element by constant.
    for (i = l - 1; i >= 0; i--) {
        d = c * 10 + (v[i] - '0');
        c = d % q;
        d /= q;
        v[i] = '0' + d;
    }
 
    i = l - 1;
 
    while (i > 0 && v[i] == '0')
        i--;
 
    v[i + 1] = '\0';
    return c;
}
 
// Function to reverses the character array.
void rev(char v[])
{
    int l = strlen(v);
    int i;
    char cc;
 
    // Reversing the array.
    for (i = 0; i < l - 1 - i; i++) {
        cc = v[i];
        v[i] = v[l - 1 - i];
        v[l - i - 1] = cc;
    }
}
 
// Wrapper Function
void divideWithDiffK(char a[], char k[])
{
 
    // Reversing the character array.
    rev(a);
    rev(k);
 
    // Adding the each element of both array
    // and storing the sum in array a[].
    add(a, k);
 
    // Dividing the array a[] by 2.
    divi(a, 2);
 
    // Reversing the character array to get output.
    rev(a);
    printf("%s ", a);
 
    // Subtracting each element of array
    // i.e calculating a = a - b
    rev(a);
    subs(a, k);
 
    // Reversing the character array to get output.
    rev(a);
    printf("%s", a);
}
 
// Driven Program
int main()
{
    char a[MAX] = "100", k[MAX] = "20";
    divideWithDiffK(a, k);
    return 0;
}

Javascript

// JavaScript program to Divide a Big
// Number into two parts
let MAX = 100;
 
// Function to adds two Numbers
// represented as array of character.
function add(v1, v2)
{
    let i, d, c = 0;
 
    // length of string
    let l1 = v1.length;
    let l2 = v2.length;
 
    // initializing extra character
    // position to 0
    for (i = l1; i < l2; i++)
        v1[i] = '0';
 
    for (i = l2; i < l1; i++)
        v2[i] = '0';
 
    // Adding each element of character
    // and storing the carry.
    for (i = 0; i < l1 || i < l2; i++) {
        d = parseInt(v1[i] ) + parseInt(v2[i] ) + c;
        c = Math.floor(d / 10);
        d %= 10;
        v1[i] = d.toString();
    }
 
    // If remainder remains.
    while (c) {
        v1[i] = (c % 10).toString();
        c = Math.floor(c / 10);
        i++;
    }
 
    return v1;
}
 
// Function to subtracts two numbers
// represented by string.
function subs(v1, v2)
{
    let i, d, c = 0;
 
    // Finding the length of the string.
    let l1 = v1.length;
    let l2 = v2.length;
 
    // initializing extra character position to 0.
    for (i = l2; i < l1; i++)
        v2[i] = '0';
 
    // Subtracting each element of character.
    for (i = 0; i < l1; i++) {
        d = parseInt(v1[i]) - c - parseInt(v2[i]);
 
        if (d < 0) {
            d += 10;
            c = 1;
        }
        else
            c = 0;
 
        v1[i] = d.toString();
    }
 
    i = l1 - 1;
 
    while (i > 0 && v1[i] == '0')
        i--;
 
    return v1;
}
 
// Function divides a number represented by
// character array a constant.
function divi(v, q)
{
    let i, l = v.length;
 
    let c = 0, d;
 
    // Dividing each character element by constant.
    for (i = l - 1; i >= 0; i--) {
        d = c * 10 + parseInt(v[i]);
        c = d % q;
        d = Math.floor(d / q);
        v[i] = d.toString();
    }
 
    i = l - 1;
 
    while (i > 0 && v[i] == '0')
        i--;
         
    return v;
    // return c;
}
 
// Function to reverses the character array.
function rev(v)
{
    let l = v.length;
    let i;
    let cc;
 
    // Reversing the array.
    for (i = 0; i < l - 1 - i; i++) {
        cc = v[i];
        v[i] = v[l - 1 - i];
        v[l - i - 1] = cc;
    }
    return v;
}
 
// Wrapper Function
function divideWithDiffK(a, k)
{
    // Reversing the character array.
    a = rev(a);
    k = rev(k);
 
    // Adding the each element of both array
    // and storing the sum in array a[].
    a = add(a, k);
 
    // Dividing the array a[] by 2.
    a = divi(a, 2);
 
    // Reversing the character array to get output.
    a = rev(a);
    process.stdout.write(" " + parseInt(a.join("")));
 
    // Subtracting each element of array
    // i.e calculating a = a - b
    a = rev(a);
    a  = subs(a, k);
 
    // Reversing the character array to get output.
    a = rev(a);
    process.stdout.write(" " + parseInt(a.join("")));
}
 
// Driven Program
let a = "100".split("");
let k = "20".split("");
 
divideWithDiffK(a, k);
 
// This code is contributed by phasing17
Producción: 

60 40

 

Publicación traducida automáticamente

Artículo escrito por anuj0503 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *