标签:循环 ace 乙级 比较 can stream turn str span
#include<stdio.h> int main(){ int n,k; scanf("%d %d",&n,&k); k = k%n; int a[1000]; if(n == 1){ int t; scanf("%d",&t); printf("%d",t); } else{ int rear = n-1; int front = 0; for(int i = 0;i<n;i++){ scanf("%d",&a[i]); } front+=k; rear = rear-k+1; for(int i =rear;i<n;i++){ printf("%d ",a[i]); } int j = 0; while(j!=rear-1){ printf("%d ",a[j]); j++; } printf("%d",a[rear-1]);} return 0; }
上面的 写法其实是比较复杂的,是通过真正的对数组进行右移来实现,以下的写法是通过控制打印顺序来实现“右移”,不过值得注意的是:题目没有规定右移的大小,当右移大小超过数组大小的时候需要对右移的大小进行一次转换取模,使之小于数组大小,在进行打印输出。
1 #include <iostream> 2 #include<cstdio> 3 #include<algorithm> 4 using namespace std; 5 int main() 6 { 7 int m, n; 8 cin >> m >> n; 9 int a[110]; 10 n = n % m; 11 for (int i = 0; i < m; i++) { 12 cin >> a[i]; 13 } 14 for (int i = m - n; i < m; i++) { 15 cout << a[i]<<" "; 16 } 17 for (int i = 0; i < m - n; i++) { 18 cout << a[i]; 19 if (i != m - n - 1) { 20 cout << " "; 21 } 22 } 23 return 0; 24 }
标签:循环 ace 乙级 比较 can stream turn str span
原文地址:https://www.cnblogs.com/ZJU-LOSER/p/13339175.html