小易有一个长度为 n 的整数序列,,...,。然后考虑在一个空序列 b 上进行 n 次以下操作:
1、将 放入 b 序列的末尾
2、逆置 b 序列
小易需要你计算输出操作 n 次之后的 b 序列。
输入包括两行,第一行包括一个整数n(2 ≤ n ≤ 2*10^5),即序列的长度。 第二行包括n个整数 ai (1 ≤ ai ≤ 10^9),即序列a中的每个整数,以空格分割。
在一行中输出操作 n 次之后的 b 序列,以空格分割,行末无空格。
4 1 2 3 4
4 2 1 3
//规律题 //n = 1,b = 1 n = 1直接输出 //n = 2,b = 2,1 //n = 3,b = 3,1,2 //n = 4,b = 4,2,1,3 //n = 5,b = 5,3,1,2,4 //n = 6,b = 6,4,2,1,3,5 //由上述可推,当n 为奇数时, //先从后向前输出奇数位置的数字,再从前向后输出偶数位置的数字 //当n 为偶数时 //先从后向前输出偶数位置的数字,再从前向后输出奇数位置的数字 import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Scanner; public class test4 { public static void main(String[] args) { // TODO Auto-generated method stub Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int [] array = new int [n]; for(int i = 0;i < n;i++){ array[i] = scanner.nextInt(); } if(n == 1){ System.out.println(array[0]); } if(n % 2 == 0){ for(int i = n-1;i >= 0;i -= 2){ System.out.print(array[i] + " "); } for(int i = 0;i < n - 2;i += 2){ System.out.print(array[i] + " "); } System.out.print(array[n-2]); } else { //n % 2 == 1 for(int i = n-1;i >= 0;i -= 2){ System.out.print(array[i] + " "); } for(int i = 1;i < n - 2;i += 2){ System.out.print(array[i] + " "); } System.out.print(array[n-2]); } } }
#include<iostream> #include<deque> using namespace std; const int maxn = 2 * 1e5 + 5; int arr[maxn], n; deque<int> dp; int main(){ cin>>n; for(int i=0;i<n;i++) scanf("%d", &arr[i]); for(int i=0;i<n;i++){ if(i % 2 == 0) dp.push_back(arr[i]); else dp.push_front(arr[i]); } if(n % 2 == 1){ deque<int> dp_rev(dp.rbegin(), dp.rend()); dp = dp_rev; } for(int i = 0; i < dp.size(); i++){ printf("%d", dp[i]); if(i != dp.size() - 1) printf(" "); } printf("\n"); return 0; }
#!/usr/bin/env python #-*- coding:utf8 -*- def findNum(nums, n): for i in range(n-1, -1, -2): print nums[i], if n&1 == 0: for i in range(0, n, 2): print nums[i], else: for i in range(1, n ,2): print nums[i], if __name__ == '__main__': n = input() nums = map(int, raw_input().split()) findNum(nums, n)
//别被迷惑了,其实不需要逆序,直接从后向前间隔一个输出,然后没有输出的顺序输出即可 #include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { long long n; while(cin>>n) { vector<int> a(n); for(long long i=0;i<n;++i) cin>>a[i]; if(n == 1)//只有一个的话直接输出就行,感谢"...石头..."的纠正. { cout<<a[0]<<endl; return 0; } if(n%2==0)//我这里区分了一下奇数和偶数,更好处理 { for(long long i=n-1;i>=0;i-=2) { cout<<a[i]<<" "; } cout<<a[0]; for(long long i=2;i<n-1;i+=2) cout<<" "<<a[i]; } else { for(long long i=n-1;i>=0;i-=2) { cout<<a[i]<<" "; } cout<<a[1]; for(long long i=3;i<n-1;i+=2) cout<<" "<<a[i]; } cout<<endl; } return 0; }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNextInt()) { int n = in.nextInt(); int[] nums = new int[n]; for (int i = 0; i < n; i++) { nums[i] = in.nextInt(); } for (int i = n - 1; i >= 0; i -= 2) { // 前一半从最后一个数开始以2为步长递减 System.out.print(nums[i] + " "); } for (int i = n % 2; i < n - 2; i += 2) { // 后一半根据整数个数的奇偶,分别从第二个或第一个数开始以2为步长递增 System.out.print(nums[i] + " "); } System.out.print(nums[n - 2]); // 最后一个数 } } }
#include <iostream> #include <vector> using namespace std; int main() { int n; cin>>n; vector<int> a; for(int i=0;i<n;i++){ int temp; cin>>temp; a.push_back(temp); } for (int i=n-1;i>=0;i-=2) cout<<a[i]<<' '; for (int i=((n&1)==1? 1:0);i<n-2;i+=2) cout<<a[i]<<' '; cout<<a[n-2]<<endl; return 0; }
#include<bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> a(n, 0); for (int i = 0; i < n; i++) cin >> a[i]; vector<int> b(n, 0); int cnt = 0; for (int i = n-1; i >=0; i--) { if (cnt % 2 == 0) { b[cnt / 2] = a[i]; } else { b[n - 1 - cnt/2] = a[i]; } cnt++; } for (int bi : b) cout << bi << " "; cout << endl; return 0; }
linkList结构:既可以用来做栈、也可以用来做队列 一次往尾节点插入(队列),一次往头结点插入(栈) 如果是偶数,顺着输出;如果是奇数,逆着输出 import java.util.Iterator; import java.util.LinkedList; import java.util.Scanner; public class CaoZuoXuLie { public static void main(String[] args) { // int n = 5; // int[] a = { 1, 2, 3, 4, 5 }; Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = scanner.nextInt(); } Iterator<Integer> iterator = solve(n, a); while (iterator.hasNext()) { System.out.print(iterator.next() + " "); } System.out.println(); } public static Iterator<Integer> solve(int n, int[] a) { LinkedList<Integer> linkedList = new LinkedList<>(); for (int i = 0; i < n; i++) { if ((i & 1) == 0) { linkedList.offer(a[i]); } else { linkedList.push(a[i]); } } if ((n & 1) == 0) { return linkedList.iterator(); } return linkedList.descendingIterator(); } }
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; import java.util.Deque; import java.util.ArrayDeque; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine().trim()); String[] seq = br.readLine().trim().split(" "); // 创建一个双端队列,无需反转,根据操作次数的奇偶分别向对头和队尾添加元素 Deque<String> deque = new ArrayDeque<>(); for(int i = 0; i < n; i++){ if(i % 2 == 0) deque.addFirst(seq[i]); else deque.addLast(seq[i]); } if(n % 2 == 0){ // 偶数长度从后面出队 while(!deque.isEmpty()) System.out.print(deque.pollLast() + " "); }else{ // 奇数长度从前面出队 while(!deque.isEmpty()) System.out.print(deque.pollFirst() + " "); } } }
#include <bits/stdc++.h> using namespace std; int main(){ int n, j=0, k=0; cin>>n; int a[n/2], b[n/2]; for(int i=0;i<n;i++){ if(i&1) scanf("%d", &b[k++]); else scanf("%d", &a[j++]); } if(n==1) printf("%d\n", a[0]); else{ if(n&1){ for(int i=(n-1)/2;i>=0;i--) printf("%d ", a[i]); for(int i=0;i<n/2;i++){ if(i==n/2-1) printf("%d\n", b[i]); else printf("%d", b[i]); } }else{ for(int i=(n-1)/2;i>=0;i--) printf("%d ", b[i]); for(int i=0;i<=(n-1)/2;i++){ if(i==(n-1)/2) printf("%d\n", a[i]); else printf("%d ", a[i]); } } } return 0; }
import java.util.Arrays; import java.util.Scanner; /** * @Date : @{DATE} * @Author : xiuc_shi **/ public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = Integer.valueOf(in.nextLine()); int[] a = Arrays.stream(in.nextLine().split(" ")).mapToInt(i -> Integer.valueOf(i)).toArray(); for(int i = n - 1; i >= 0;i -= 2){ System.out.print(a[i] + " "); } for(int i = (n % 2 == 0? 0 : 1) ; i < n - 1;i += 2){ System.out.print(a[i] + " "); } } }
n = int(input()) a = list(map(int, input().strip().split())) res = [] for digit in a[::-2]: res.append(digit) begin = 1 if n & 1 else 0 for digit in a[begin::2]: res.append(digit) print(" ".join(str(v) for v in res))
clean the code
n = int(input()) a = list(input().strip().split()) print(' '.join(a[::-2]) + ' ' + ' '.join([n & 1::2]))
#include<iostream> #include<vector> #include<algorithm> using namespace std; int main() { long n; scanf("%ld", &n); vector<int> input_list; for (long i = 0; i < n; i++) { int temp; scanf("%d", &temp); input_list.push_back(temp); } vector<int> b_list (n,0); for (long i = 0; i < n/2.0; i++) { if(!input_list.empty()) { b_list.at(i) = input_list.back(); input_list.pop_back(); } if(!input_list.empty() && (n - 1 - i) != i) { b_list.at(n - 1 - i) = input_list.back(); input_list.pop_back(); } } for (long i = 0; i < n - 1; i++) { printf("%d ", b_list.at(i)); } printf("%d", b_list.at(n - 1)); }
//有同学知道为什么我这个运行时间这么长吗?100多ms //本题是有规律的,偶数个数时,如 6 4 2 1 3 5,奇数个数时,如7 5 3 1 2 4 6; #include <iostream> #include <vector> using namespace std; int main() { unsigned num; unsigned i; vector<unsigned> vec; //vector容器应事先预留确定的大小,因为当前vector容量达最大而又有新的数据插入时,重新复制数据将非常耗时! vec.reserve(2e5); cin >> num; vec.push_back(0);//调整下标,可以不补 while (num--) { cin >> i; vec.push_back(i); } int sz = vec.size(); //看评论区发现if-else结构可以简化为一个顺序结构,学习了! if (sz % 2==0) { for (int k = sz-1; k >= 1; k -= 2) cout << vec[k] << " "; for (int k = 2; k <= sz - 2; k += 2) cout << vec[k] << " "; } else { for (int k = sz-1; k >= 2; k -= 2) cout << vec[k] << " "; for (int k = 1; k <= sz - 2; k += 2) cout << vec[k] << " "; } cout << endl; }
//首先这是一个规律题,他后面的n贼大,按他的思路走大概率超时//规律如下 //0 //0 1 1 0 //1 0 2 2 0 1 //2 0 1 3 3 1 0 2 //3 1 0 2 4 4 2 0 1 3 //4 2 0 1 3 5 5 3 1 0 2 4 //5 3 1 0 2 4 6 6 4 2 0 1 3 5 #include<iostream> #include<vector> using namespace std; int main(){ int n, m; while (cin >> n){ vector<int> a; for (int i = 0; i < n; i++){ cin >> m; a.push_back(m); } if (n == 1) cout << a[0] << endl; if (n % 2 == 1){ for (int i = n - 1; i >= 0; i-=2) cout << a[i] << " "; for (int i = 1; i < n; i+=2) cout << a[i] << " "; } else{ for (int i = n - 1; i >= 1; i -= 2) cout << a[i] << " "; for (int i = 0; i < n; i += 2) cout << a[i] << " "; } } return 0; }
按照题目的意思可以这样写
使用一个LinkedList保存数据,按照顺序分别从后和从前插入数据,之后按照插入数据的总数量,从前或者从后遍历输出就行了。
import java.util.*; public class Main{ public static void main(String[] args){ Scanner s= new Scanner(System.in); int len = s.nextInt(); LinkedList<Integer> list = new LinkedList<Integer>(); for(int i=0;i<len;){ list.add(s.nextInt()); i++; if(i<len){ list.addFirst(s.nextInt()); i++; } } s.close(); Iterator itr; if(len%2==0) itr = list.iterator(); else itr = list.descendingIterator(); if(itr.hasNext())System.out.print(itr.next()); while(itr.hasNext()){ System.out.print(" "+itr.next()); } } }