SDUT 2018 Winter Individual Training (for 17) -6

There are another kind of Fibonacci numbers: F(0) = 7, F(1) = 11, F(n) = F(n-1) + F(n-2) (n>=2). 
InputInput consists of a sequence of lines, each containing an integer n. (n < 1,000,000). 
OutputPrint the word "yes" if 3 divide evenly into F(n). 
(3平均分,就是被三整除)
Print the word "no" if not. 
Sample Input
0
1
2
3
4
5
Sample Output
no
no
yes
no
no
no
#include<bits/stdc++.h>
using namespace std;
#define N 1000000
typedef long long ll;
using namespace std;
ll num[N];
void dabiao()
{
    num[0]=7%3;
    num[1]=11%3;
    for(int i=2;i<=1000000;++i)
    num[i]=(num[i-1]+num[i-2])%3;
}
int main()
{
dabiao();
int n;
while(cin>>n)
{
    if(num[n]==0)
        cout<<"yes"<<endl;
    else
        cout<<"no"<<endl;}
    return 0;
}//关键在于打表的时候对每次都对3取余。else 数据会爆。
也可以找规律做

  1. #include<iostream>  
  2. using namespace std;  
  3.   
  4. int main()  
  5. {// 7 11 18|. 29 47 76 123|. 199 322 521 843|. 1364 2207 3571 5578|.  
  6.     // 2  6  10...  
  7.     //综上所述可知规律  
  8.     //一般的思维会想着先把数列写出来,然后进行作除判断,但是这道题有其规律性  
  9.     int n ;  
  10.   
  11.     while(cin>>n)  
  12.     {  
  13.         if(n%4 == 2)  
  14.         {  
  15.             cout<<"yes"<<endl;  
  16.         }  
  17.         else  
  18.         {  
  19.             cout<<"no"<<endl;  
  20.         }  
  21.     }  
  22.   
  23.     return 0;  
  24. }  
  25. You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot move diagonally and the maze is surrounded by solid rock on all sides. 

    Is an escape possible? If yes, how long will it take? 
    Input
    The input consists of a number of dungeons. Each dungeon description starts with a line containing three integers L, R and C (all limited to 30 in size). 
    L is the number of levels making up the dungeon. 
    R and C are the number of rows and columns making up the plan of each level. 
    Then there will follow L blocks of R lines each containing C characters. Each character describes one cell of the dungeon. A cell full of rock is indicated by a '#' and empty cells are represented by a '.'. Your starting position is indicated by 'S' and the exit by the letter 'E'. There's a single blank line after each level. Input is terminated by three zeroes for L, R and C.
    Output
    Each maze generates one line of output. If it is possible to reach the exit, print a line of the form 
    Escaped in x minute(s).

    where x is replaced by the shortest time it takes to escape. 
    If it is not possible to escape, print the line 
    Trapped!
    Sample Input
    3 4 5
    S....
    .###.
    .##..
    ###.#
    
    #####
    #####
    ##.##
    ##...
    
    #####
    #####
    #.###
    ####E
    
    1 3 3
    S##
    #E#
    ###
    
    0 0 0
    
    Sample Output
    Escaped in 11 minute(s).
    Trapped!
    #include <stdio.h>
    #include <queue>
    #include<iostream>
    #include<cstring>
    using namespace std;
    char mmp[35][35][35];
    int vis[35][35][35];
    int k,n,m,sx,sy,sz,ex,ey,ez;
    int to[6][3]={{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};
    struct node
    {
        int x,y,z,step;
    };
    int check(int x,int y,int z)
    {
        if(x<0 || y<0 || z<0 || x>=k || y>=n || z>=m)
            return 1;
        else if(mmp[x][y][z] == '#')
            return 1;
        else if(vis[x][y][z])
            return 1;
        return 0;
    }
    int bfs()
    {
        int i;node a,next;
        queue<node>Q;
        a.x=sx,a.y=sy,a.z=sz;
        a.step=0;
        vis[sx][sy][sz]=1;
        Q.push(a);
        while(!Q.empty())
        {
            a=Q.front();
            Q.pop();
            if(a.x==ex&&a.y==ey&&a.z==ez)
            return a.step;
            for(i=0;i<6;i++)
            {
                next=a;
                next.x=a.x+to[i][0];
                next.y=a.y+to[i][1];
                next.z=a.z+to[i][2];
                if(check(next.x,next.y,next.z))
                    continue;
                vis[next.x][next.y][next.z]=1;
                next.step=a.step+1;
                Q.push(next);
            }
        }
        return 0;
    }
    int main()
    {
        int i,j,r;
        while(scanf("%d%d%d",&k,&n,&m),n+m+k)
        {
            for(i = 0; i<k; i++)
            {
                for(j = 0; j<n; j++)
                {
                    scanf("%s",mmp[i][j]);
                    for(r = 0; r<m; r++)
                    {
                        if(mmp[i][j][r] == 'S')
                        {
                            sx = i,sy = j,sz = r;
                        }
                        else if(mmp[i][j][r] == 'E')
                        {
                            ex = i,ey = j,ez = r;
                        }
                    }
                }
            }
            memset(vis,0,sizeof(vis));
            int ans;
            ans = bfs();
            if(ans)
                printf("Escaped in %d minute(s).\n",ans);
            else
                printf("Trapped!\n");
        }


        return 0;
  26. }



There is sequence 1, 12, 123, 1234, ..., 12345678910, ... . Now you are given two integers A and B, you have to find the number of integers from Ath number to Bth (inclusive) number, which are divisible by 3.

For example, let A = 3. B = 5. So, the numbers in the sequence are, 123, 1234, 12345. And 123, 12345 are divisible by 3. So, the result is 2.

Input

Input starts with an integer T (≤ 10000), denoting the number of test cases.

Each case contains two integers A and B (1 ≤ A ≤ B < 231) in a line.

Output

For each case, print the case number and the total numbers in the sequence between Ath and Bthwhich are divisible by 3.

Sample Input

2

3 5

扫描二维码关注公众号,回复: 1439736 查看本文章

10 110

Sample Output

Case 1: 2

Case 2: 67

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
long long f(long long n)
{
long long s=n/3;
long long sum;
if(n%3==1||n%3==0)
sum=s*2;
else
sum=s*2+1;
return sum;
}
int main()
{
int t,k=1;
long long a,b;
scanf("%d",&t);
while(t--)
{
scanf("%lld%lld",&a,&b);
long long num=0;
num=f(b)-f(a)+(a%3==1?0:1);
printf("Case %d: ",k++);
printf("%lld\n",num);
}
return 0;

}

An array of size  n ≤ 10  6 is given to you. There is a sliding window of size  kwhich is moving from the very left of the array to the very right. You can only see the  k numbers in the window. Each time the sliding window moves rightwards by one position. Following is an example: 
The array is  [1 3 -1 -3 5 3 6 7], and  k is 3.
Window position Minimum value Maximum value
[1  3  -1] -3  5  3  6  7  -1 3
 1 [3  -1  -3] 5  3  6  7  -3 3
 1  3 [-1  -3  5] 3  6  7  -3 5
 1  3  -1 [-3  5  3] 6  7  -3 5
 1  3  -1  -3 [5  3  6] 7  3 6
 1  3  -1  -3  5 [3  6  7] 3 7

Your task is to determine the maximum and minimum values in the sliding window at each position. 

Input
The input consists of two lines. The first line contains two integers  n and  k which are the lengths of the array and the sliding window. There are  n integers in the second line. 
Output
There are two lines in the output. The first line gives the minimum values in the window at each position, from left to right, respectively. The second line gives the maximum values. 
Sample Input
8 3
1 3 -1 -3 5 3 6 7
Sample Output
-1 -3 -3 -3 3 3
3 3 5 5 6 7

猜你喜欢

转载自blog.csdn.net/beposit/article/details/79344775