PAT1008数组元素循环右移问题(2,3测试点不过的解决办法)

简介: PAT1008数组元素循环右移问题(2,3测试点不过的解决办法)

PATB 1008

本篇描述三种解题代码供读者参考

第一种:使用大数组,控制输出,用截断的方法

不要犯笔者的错误,笔者一开始理解错了题意,想成了左移,已注释,也粘贴了上来

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int A[1000000000];
//下为左移
/*int main(int argc, char **argv){
        int step = 0, count = 0;
        scanf("%d %d",&count,&step);
        for(int i = 0; i < count; i++){
                scanf("%d",&A[i]);
        }
        int len = count + step;
        for(int i = 0; i < step; i++){
                A[count+i] = A[i];
        }
        for(int i = step; i < len; i++){
                printf("%d",A[i]);
                if(i!=len-1){
                        printf(" ");
                }
        }
}*/
int main(int argc, char **argv){
        int count = 0, step = 0;
        scanf("%d %d",&count,&step);
        step = step % count;//过2,3数据点的关键
        for(int i = 0; i < count; i++){
                scanf("%d",&A[i + step]);
        }
        for(int i = 0; i < step; i++){
                A[i] = A[count+i];
        }
        for(int i = 0; i < count; i++){
                printf("%d",A[i]);
                if(i!=count-1){
                        printf(" ");
                }
        }
}

注意笔者,一开始没有添加注释中所写的关键步骤,导致2,3数据点一直不过,下同,加上即可

第二种,矩阵转置的思路,(B,A)=(A^-1, B^-1 )^-1

#include <bits/stdc++.h>
using namespace std;
void reverse(int R[], int left, int right);
int main(int argc, char **argv){
        int m, n;
        scanf("%d %d", &n, &m);
        int arr[n];
        for(int i = 0; i < n; i++){
                scanf("%d",&arr[i]);
        }
        m = m % n;
        reverse(arr, 0, n-m-1);
        reverse(arr, n-m, n-1);
        reverse(arr, 0, n-1);
        for(int i = 0; i < n-1; i++){
                printf("%d ",arr[i]);
        }
        printf("%d",arr[n-1]);
        return 0;
}
void reverse(int R[], int left, int right){
        int i = left, j = right, temp;
        while(i < j){
                temp = R[i];
                R[i] = R[j];
                R[j] = temp;
                i++;
                j--;
        }
}

第三种,其他网友参考答案,与笔者的第一种方法相反,控制输入,这种方法更巧妙一些

#include <cstdio>
#include <bits/stdc++.h>
using namespace std;
//在输入上下手,高招
int main(int argc, char **argv){
        int m, n;
        scanf("%d %d",&n,&m);
        m = m % n;
        int a[n];
        for(int i = m; i <= n-1; i++){
                scanf("%d ",&a[i]);
        }
        for(int i = 0; i<=m-1; i++){
                scanf("%d",&a[i]);
        }
        for(int i = 0; i <n-1; i++)
                printf("%d ",a[i]);
        printf("%d",a[n-1]);
        return 0;
}


相关文章
|
2月前
|
Perl
在awk中,初始化-测试-递增型的 `for` 循环
在awk中,初始化-测试-递增型的 `for` 循环
17 2
|
6月前
|
Shell Linux Go
《Linux操作系统编程》第八章 Shell程序设计: shell 语言结构,包括测试、分支、循环、跳转、函数、语句组
《Linux操作系统编程》第八章 Shell程序设计: shell 语言结构,包括测试、分支、循环、跳转、函数、语句组
67 0
|
8月前
【单片机期中测试】3.按键控制流水灯循环
【单片机期中测试】3.按键控制流水灯循环
117 0
|
10月前
|
PHP
PHP递归和循环的速度测试
写了一个可以对 $_GET`, `$_POST 等输入进行过滤的函数,做了PHP递归和循环的速度测试。
55 0
|
测试技术 索引 Python
软件测试|最全的Python for循环和while循环使用介绍
软件测试|最全的Python for循环和while循环使用介绍
116 0
|
缓存 Java Spring

热门文章

最新文章