📜  完美方形弦

📅  最后修改于: 2021-05-07 10:04:15             🧑  作者: Mango

给定String str ,任务是检查所有字符的ASCII值之和是否为完美的平方。

例子 :

Input : ddddddddddddddddddddddddd
Output : Yes

Input : GeeksForGeeks
Output : No

算法

  • 计算字符串长度
  • 计算所有字符的ASCII值之和
  • 取数字和的平方根并将其存储到变量squareRoot中
  • 取squareRoot的底值并从squareRoot中减去
  • 如果squareRoot和squareRoot的底值之差为0,则打印“是”,否则打印“否”

下面是上述方法的实现:

C++
// C++ program to find if string is a
// perfect square or not.
#include 
using namespace std;
 
bool isPerfectSquareString(string str)
{
    int sum = 0;
     
    // calculating the length of
    // the string
    int len = str.length();
     
    // calculating the ASCII value
    // of the string
    for (int i = 0; i < len; i++)
        sum += (int)str[i];
     
    // Find floating point value of
    // square root of x.
    long double squareRoot = sqrt(sum);
     
    // If square root is an integer
    return ((squareRoot -
             floor(squareRoot)) == 0);
}
 
// Driver code
int main()
{
    string str = "d";
     
    if (isPerfectSquareString(str))
        cout << "Yes";
    else
        cout << "No";
}


Java
// Java program to find if string
// is a perfect square or not.
import java.io.*;
 
class GFG {
     
    static boolean isPerfectSquareString(String str)
    {
        int sum = 0;
         
        // calculating the length
        // of the string
        int len = str.length();
         
        // calculating the ASCII
        // value of the string
        for (int i = 0; i < len; i++)
            sum += (int)str.charAt(i);
         
        // Find floating point value
        // of square root of x.
        long squareRoot = (long)Math.sqrt(sum);
         
        // If square root is an integer
        return ((squareRoot -
                Math.floor(squareRoot)) == 0);
    }
 
    // Driver code
    public static void main (String[] args)
    {
        String str = "d";
     
        if (isPerfectSquareString(str))
            System.out.println("Yes");
        else
            System.out.println("No");
    }
}
 
// This code is contributed by Ajit.


Python3
# Python3 program to find
# if string is a perfect
# square or not.
import math;
def isPerfectSquareString(str):
    sum = 0;
     
    # calculating the length
    # of the string
    l = len(str);
     
    # calculating the ASCII
    # value of the string
    for i in range(l):
        sum = sum + ord(str[i]);
     
    # Find floating point value
    # of square root of x.
    squareRoot = math.sqrt(sum);
     
    # If square root is an integer
    return ((squareRoot -
             math.floor(squareRoot)) == 0);
 
# Driver code
str = "d";
 
if (isPerfectSquareString(str)):
    print("Yes");
else:
    print("No");
 
# This code is contributed by mits


C#
// C# program to find if string
// is a perfect square or not.
using System;
 
class GFG
{
    static bool isPerfectSquareString(string str)
    {
        int sum = 0;
         
        // calculating the length
        // of the string
        int len = str.Length;
         
        // calculating the ASCII
        // value of the string
        for (int i = 0; i < len; i++)
            sum += (int)str[i];
         
        // Find floating point value
        // of square root of x.
        double squareRoot = Math.Sqrt(sum);
        double F = Math.Floor(squareRoot);
 
        // If square root is an integer
        return ((squareRoot - F) == 0);
    }
 
    // Driver Code
    public static void Main()
    {
        string str = "d";
     
        if (isPerfectSquareString(str))
            Console.WriteLine("Yes");
        else
            Console.WriteLine("No");
    }
}
 
// This code is contributed by Sam007


PHP


Javascript


输出 :
Yes