📌  相关文章
📜  打破一个数字,使得第一部分是第二部分的整数除以 10 的幂

📅  最后修改于: 2022-05-13 01:57:07.169000             🧑  作者: Mango

打破一个数字,使得第一部分是第二部分的整数除以 10 的幂

给定一个非常大的数字 N,我们需要计算总的方式,如果我们将数字分成两部分ab ,第一部分a可以通过第二个b的整数除以 10 的某个幂 p 和 p> =0。 1 <= N 中的数字个数 <= 10^5  .例子:

Input : 220
Output : 1
220 can be divided as a = 2 and b = 20
such that for p = 1, b/10 = a.


Input : 1111
Output : 2
We get answer 2 because we need to consider
integral division.
Let's consider the first partition a = 1,
b = 111. for p = 2, b/pow(10,p) = a thus
this is a valid partition.
now a = 11, b = 11. for p = 0, b/pow(10,p) 
= a thus this too is a valid combination.

Input : 2202200
Output : 2
for a = 2 b = 202200, p = 5 and
a = 220, b = 2200, p = 1

由于即使在 long long int 中也可能包含非常大的数字,我们将其存储为字符串。根据问题中提到的条件,除法是地板函数。一种简单而低效的方法是将字符串分成两个子字符串,然后将它们转换为整数并执行除法。一种有效的方法是使用字符串比较函数来匹配两个字符串的最高有效数字并忽略其余的(地板函数)。下面是这个想法的实现:

C++
#include 
using namespace std;
 
// c++ function to count ways to divide a
// string in two parts a and b such that
// b/pow(10, p) == a
int calculate(string N)
{
    int len = N.length();
    int l = (len) / 2;
    int count = 0;
 
    for (int i = 1; i <= l; i++) {
 
        // substring representing int a
        string s = N.substr(0, i);
 
        // no of digits in a
        int l1 = s.length();
 
        // consider only most significant
        // l1 characters of remaining string
        // for int b
        string t = N.substr(i, l1);
 
        // if any of a or b contains leading 0s
        // discard this combination
        if (s[0] == '0' || t[0] == '0')
            continue;
 
        // if both are equal
        if (s.compare(t) == 0)
            count++;       
    }
    return count;
}
 
// driver function to test above function
int main()
{
    string N = "2202200";
    cout << calculate(N);
    return 0;
}


Java
// Java program to count ways to divide a
// String in two parts a and b such that
// b/pow(10, p) == a
import java.util.*;
 
class GFG
{
static int calculate(String N)
{
    int len = N.length();
    int l = (len) / 2;
    int count = 0;
 
    for (int i = 1; i <= l; i++)
    {
 
        // subString representing int a
        String s = N.substring(0, i);
 
        // no of digits in a
        int l1 = s.length();
 
        // consider only most significant
        // l1 characters of remaining String
        // for int b
        String t = N.substring(i, l1 + i);
 
        // if any of a or b contains leading 0s
        // discard this combination
        if (s.charAt(0) == '0' || t.charAt(0) == '0')
            continue;
 
        // if both are equal
        if (s.compareTo(t) == 0)
            count++;    
    }
    return count;
}
 
// Driver Code
public static void main(String[] args)
{
    String N = "2202200";
    System.out.print(calculate(N));
}
}
 
// This code is contributed by Rajput-Ji


Python3
# Python3 program to count ways to divide
# a string in two parts a and b such that
# b/pow(10, p) == a
 
def calculate( N ):
    length = len(N)
    l = int((length) / 2)
    count = 0
     
    for i in range(l + 1):
        print(i)
         
        # substring representing int a
        s = N[0: 0 + i]
         
        # no of digits in a
        l1 = len(s)
        print(s,l1)
         
        # consider only most significant
        # l1 characters of remaining
        # string for int b
        t = N[i: l1 + i]
         
        # if any of a or b contains
        # leading 0s discard this
        try:
            if s[0] == '0' or t[0] == '0':
                continue
        except:
            continue
         
        # if both are equal
        if s == t:
            count+=1
        print(i,N[i],count)
    return count
     
# driver code to test above function
N = str("2202200")
print(calculate(N))
 
# This code is contributed by "Sharad_Bhardwaj".


C#
// C# program to count ways to divide a
// String in two parts a and b such that
// b/pow(10, p) == a
using System;
 
class GFG
{
static int calculate(String N)
{
    int len = N.Length;
    int l = (len) / 2;
    int count = 0;
 
    for (int i = 1; i <= l; i++)
    {
 
        // subString representing int a
        String s = N.Substring(0, i);
 
        // no of digits in a
        int l1 = s.Length;
 
        // consider only most significant
        // l1 characters of remaining String
        // for int b
        String t = N.Substring(i, l1);
 
        // if any of a or b contains leading 0s
        // discard this combination
        if (s[0] == '0' || t[0] == '0')
            continue;
 
        // if both are equal
        if (s.CompareTo(t) == 0)
            count++;    
    }
    return count;
}
 
// Driver Code
public static void Main(String[] args)
{
    String N = "2202200";
    Console.Write(calculate(N));
}
}
 
// This code is contributed by PrinciRaj1992


Javascript


输出:

2