OR112. 大巴车(数组分块,按块翻转,块内不变)
描述
某天猿辅导 HR 组织大家去漂流,早上,参加团建的同学都到齐了,并且按到达公司的先后顺序排好队了。 由于员工太多,一个大巴车坐不下,需要分多个车,车是足够的,但所有人需要按一定顺序上车,按如下规则安排上车的顺序:#include <iostream> int main() { int memberCount, carCount; std::cin >> memberCount; std::cin >> carCount; int* members = new int[memberCount]; for (int i = 0; i < memberCount; i++) { std::cin >> members[i]; } // TODO: 按规则调整顺序 while(true) { // TODO: 输出,注意有空格隔开 std::cout << orders[i]; } delete[] members; delete[] orders; }
输入描述
第一行: 员工数和大巴容量输出描述
员工编号示例1
输入:
5 3 1 3 5 2 4
输出:
2 4 1 3 5
C 解法, 执行用时: 2ms, 内存消耗: 232KB, 提交时间: 2018-12-29
#include <stdio.h> int main(){ int m,n; while(scanf("%d %d",&n,&m)!=EOF){ int list[n]; int i; for(i=0;i<n;i++){ scanf("%d",&list[i]); } if(n<=m){ for(i=n-1;i>=0;i--){ printf("%d",list[i]); if(i!=0){ printf(" "); } } }else{ int start =0; while(start<=n-1){ start+=m; } start-=m; while(start>=0){ int temp = start; int count =0; while(count<m&&temp<n){ printf("%d",list[temp]); count++; temp++; printf(" "); } start-=m; } } printf("\n"); } return 0; }
C 解法, 执行用时: 2ms, 内存消耗: 368KB, 提交时间: 2020-11-22
#include<stdio.h> #include<stdlib.h> int main() { int i, member, car, *list; scanf("%d %d", &member, &car); list = (int *)malloc(member * sizeof(int)); for(i = 0; i < member; i++) { scanf("%d", &list[i]); } int m = member - member%car; for(i = m; i < member; i++) { printf("%d ", list[i]); } while(m != 0) { for(i = m - car; i < m; i++) { printf("%d ", list[i]); } m -= car; } return 0; }