📜  检查数字是否平衡

📅  最后修改于: 2021-04-22 08:49:24             🧑  作者: Mango

给定字符串形式的数字N ,任务是检查给定数字是否平衡

例子:

方法:
从一开始就迭代数字长度的一半以上。通过将s [i]s [数字位数– 1 – i]分别加到leftSumrightSum上,可以同时计算上半部分和下半部分的数字总和。最后,检查leftSumrightSum是否相等。

下面是上述方法的实现。

C++
// C++ program to check
// if a number is
// Balanced or not
 
#include 
using namespace std;
 
// Function to check whether N is
// Balanced Number or not
void BalancedNumber(string s)
{
    int Leftsum = 0;
    int Rightsum = 0;
 
    // Calculating the Leftsum
    // and rightSum simultaneously
    for (int i = 0; i < s.size() / 2; i++) {
 
        // Typecasting each character
        // to integer and adding the
        // digit to respective sums
        Leftsum += int(s[i] - '0');
        Rightsum += int(s[s.size() - 1 - i]
                        - '0');
    }
 
    if (Leftsum == Rightsum)
        cout << "Balanced" << endl;
    else
        cout << "Not Balanced" << endl;
}
 
// Driver Code
int main()
{
    string s = "12321";
 
    // Function call
    BalancedNumber(s);
 
    return 0;
}


Java
// Java program to check if a number
// is Balanced or not
import java.io.*;
 
class GFG{
   
// Function to check whether N is
// Balanced Number or not
private static void BalancedNumber(String s)
{
    int Leftsum = 0;
    int Rightsum = 0;
     
    // Calculating the Leftsum
    // and rightSum simultaneously
    for(int i = 0; i < s.length() / 2; i++)
    {
         
        // Typecasting each character
        // to integer and adding the
        // digit to respective sums
        Leftsum += (int)(s.charAt(i) - '0');
        Rightsum += (int)(s.charAt(
            s.length() - 1 - i) - '0');
    }
     
    if (Leftsum == Rightsum)
        System.out.println("Balanced");
    else
        System.out.println("Not Balanced");
}
 
// Driver Code
public static void main (String[] args)
{
    String s = "12321";
     
    // Function call
    BalancedNumber(s);
}
}
 
// This code is contributed by jithin


Python3
# Python3 program to check
# if a number is
# Balanced or not
 
# Function to check whether N is
# Balanced Number or not
def BalancedNumber(s):
 
    Leftsum = 0
    Rightsum = 0
 
    # Calculating the Leftsum
    # and rightSum simultaneously
    for i in range(0, int(len(s) / 2)):
 
        # Typecasting each character
        # to integer and adding the
        # digit to respective sums
        Leftsum = Leftsum + int(s[i])
        Rightsum = (Rightsum +
                    int(s[len(s) - 1 - i]))
 
    if (Leftsum == Rightsum):
        print("Balanced", end = '\n')
    else:
        print("Not Balanced", end = '\n')
 
# Driver Code
s = "12321"
 
# Function call
BalancedNumber(s)
 
# This code is contributed by PratikBasu


C#
// C# program to check
// if a number is
// Balanced or not
using System;
class GFG{
     
// Function to check whether N is
// Balanced Number or not
static void BalancedNumber(string s)
{
  int Leftsum = 0;
  int Rightsum = 0;
 
  // Calculating the Leftsum
  // and rightSum simultaneously
  for (int i = 0; i < s.Length / 2; i++)
  {
    // Typecasting each character
    // to integer and adding the
    // digit to respective sums
    Leftsum += (int)(Char.GetNumericValue(s[i]) -
                     Char.GetNumericValue('0'));
    Rightsum += (int)(Char.GetNumericValue(s[s.Length -
                                             1 - i]) -
                      Char.GetNumericValue('0'));
  }
 
  if (Leftsum == Rightsum)
    Console.WriteLine("Balanced");
  else
    Console.WriteLine("Not Balanced");
} 
 
// Driver code
static void Main()
{
  string s = "12321";
 
  // Function call
  BalancedNumber(s);
}
}
 
// This code is contributed by divyeshrabadiya07


输出:
Balanced