📜  完美的立方体字符串

📅  最后修改于: 2021-09-08 12:44:09             🧑  作者: Mango

给定一个 String str ,任务是检查该字符串中所有字符的 ASCII 值之和是否是一个完美的立方体。

例子 :

Input: str = "ll"
Output: Yes
ASCII value of l = 108
Therefore, sum of ASCII values = 108 + 108 = 216
which is a perfect cube 6 (6 * 6 * 6 = 216)

Input: str = "a"
Output: No
ASCII value of a = 97
Therefore, sum of ASCII values = 97
which is not a perfect cube

算法

  • 对于字符中的每个字符,找出其 ASCII 值
  • 计算所有字符的 ASCII 值总和
  • 检查这个和是否是一个完美的立方体。
  • 如果和是一个完美的立方体,则打印“是”,否则打印“否”

下面是上述方法的实现:

C++
// C++ program to find if string is a
// perfect cube or not.
 
#include 
using namespace std;
 
bool isPerfectCubeString(string str)
{
    int sum = 0;
 
    // Finding ASCII values of each
    // character and finding its sum
    for (int i = 0; i < str.length(); i++)
        sum += (int)str[i];
 
    // Find the cube root of sum
    long double cr = round(cbrt(sum));
 
    // Check if sum is a perfect cube
    return (cr * cr * cr == sum);
}
 
// Driver code
int main()
{
    string str = "ll";
 
    if (isPerfectCubeString(str))
        cout << "Yes";
    else
        cout << "No";
}


Java
// Java program to find if String is a
// perfect cube or not.
import java.util.*;
 
class GFG{
  
static boolean isPerfectCubeString(String str)
{
    int sum = 0;
  
    // Finding ASCII values of each
    // character and finding its sum
    for (int i = 0; i < str.length(); i++)
        sum += (int)str.charAt(i);
  
    // Find the cube root of sum
    double cr = Math.round(Math.cbrt(sum));
  
    // Check if sum is a perfect cube
    return (cr * cr * cr == sum);
}
  
// Driver code
public static void main(String[] args)
{
    String str = "ll";
  
    if (isPerfectCubeString(str))
        System.out.print("Yes");
    else
        System.out.print("No");
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program to find if str1ing is a
# perfect cube or not.
from math import ceil
 
def isPerfectCubeString(str1):
    sum = 0
 
    # Finding ASCII values of each
    # character and finding its sum
    for i in range(len(str1)):
        sum += ord(str1[i])
 
    # Find the cube root of sum
    cr = ceil((sum)**(1/3))
 
    # Check if sum is a perfect cube
    return (cr * cr * cr == sum)
 
# Driver code
str1 = "ll"
 
if (isPerfectCubeString(str1)):
    print("Yes")
else:
    print("No")
 
# This code is contributed by mohit kumar 29


C#
// C# program to find if String is a
// perfect cube or not.
using System;
 
class GFG{
 
static bool isPerfectCubeString(String str)
{
    int sum = 0;
 
    // Finding ASCII values of each
    // character and finding its sum
    for (int i = 0; i < str.Length; i++)
        sum += (int)str[i];
 
    // Find the cube root of sum
    double cr = Math.Round(Math.Pow(sum, (double) 1 / 3));
     
    // Check if sum is a perfect cube
    return (cr * cr * cr == sum);
}
 
// Driver code
public static void Main(String[] args)
{
    String str = "ll";
 
    if (isPerfectCubeString(str))
        Console.Write("Yes");
    else
        Console.Write("No");
}
}
 
// This code is contributed by PrinciRaj1992


Javascript


输出:
Yes

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live