A1046——入门模拟 Shortest Distance

2019-12-15

15:25:34

#include <bits/stdc++.h>
#include<math.h>
using namespace std;
const int MAXN = 100005;
int main(){
    int N;
    cin>>N;
    int temp[N+1];
    for(int i=0;i<N+1;++i){
        temp[i] =  0;
    }
    for(int i = 1;i<N+1;++i){
        cin>>temp[i];
    }
    int K = 0;
    cin>>K;
    int result[K];
    for(int i =0;i<K;++i){
        result[i] = 0;
    }
    for(int i=0;i<K;++i){
        int temp1 = 0,temp2 = 0;
        cin>>temp1;
        cin>>temp2;
        int tempt = abs(temp1-temp2);//求差值
        int tempt1 = N-tempt;//求N-差值
        int minmum = min(temp1,temp2);
        int result1 = 0;//正序结果
        int t = minmum;
        for(int j=0;j<tempt;++j){
            //int t = minmum;
            result1 += temp[t];
            t++;
        }
        int result2 = 0;//逆序结果
        int p;//p存放较大的数
        if(minmum==temp1){
            p = temp2;
        }
        if(minmum == temp2){
            p = temp1;
        }
        int s = p;
        for(int l=0;l<tempt1;++l){
            if(s>N){
                s = 1;
            }
            result2 += temp[s];
            s++;
        }
        result[i] = min(result1,result2);

    }
    for(int i=0;i<K;++i){
        cout<<result[i]<<endl;
    }
    system("pause");
    return 0;
} 


A1046——入门模拟 Shortest Distance

原文地址:https://www.cnblogs.com/JasonPeng1/p/12044299.html

时间: 2024-10-08 22:35:45

A1046——入门模拟 Shortest Distance的相关文章

[LeetCode] 613. Shortest Distance in a Line_Easy tag: SQL

Table point holds the x coordinate of some points on x-axis in a plane, which are all integers. Write a query to find the shortest distance between two points in these points. | x | |-----| | -1 | | 0 | | 2 | The shortest distance is '1' obviously, w

pat 1046 Shortest Distance(20 分) (线段树)

1046 Shortest Distance(20 分) The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed to tell the shortest distance between any pair of exits. Input Specification: Each input file contains one test case. For

[LeetCode&amp;Python] Problem 821. Shortest Distance to a Character

Given a string S and a character C, return an array of integers representing the shortest distance from the character C in the string. Example 1: Input: S = "loveleetcode", C = 'e' Output: [3, 2, 1, 0, 1, 0, 0, 1, 2, 2, 1, 0] Note: S string leng

PAT——甲级1046S:shortest Distance

这道题,折磨了我一个多小时,前前后后写了三个算法. 1046 Shortest Distance (20 point(s)) The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed to tell the shortest distance between any pair of exits. Input Specification: Each input

[Solution] 821. Shortest Distance to a Character

Difficulty: Easy Problem Given a string S and a character C, return an array of integers representing the shortest distance from the character C in the string. Example 1: Input: S = "loveleetcode", C = 'e' Output: [3, 2, 1, 0, 1, 0, 0, 1, 2, 2,

1046 Shortest Distance (20 分)

1046 Shortest Distance (20 分) The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed to tell the shortest distance between any pair of exits. Input Specification: Each input file contains one test case. For

(Easy) Shortest distance to Character LeetCode

Description: Given a string S and a character C, return an array of integers representing the shortest distance from the character C in the string. Example 1: Input: S = "loveleetcode", C = 'e' Output: [3, 2, 1, 0, 1, 0, 0, 1, 2, 2, 1, 0] Note:

1046 Shortest Distance (20分)

The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed to tell the shortest distance between any pair of exits. Input Specification: Each input file contains one test case. For each case, the first line con

1046 Shortest Distance (20 分)(简单模拟,前缀和)

题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805435700199424 注意:1.这是一个环,两点之间的距离有两种,一是顺时针,二是逆时针,求出其中的一个距离,另一个距离用这个圆的总距离减去得到,输出两个距离之间最小的那个 2.注意区分x,y的大小,有可能下标大的在前面. 3.用到了前缀和的思想 1 #include <algorithm> 2 #include <iostream> 3