📜  检查给定的数字是否为花式

📅  最后修改于: 2021-04-29 01:00:33             🧑  作者: Mango

奇特的数字是当旋转180度时相同的数字。给定一个数字,找出它是否花哨。

6、9、1、0和8的180度旋转分别是9、6、1、0和8

例子:

Input:  num =  96
Output: Yes
If we rotate given number by 180, we get same number

Input:  num =  916
Output: Yes
If we rotate given number by 180, we get same number

Input:  num =  996
Output: No

Input:  num =  121
Output: No

强烈建议您最小化浏览器,然后自己尝试。

这个想法是创建一个存储花式对映射的映射。创建地图后,从两端遍历给定的数字,并且如果当前端的字符在任何时候都不是花哨的对,则返回false。该算法类似于回文检查算法。

以下是上述想法的实现。

C++
// C++ program to find if a given number is fancy or not.
#include
using namespace std;
  
bool isFancy(string& num)
{
    // To store mappings of fancy pair characters. For example
    // 6 is paired with 9 and 9 is paired with 6.
    map fp;
    fp['0'] = '0';
    fp['1'] = '1';
    fp['6'] = '9';
    fp['8'] = '8';
    fp['9'] = '6';
  
    // Find number of digits in given number
    int n = num.length();
  
    // Traverse from both ends, and compare characters one by one
    int l = 0, r = n-1;
    while (l<=r)
    {
        // If current characters at both ends are not fancy pairs
        if (fp.find(num[l]) == fp.end() || fp[num[l]] != num[r])
            return false;
        l++;
        r--;
    }
    return true;
}
  
// Driver program
int main()
{
    string str = "9088806";
    isFancy(str)? cout << "Yes": cout << "No";
    return 0;
}


Java
// Java program to find if a given number 
// is fancy or not
import java.util.*;
  
class GFG 
{
static boolean isFancy(String num) 
{ 
    // To store mappings of fancy pair characters. 
    // For example 6 is paired with 9 and 9 is paired with 6. 
    Map fp = new HashMap(); 
                                      
    fp. put('0', '0'); 
    fp. put('1', '1'); 
    fp. put('6', '9'); 
    fp. put('8', '8'); 
    fp. put('9', '6'); 
  
    // Find number of digits in given number 
    int n = num.length(); 
  
    // Traverse from both ends, 
    // and compare characters one by one 
    int l = 0, r = n-1; 
    while (l <= r) 
    { 
        // If current characters at both ends 
        // are not fancy pairs 
        if (!fp.containsKey(num.charAt(l)) || 
             fp.get(num.charAt(l)) != num.charAt(r)) 
            return false; 
        l++; 
        r--; 
    } 
    return true; 
} 
  
// Driver Code
public static void main(String[] args) 
{
    String str = "9088806"; 
    if(isFancy(str))
        System.out.println("Yes");
    else
        System.out.println("No");
}
} 
  
// This code is contributed by PrinciRaj1992


C#
// C# program to find if a given number 
// is fancy or not
using System;
using System.Collections.Generic;
  
class GFG 
{
static bool isFancy(String num) 
{ 
    // To store mappings of fancy pair characters. 
    // For example 6 is paired with 9 and 9 is paired with 6. 
    Dictionary fp = new Dictionary();
                                      
    fp.Add('0', '0'); 
    fp.Add('1', '1'); 
    fp.Add('6', '9'); 
    fp.Add('8', '8'); 
    fp.Add('9', '6'); 
  
    // Find number of digits in given number 
    int n = num.Length; 
  
    // Traverse from both ends, 
    // and compare characters one by one 
    int l = 0, r = n - 1; 
    while (l <= r) 
    { 
        // If current characters at both ends 
        // are not fancy pairs 
        if (!fp.ContainsKey(num[l]) || 
             fp[num[l]] != num[r]) 
            return false; 
        l++; 
        r--; 
    } 
    return true; 
} 
  
// Driver Code
public static void Main(String[] args) 
{
    String str = "9088806"; 
    if(isFancy(str))
        Console.WriteLine("Yes");
    else
        Console.WriteLine("No");
}
} 
  
// This code is contributed by Rajput-Ji


输出:

Yes

感谢Gaurav Ahirwar提出上述解决方案。