天天看點

[PAT][basic level] 1008 review

Keywords: array reverse;defination && declare of the function;pointer

AC Code:

#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <algorithm>
using namespace std;
void reverse(int *array , int Pos_start , int Pos_end)
{
    int temp = 0 ;
    int i = Pos_start;
    int j = Pos_end;
     while(i<((Pos_start+Pos_end+1)/2)){
         
         temp = array[i];
        array[i] =  array[j];
        array[j] = temp;
        
         i++;
         j--;
     }
       
    return;
}
int main()
{
    int n,m;
    cin>>n>>m;
    m = m % n ;
    int *array_input;
    array_input=(int *)malloc(sizeof(int)*n);
    int *array_output;
    array_output=(int *)malloc(sizeof(int)*n);
    for(int i = 0 ; i < n ; i ++){
        cin>>array_input[i];
    }
        
        
        reverse(array_input,n-m,n-1);
        reverse(array_input,0,n-m-1);
        reverse(array_input,0,n-1);
        
    for(int i = 0; i < n ; i ++)
        array_output[i] = array_input[i];
    for(int i = 0 ;i < n ; i ++){
        
        if(i < n-1) 
         cout<<array_output[i]<<" ";
        else
        cout<<array_output[i];
    }
        
        
    return 0;
}