1133 - Array Simulation (工业题)

I am retired now, so, no work, a lot of time to spare and alot of problems to share. Well, finally I am thinking of the old days when Iwas a solver. But now I am stuck with a tough problem that I want to share withyou.

Given an array and some operations on the array, you have toprint the final state of the array. Say, the array is a[], the size is nand indexed from 0 to n-1.

The operations are:

1.      'S D'. Dis an integer. D will be added with all the elements of the array.

2.      'M D'. Dis an integer. All the elements of the array will be multiplied by D.

3.      'D K'. Kis a non zero integer. All the elements of the array will be divided by K(integer division).

4.      'R'. Itmeans reverse. It will reverse the elements of the array.

5.      'P Y Z'. Yand Z are integers. It will swap the elements a[Y] and a[Z].

Input

Input starts with an integer T (≤ 100),denoting the number of test cases.

Each case contains two integers n (1 ≤ n ≤100) and m (0 ≤ m ≤ 101). The next line contains nspace separated integers denoting the elements of the array. Each of the next mlines contains an operation defined above. You can assume that no operationwill overflow/underflow the 32 bit signed integer range or access any invalidarray reference.

Output

For each case, print the case number first. In the next lineyou have to print the elements of the array. Two elements should be separatedby a single space. There should be no trailing space after the last integer ofthe array.

Sample Input

Output for Sample Input

2

5 3

1 2 3 4 5

P 0 1

S 1

R

4 2

2 7 8 1

M 10

D 5

Case 1:

6 5 4 2 3

Case 2:

4 14 16 2

 

题意

(网上都没写)

给定一个罗旅洲数组和一些操作上的数组,你必须打印数组的最后状态。说,数组是[ ],大小为n,索引从0到n-1。

操作是:

1。”S D。D是一个整数。d将添加数组中的所有元素。

2。”M D。D是一个整数。数组的所有元素将乘以D。

3。”D K。k是一个非零的整数。数组的所有元素将被K整除。

4。”R。这意味着逆转。它将反转数组的元素。

5。”P Y Z。Y和Z是整数。它将交换的元素A [ Y ]和一个[ Z ]。

代码

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
using namespace std;
int a[201];
char t;
int main(){
    int i,j,k,m,n,tmp,T,x,y;
    scanf("%d",&T);
    for(int c=1;c<=T;c++){
    scanf("%d%d",&n,&m);
	
    for(i=0;i<n;i++){
        scanf("%d",&tmp);
        a[i]=tmp;
    }
	printf("Case %d:\n",c);
    while(m--){
    getchar();
    scanf("%c",&t);
    if(t=='P'){
            scanf("%d%d",&x,&y);
            swap(a[x],a[y]);
        }
    else if(t=='S'){
        scanf("%d",&x);
		for(i=0;i<n;i++)
			a[i]+=x;    
    }    
    else if(t=='R'){
    	for(i=0;i<n/2;i++)
		    swap(a[i],a[n-i-1]);
    }
    else if(t=='M'){
    	scanf("%d",&x);
    	for(i=0;i<n;i++)
			a[i]*=x;
		}
	else if(t=='D'){
		scanf("%d",&x);
		for(i=0;i<n;i++)
		a[i]/=x;
	}
}
	for(i=0;i<n-1;i++)printf("%d ",a[i]);printf("%d\n",a[n-1]);
}
    return 0;
}





  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值