J. 一道簡單的幾何變換
Time Limit: 1000 MS | Memory Limit: 65536 KB |
Total Submissions: 69 | Accepted: 15 |
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
1 1
2 2
1
1 1 1
Sample Output
3 3
Hint
注意同一組數據中每個點進行的變換都相同。
Source
安徽省2014年“京勝杯”大學生程序設計競賽
思路:一開始的想法是對每個座標全部操作,然後超時,然後改用操作座標的係數,最後全部乘一遍即可。
這題最讓我鬱悶的就是係數和座標用long long 操作就WA,不是怕乘的太大了嘛,然後改成int就能過,實在是對合工大oj的測試數據表示不解,用long long在合工大宣城校區oj上提交都過了的,不能忍啊。
/**AC
* Project Name: 省賽
* File Name: J.一道簡單的幾何變換.cpp
* Created on: 2015年4月28日 下午3:35:33
* Author: jtahstu
* QQ: 1373758426 E-mail:[email protected]
* Copyright (c) 2015, jtahstu , All Rights Reserved.
*/
//2
//1 1
//2 2
//1
//1 1 1
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstring>
using namespace std;
struct point{
int x;
int y;
}a[10005];
int main(){
int m,n,b,p,q,l;
while(scanf("%d",&n)==1){
int a1=1,b1=0,c1=1,d1=0;
for(int i=0;i<n;i++)
scanf("%d%d",&a[i].x,&a[i].y);
scanf("%d",&m);
for(int i=0;i<m;i++){
scanf("%d",&b);
if(b==1){
scanf("%d%d",&p,&q);
b1+=p;
d1+=q;
}
else if(b==2){
scanf("%d",&l);
a1*=l;
b1*=l;
c1*=l;
d1*=l;
}
else if(b==3){
c1=-c1;
d1=-d1;
}
else if(b==4){
a1=-a1;
b1=-b1;
}
}
for(int i=0;i<n;i++)
printf("%d %d\n",a1*a[i].x+b1,c1*a[i].y+d1);
}
return 0;
}
/**超時
* Project Name: 省賽
* File Name: J.一道簡單的幾何變換.cpp
* Created on: 2015年4月28日 下午3:35:33
* Author: jtahstu
* QQ: 1373758426 E-mail:[email protected]
* Copyright (c) 2015, jtahstu , All Rights Reserved.
*/
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstring>
using namespace std;
struct point{
long long x;
long long y;
}a[100000];
int main()
{
int m,n,b,p,q,l;
while(scanf("%d",&n)==1){
for(int i=0;i<n;i++)
scanf("%lld%lld",&a[i].x,&a[i].y);
scanf("%d",&m);
for(int i=0;i<m;i++){
scanf("%d",&b);
if(b==1){
scanf("%d%d",&p,&q);
for(int i=0;i<n;i++){
a[i].x+=p;
a[i].y+=q;
}
}
else if(b==2){
scanf("%d",&l);
for(int i=0;i<n;i++){
a[i].x*=l;
a[i].y*=l;
}
}
else if(b==3){
for(int i=0;i<n;i++)
a[i].y*=-1;
}
else{
for(int i=0;i<n;i++)
a[i].x*=-1;
}
}
for(int i=0;i<n;i++)
printf("%lld %lld\n",a[i].x,a[i].y);
}
return 0;
}