📜  骰子问题

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

骰子问题

给你一个有6 个面的立方骰子。所有个人的脸上都印有一个数字。数字在1 到 6的范围内,就像任何普通骰子一样。您将获得这个立方体的一个面,您的任务是猜测立方体反面的数字。

例子:

天真的方法:在普通的 6 面骰子中,1 与 6 相对,2 与 5 相对,3 与 4 相对。因此可以放置普通的 if-else-if 块

方法:这个想法是基于观察到立方体骰子的两个对边之和等于7 。因此,只需从7中减去给定的N并打印答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include
using namespace std;
 
// Function to find number
// written on the
// opposite face of the dice
int oppositeFaceOfDice(int N)
{
    // Stores number on opposite face
    // of dice
    int ans = 7 - N;
 
    // Print the answer
    cout << ans;
}
 
// Driver Code
int main()
{
    // Given value of N
    int N = 2;
 
    // Function call to find number written on
    // the opposite face of the dice
    oppositeFaceOfDice(N);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
 
class GFG
{
   
    // Function to find number
    // written on the
    // opposite face of the dice
    static void oppositeFaceOfDice(int N)
    {
       
        // Stores number on opposite face
        // of dice
        int ans = 7 - N;
 
        // Print the answer
        System.out.println(ans);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
       
        // Given value of N
        int N = 2;
 
        // Function call to find number written on
        // the opposite face of the dice
        oppositeFaceOfDice(N);
    }
}
 
// This code is contributed by Potta Lokesh


Python3
# Python3 program for the above approach
 
# Function to find number written on the
# opposite face of the dice
def oppositeFaceOfDice(N):
     
    # Stores number on opposite face
    # of dice
    ans = 7 - N
     
    # Print the answer
    print(ans)
 
# Driver Code
 
# Given value of N
N = 2
 
# Function call to find number written on
# the opposite face of the dice
oppositeFaceOfDice(N)
 
# This code is contributed by gfgking


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to find number
// written on the
// opposite face of the dice
static void oppositeFaceOfDice(int N)
{
   
    // Stores number on opposite face
    // of dice
    int ans = 7 - N;
 
    // Print the answer
    Console.Write(ans);
}
 
// Driver Code
public static void Main()
{
    // Given value of N
    int N = 2;
 
    // Function call to find number written on
    // the opposite face of the dice
    oppositeFaceOfDice(N);
}
}
 
// This code is contributed by SURENDRA_GANGWAR.


Javascript


输出
5

时间复杂度: O(1)
辅助空间: O(1)