1. 程式人生 > 實用技巧 >SQL中的case when then else end用法

SQL中的case when then else end用法

A. Box is Pull time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output

Wabbit is trying to move a box containing food for the rest of the zoo in the coordinate plane from the point(x1,y1)(x1,y1)to the point(x2,y2)(x2,y2).

He has a rope, which he can use to pull the box. He can only pull the box if he standsexactly11unit away from the box in the direction of one of two coordinate axes. He will pull the box to where he is standing before moving out of the way in the same direction by

11unit.

For example, if the box is at the point(1,2)(1,2)and Wabbit is standing at the point(2,2)(2,2), he can pull the box right by11unit, with the box ending up at the point(2,2)(2,2)and Wabbit ending at the point(3,2)(3,2).

Also, Wabbit can move11unit to the right, left, up, or down without pulling the box. In this case, it is not necessary for him to be in exactly

11unit away from the box. If he wants to pull the box again, he must return to a point next to the box. Also, Wabbit can't move to the point where the box is located.

Wabbit can start at any point. It takes11second to travel11unit right, left, up, or down, regardless of whether he pulls the box while moving.

Determine the minimum amount of time he needs to move the box from(x1,y1)(x1,y1)to(x2,y2)(x2,y2). Note that the point where Wabbit ends up at does not matter.

Input

Each test contains multiple test cases. The first line contains a single integertt(1t1000)(1≤t≤1000): the number of test cases. The description of the test cases follows.

Each of the nextttlines contains four space-separated integersx1,y1,x2,y2x1,y1,x2,y2(1x1,y1,x2,y2109)(1≤x1,y1,x2,y2≤109), describing the next test case.

Output

For each test case, print a single integer: the minimum time in seconds Wabbit needs to bring the box from(x1,y1)(x1,y1)to(x2,y2)(x2,y2).

Example input Copy
2
1 2 2 2
1 1 2 2
output Copy
1
4
Note

In the first test case, the starting and the ending points of the box are(1,2)(1,2)and(2,2)(2,2)respectively. This is the same as the picture in the statement. Wabbit needs only11second to move as shown in the picture in the statement.

In the second test case, Wabbit can start at the point(2,1)(2,1). He pulls the box to(2,1)(2,1)while moving to(3,1)(3,1). He then moves to(3,2)(3,2)and then to(2,2)(2,2)without pulling the box. Then, he pulls the box to(2,2)(2,2)while moving to(2,3)(2,3). It takes44seconds.

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

int main(){
    int T; cin >> T;
    while (T -- ) {
        int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2;
        if (abs(x1 - x2) == 0) cout << abs(y1 - y2) << endl;
        else if (abs(y1 - y2) == 0) cout << abs(x1 - x2) << endl;
        else cout << abs(x1 - x2) + abs(y1 - y2) + 2 << endl;
    }
    
    return 0;
}

  

B. Belted Rooms time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output

In the snake exhibition, there arennrooms (numbered00ton1n−1) arranged in a circle, with a snake in each room. The rooms are connected bynnconveyor belts, and theii-th conveyor belt connects the roomsiiand(i+1)modn(i+1)modn. In the other words, rooms00and11,11and22,…,n2n−2andn1n−1,n1n−1and00are connected with conveyor belts.

Theii-th conveyor belt is in one of three states:

  • If it is clockwise, snakes can only go from roomiito(i+1)modn(i+1)modn.
  • If it is anticlockwise, snakes can only go from room(i+1)modn(i+1)modntoii.
  • If it is off, snakes can travel in either direction.

Above is an example with44rooms, where belts00and33are off,11is clockwise, and22is anticlockwise.

Each snake wants to leave its room and come back to it later. A room isreturnableif the snake there can leave the room, and later come back to it using the conveyor belts. How many suchreturnablerooms are there?

Input

Each test contains multiple test cases. The first line contains a single integertt(1t10001≤t≤1000): the number of test cases. The description of the test cases follows.

The first line of each test case description contains a single integernn(2n3000002≤n≤300000): the number of rooms.

The next line of each test case description contains a stringssof lengthnn, consisting of only '<', '>' and '-'.

  • Ifsi=si='>', theii-th conveyor belt goes clockwise.
  • Ifsi=si='<', theii-th conveyor belt goes anticlockwise.
  • Ifsi=si='-', theii-th conveyor belt is off.

It is guaranteed that the sum ofnnamong all test cases does not exceed300000300000.

Output

For each test case, output the number of returnable rooms.

Example input Copy
4
4
-><-
5
>>>>>
3
<--
2
<>
output Copy
3
5
3
0
Note

In the first test case, all rooms are returnable except room22. The snake in the room22is trapped and cannot exit. This test case corresponds to the picture from the problem statement.

In the second test case, all rooms are returnable by traveling on the series of clockwise belts.

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    
    int T; cin >> T;
    while (T -- ) {
        int n; cin >> n;
        string s; cin >> s;
        
        //判斷一下是否存在正序和倒序。
        //如果兩個同時存在,則一定會有走不通的情況
        //如果兩個不同時存在,或者都不存在,剩下的'-'會讓
        //是所有蛇都有通路
        bool has_cw = false, has_ccw = false;
        for (int i = 0; i < n; i ++ ) {
            if (s[i] == '<') has_cw = true;
            if (s[i] == '>') has_ccw = true;
        }
        
        if (has_cw && has_ccw){
            int ans = 0;
            s += s[0];//處理一下n-1到0
            for (int i = 0; i < n; i ++ ){
                if (s[i] == '-' || s[i + 1] == '-') ans ++;
                //處理一下n-1到0            
                
            }
            cout << ans << endl;
        }
        else cout << n<< endl;
    }
    
    return 0;
}