zoukankan      html  css  js  c++  java
  • 2017 JUST Programming Contest 3.0 D. Dice Game



    D. Dice Game
    time limit per test
    1.0 s
    memory limit per test
    256 MB
    input
    standard input
    output
    standard output

    A dice is a small cube, with each side having a different number of spots on it, ranging from 1 to 6.

    Each side in the dice has 4 adjacent sides that can be reached by rotating the dice (i.e. the current side) 90 degrees. The following picture can help you to conclude the adjacent sides for each side in the dice.

    In this problem, you are given a dice with the side containing 1 spot facing upwards, and a sum n, your task is to find the minimum number of required moves to reach the given sum.

    On each move, you can rotate the dice 90 degrees to get one of the adjacent sides to the side that currently facing upwards, and add the value of the new side to your current sum. According to the previous picture, if the side that currently facing upwards contains 1 spot, then in one move you can move to one of sides that contain 2, 3, 4, or 5 spots.

    Initially, your current sum is 0. Even though at the beginning the side that containing 1 spot is facing upwards, but its value will not be added to your sum from the beginning, which means that you must make at least one move to start adding values to your current sum.

    Input

    The first line contains an integer T (1 ≤ T ≤ 200), where T is the number of test cases.

    Then T lines follow, each line contains an integer n (1 ≤ n ≤ 104), where n is the required sum you need to reach.

    Output

    For each test case, print a single line containing the minimum number of required moves to reach the given sum. If there is no answer, print -1.

    Example
    input
    2
    5
    10
    
    output
    1
    2
    
    Note

    In the first test case, you can rotate the dice 90 degrees one time, and make the side that contains 5 spots facing upwards, which make the current sum equal to 5. So, you need one move to reach sum equal to 5.

    In the second test case, you can rotate the dice 90 degrees one time, and make the side that contains 4 spots facing upwards, which make the current sum equal to 4. Then rotate the dice another 90 degrees, and make the side that contains 6 spots facing upwards, which make the current sum equal to 10. So, you need two moves to reach sum equal to 10.



    #include<iostream>
    #include<cstdio>
    #include<cmath>
    using namespace std;
    #define ll long long
    const int maxn=100005;
    
    int a[25]={0,1,1,1,1,1,
                2,3,2,2,2,
                2,3,3,3,3,
                3,4,4,4,4,4,4
                };
    
    int main()
    {
    //    freopen("in.txt","r",stdin);
        int T,n;
        scanf("%d",&T);
        int yu,bei;
        while(T--)
        {
            int ans=0;
            scanf("%d",&n);
            if(n==1)
                cout<<-1<<endl;
            else if(n<=11)
                cout<<a[n]<<endl;
            else if(n>11)
            {
                while(n>11){
                    n-=11;
                    ans+=2;
                }
                if(n==7)
                    ans-=1;
                ans+=a[n];
                cout<<ans<<endl;
            }
        }
    }


  • 相关阅读:
    数据结构(四十)平衡二叉树(AVL树)
    数据结构(三十九)二叉排序树
    数据结构(三十八)静态查找表(顺序查找、二分查找、插值查找、斐波那契查找、线性索引查找)
    数据结构(三十七)查找的基本概念
    数据结构(三十六)关键路径
    数据结构(三十五)拓扑排序
    数据结构(三十四)最短路径(Dijkstra、Floyd)
    数据结构(三十三)最小生成树(Prim、Kruskal)
    字符串匹配算法之KMP
    最长公共子序列(Longest common subsequence)
  • 原文地址:https://www.cnblogs.com/bryce1010/p/9387152.html
Copyright © 2011-2022 走看看