Giải pháp HackerRank máy in tròn trong C++

John Watson biết về một phép toán gọi là phép quay vòng phải trên một mảng các số nguyên. Một thao tác xoay sẽ di chuyển phần tử mảng cuối cùng đến vị trí đầu tiên và dịch chuyển tất cả các phần tử còn lại sang phải một. Để kiểm tra khả năng của Sherlock, Watson cung cấp cho Sherlock một dãy số nguyên. Sherlock là thực hiện thao tác xoay một số lần sau đó xác định giá trị của phần tử tại một vị trí nhất định

Đối với mỗi mảng, thực hiện một số phép quay tròn bên phải và trả về các giá trị của các phần tử tại các chỉ số đã cho

Show

    Ví dụ

    a = [3,4,5]

    k = 2

    truy vấn = [1,2]

    Ở đây k là số lần quay trên a và các truy vấn chứa danh sách các chỉ số cần báo cáo. Đầu tiên chúng tôi thực hiện hai phép quay. [3,4,5] -> [5,3,4] -> [4,5,3]

    Bây giờ hãy trả lại các giá trị từ các chỉ số dựa trên số không 1 và 2 như được chỉ ra trong mảng truy vấn

    a[1] = 5

    a[5] = 3

    Mô tả chức năng

    Hoàn thành chức năng circleArrayRotation trong trình chỉnh sửa bên dưới

    circleArrayRotation có (các) tham số sau

    • int a[n]. mảng để xoay
    • int k. số vòng quay
    • truy vấn int[1]. các chỉ số để báo cáo

    trả lại

    • int[q]. các giá trị trong phần được xoay theo yêu cầu trong

    Định dạng đầu vào

    Dòng đầu tiên chứa 3 số nguyên n, k và q, số phần tử trong mảng số nguyên, số vòng quay và số truy vấn

    Dòng thứ hai chứa n số nguyên được phân tách bằng dấu cách, trong đó mỗi số nguyên i mô tả phần tử mảng  a[i] (trong đó 0<=i

    Mỗi q dòng tiếp theo chứa một số nguyên duy nhất,queries[i] , chỉ số của một phần tử trong a để trả về

    Cho một thùng chứa hình tròn bao gồm các bảng chữ cái viết thường từ 'a' đến 'z', một con trỏ ban đầu chỉ vào bảng chữ cái 'a' và một chuỗi str, nhiệm vụ là tìm thời gian tối thiểu cần thiết để in chuỗi str đã cho từ vòng tròn

    • Di chuyển con trỏ một ký tự ngược chiều kim đồng hồ hoặc theo chiều kim đồng hồ trong một đơn vị thời gian
    • In ký tự trong một đơn vị thời gian, sau đó di chuyển con trỏ đến chỉ mục tiếp theo của chuỗi

    Ví dụ

    Đầu vào. str = “zcd”
    Đầu ra. 8
    Giải thích. Các bước thực hiện như sau.

    • Di chuyển con trỏ ngược chiều kim đồng hồ đến 'z' trong 1 giây
    • Nhập ký tự 'z' trong 1 giây
    • Di chuyển con trỏ theo chiều kim đồng hồ đến 'c' trong 3 giây
    • Nhập ký tự 'c' trong 1 giây
    • Di chuyển con trỏ theo chiều kim đồng hồ đến 'd' trong 1 giây
    • Nhập ký tự 'd' trong 1 giây

    Đầu vào. str =”zjpc”
    Đầu ra. 34
    Giải thích. Các bước thực hiện như sau.

    • Di chuyển con trỏ ngược chiều kim đồng hồ đến 'z' trong 1 giây
    • Nhập ký tự 'z' trong 1 giây
    • Di chuyển con trỏ theo chiều kim đồng hồ đến 'j' trong 10 giây
    • Nhập ký tự 'j' trong 1 giây
    • Di chuyển con trỏ theo chiều kim đồng hồ đến 'p' trong 6 giây
    • Nhập ký tự 'p' trong 1 giây
    • Di chuyển con trỏ ngược chiều kim đồng hồ đến 'c' trong 13 giây

     

    Tiếp cận. Các bước dưới đây có thể được thực hiện để giải quyết vấn đề đã cho

    • Tính toán thời gian cần thiết để đạt được chỉ mục ký tự từ chỉ mục con trỏ hiện tại theo cả hai hướng
      • thời gian theo chiều kim đồng hồ được tính bằng cách lấy chênh lệch tuyệt đối của cả hai chỉ số
      • thời gian ngược chiều kim đồng hồ được tính bằng cách trừ thời gian theo chiều kim đồng hồ từ 26
    • Tối thiểu cả hai lần thu được trong bước trước được thêm vào câu trả lời
    • Sau đó, để in ký tự, một đơn vị thời gian cũng được thêm vào câu trả lời

    Cuối cùng in câu trả lời thu được trong thời gian tối thiểu cần thiết

    Dưới đây là việc thực hiện các phương pháp trên

    C++




    // C++ implementation for the above approach

    #include

    using namespace std;

     

    // Function to calculate minimum time to

    // print all characters in the string

    void minTime(string word)

    // C++ implementation for the above approach0

     

    // C++ implementation for the above approach1// C++ implementation for the above approach2 // C++ implementation for the above approach3

     

    // C++ implementation for the above approach1// C++ implementation for the above approach5

    // C++ implementation for the above approach1// C++ implementation for the above approach7

    // C++ implementation for the above approach1// C++ implementation for the above approach2 #include 0

     

    // C++ implementation for the above approach1#include 2 #include 3______02 #include 5

     

    #include 6#include 7

    #include 6// C++ implementation for the above approach2 using0using1using2

     

    #include 6using4

    #include 6using6

    #include 6using8

    #include 6// C++ implementation for the above approach2 namespace1____32namespace3

     

    #include 6namespace5

    #include 6namespace7

    #include 6// C++ implementation for the above approach2 std;0namespace2namespace3

     

    #include 6std;4

    #include 6std;6

    #include 6std;8

     

    #include 6// Function to calculate minimum time to0

    #include 6// Function to calculate minimum time to2

    #include 6// Function to calculate minimum time to4

     

    #include 6// Function to calculate minimum time to6using1using2

    // C++ implementation for the above approach1// print all characters in the string0

     

    // C++ implementation for the above approach1// print all characters in the string2

    // C++ implementation for the above approach1// print all characters in the string4

    // print all characters in the string0

     

    // print all characters in the string6

    // C++ implementation for the above approach2 // print all characters in the string8

    // C++ implementation for the above approach0

    // C++ implementation for the above approach1void1

    // C++ implementation for the above approach1void3void4using2

     

    // C++ implementation for the above approach1void7

    // C++ implementation for the above approach1void9

    // C++ implementation for the above approach1minTime(string word)1 minTime(string word)2

    // print all characters in the string0

    Java




    minTime(string word)4

    minTime(string word)5 minTime(string word)6

     

    // Function to calculate minimum time to

    // print all characters in the string

    minTime(string word)9 void // C++ implementation for the above approach01

    // C++ implementation for the above approach0

     

    // C++ implementation for the above approach1// C++ implementation for the above approach2 // C++ implementation for the above approach05// C++ implementation for the above approach06using2

     

    // C++ implementation for the above approach1// C++ implementation for the above approach5

    // C++ implementation for the above approach1// C++ implementation for the above approach7

    // C++ implementation for the above approach1// C++ implementation for the above approach2 // C++ implementation for the above approach14// C++ implementation for the above approach06using2

     

    // C++ implementation for the above approach1#include 2 #include 3// C++ implementation for the above approach2 // C++ implementation for the above approach21// C++ implementation for the above approach06// C++ implementation for the above approach23

     

    #include 6#include 7

    #include 6// C++ implementation for the above approach2 // C++ implementation for the above approach28// C++ implementation for the above approach2// C++ implementation for the above approach30// C++ implementation for the above approach31using2

     

    #include 6using4

    #include 6using6

    #include 6using8

    #include 6// C++ implementation for the above approach2 // C++ implementation for the above approach41

     

    #include 6namespace5

    #include 6namespace7

    #include 6// C++ implementation for the above approach2 // C++ implementation for the above approach48// C++ implementation for the above approach49 // C++ implementation for the above approach50

     

    #include 6std;4

    #include 6std;6

    #include 6// C++ implementation for the above approach56

     

    #include 6// Function to calculate minimum time to0

    #include 6// Function to calculate minimum time to2

    #include 6// Function to calculate minimum time to4

     

    #include 6// C++ implementation for the above approach64// C++ implementation for the above approach2// C++ implementation for the above approach30// C++ implementation for the above approach31using2

    // C++ implementation for the above approach1// print all characters in the string0

     

    // C++ implementation for the above approach1// print all characters in the string2

    _______01____074

    // print all characters in the string0

     

    // print all characters in the string6

    // C++ implementation for the above approach77 minTime(string word)9 void // C++ implementation for the above approach80

    // C++ implementation for the above approach0

    // C++ implementation for the above approach82

    // C++ implementation for the above approach1void1

    // C++ implementation for the above approach1// C++ implementation for the above approach86void4using2

     

    // C++ implementation for the above approach1void7

    // C++ implementation for the above approach1void9

    // print all characters in the string0

     

    // print all characters in the string0

     

    // C++ implementation for the above approach95

    Python3




    // C++ implementation for the above approach96

     

    // C++ implementation for the above approach97

    // C++ implementation for the above approach98

    // C++ implementation for the above approach99 #include 00

    ________ 01 ________ 102 ________ 103 ________ 006

     

    // C++ implementation for the above approach1#include 06

    // C++ implementation for the above approach1#include 08

    // C++ implementation for the above approach1#include 10#include 03 // C++ implementation for the above approach06

     

    // C++ implementation for the above approach1#include 2 #include 15#include 16 #include 17#include 3#include 19#include 20

    #include 6#include 22

    #include 6#include 24#include 03 #include 26#include 27#include 28 // C++ implementation for the above approach31

     

    #include 6#include 31

    #include 6#include 33

    #include 6#include 35

    #include 6#include 37#include 03 namespace2#include 40#include 28 #include 42

     

    #include 6#include 44

    #include 6#include 46

    #include 6#include 48#include 03 // C++ implementation for the above approach49 #include 28 namespace2#include 40#include 28 #include 42