一道简单的几何变换

Description


小光最近在学习几何变换,老师给他留了一个作业,在二维平面上有n个点(x,y),老师给了m个几何变换对n个点进行操作,要求小光输出变换后的n个点的坐标(x,y)。小光为了偷懒,请求你帮他写个程序来完成老师的作业。

   由于小光刚刚学习几何变换,老师只会给出四种变换,如下:

   平移变换: (x,y)=(x+p,y+q)   程序的输入格式为:1 p q  (p,q为整数

   缩放变换: (x,y)=(x*L,y*L)    程序的输入格式为:2 L    (L为整数)

   上下翻转: (x,y)=(x,-y)       程序的输入格式为:3

   左右翻转: (x,y)=(-x,y)       程序的输入格式为:4



Input


N(1<=N<=10^5)

   然后N个点(x,y)  其中x,y均为整数

   M  (1<=M<=10^5)

   然后M个变换,输入格式如上所述。


Output


N个点的坐标


Sample Input


2
1 1
2 2
1
1 1 1

Sample Output


2 2
3 3

Hint


注意同一组数据中每个点进行的变换都相同。


Source


安徽省2014年“京胜杯”大学生程序设计竞赛


思路:一开始的想法是对每个坐标全部操作,然后超时,然后改用操作坐标的系数,最后全部乘一遍即可

这题最让我郁闷的就是系数和坐标用long long 操作就WA,不是怕乘的太大了嘛,然后改成int就能过,实在是对合工大oj的测试数据表示不解,用long long在合工大宣城校区oj上提交都过了的,不能忍啊。

  1. /**AC 
  2.  * Project Name: 省赛 
  3.  * File Name: J.一道简单的几何变换.cpp 
  4.  * Created on: 2015年4月28日 下午3:35:33 
  5.  * Author: jtahstu 
  6.  * QQ: 1373758426 E-mail:1373758426@qq.com 
  7.  * Copyright (c) 2015, jtahstu , All Rights Reserved. 
  8.  */  
  9. //2  
  10. //1 1  
  11. //2 2  
  12. //1  
  13. //1 1 1  
  14. #include<iostream>  
  15. #include<cstdio>  
  16. #include<cmath>  
  17. #include<algorithm>  
  18. #include<string>  
  19. #include<cstring>  
  20. using namespace std;  
  21. struct point{  
  22.     int x;  
  23.     int y;  
  24. }a[10005];  
  25. int main(){  
  26.         int m,n,b,p,q,l;  
  27.         while(scanf("%d",&n)==1){  
  28.             int a1=1,b1=0,c1=1,d1=0;  
  29.         for(int i=0;i<n;i++)  
  30.             scanf("%d%d",&a[i].x,&a[i].y);  
  31.         scanf("%d",&m);  
  32.         for(int i=0;i<m;i++){  
  33.             scanf("%d",&b);  
  34.             if(b==1){  
  35.                 scanf("%d%d",&p,&q);  
  36.                 b1+=p;  
  37.                 d1+=q;  
  38.             }  
  39.             else if(b==2){  
  40.                 scanf("%d",&l);  
  41.                 a1*=l;  
  42.                 b1*=l;  
  43.                 c1*=l;  
  44.                 d1*=l;  
  45.             }  
  46.             else if(b==3){  
  47.                 c1=-c1;  
  48.                 d1=-d1;  
  49.             }  
  50.             else if(b==4){  
  51.                 a1=-a1;  
  52.                 b1=-b1;  
  53.             }  
  54.         }  
  55.         for(int i=0;i<n;i++)  
  56.             printf("%d %d\n",a1*a[i].x+b1,c1*a[i].y+d1);  
  57.     }  
  58.     return 0;  
  59. }  
  1. /**超时 
  2.  * Project Name: 省赛 
  3.  * File Name: J.一道简单的几何变换.cpp 
  4.  * Created on: 2015年4月28日 下午3:35:33 
  5.  * Author: jtahstu 
  6.  * QQ: 1373758426 E-mail:1373758426@qq.com 
  7.  * Copyright (c) 2015, jtahstu , All Rights Reserved. 
  8.  */  
  9. #include<iostream>  
  10. #include<cstdio>  
  11. #include<cmath>  
  12. #include<algorithm>  
  13. #include<string>  
  14. #include<cstring>  
  15. using namespace std;  
  16. struct point{  
  17.     long long x;  
  18.     long long y;  
  19. }a[100000];  
  20. int main()  
  21. {  
  22.     int m,n,b,p,q,l;  
  23.     while(scanf("%d",&n)==1){  
  24.     for(int i=0;i<n;i++)  
  25.         scanf("%lld%lld",&a[i].x,&a[i].y);  
  26.     scanf("%d",&m);  
  27.     for(int i=0;i<m;i++){  
  28.         scanf("%d",&b);  
  29.         if(b==1){  
  30.             scanf("%d%d",&p,&q);  
  31.             for(int i=0;i<n;i++){  
  32.                 a[i].x+=p;  
  33.                 a[i].y+=q;  
  34.             }  
  35.         }  
  36.         else if(b==2){  
  37.             scanf("%d",&l);  
  38.             for(int i=0;i<n;i++){  
  39.                 a[i].x*=l;  
  40.                 a[i].y*=l;  
  41.             }  
  42.              
  43.         }  
  44.         else if(b==3){  
  45.             for(int i=0;i<n;i++)  
  46.                 a[i].y*=-1;  
  47.              
  48.         }  
  49.         else{  
  50.             for(int i=0;i<n;i++)  
  51.                 a[i].x*=-1;  
  52.              
  53.         }  
  54.     }  
  55.     for(int i=0;i<n;i++)  
  56.         printf("%lld %lld\n",a[i].x,a[i].y);  
  57.     }  
  58.     return 0;  
  59. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值