NC214802. Whereisthebilliardballgoing?
描述
Suppose that the lower left corner of the billiard table is the coordinate origin (0, 0), and the radius of the billiard ball is r. It is known that the starting position of the billiard ball center is (px, py) and the initial velocity is (vx, vy).
Please calculate: after time t, where is the billiard ball going?
输入描述
The first line contains one integer T(T ≤ 1,000), the number of test cases.For each test case, there are eight integers in one line in the following format:L W r px py vx vy t(10≤ L,W ≤ 1,024 , 1≤r<5 , r≤px≤L-r , r≤py≤W-r , -1,000≤vx,vy≤1,000 , 0≤t≤109)
输出描述
For each case, your program will output the position of the billiard ball center after time t in one line.The position is represented by two integer coordinates separated by a space.
示例1
输入:
4 200 100 1 100 50 1 1 1 200 100 1 100 50 1 0 1000 200 200 1 100 100 1 1 1000 100 100 1 1 1 2 3 10000
输出:
101 51 90 50 90 90 9 13
Python3 解法, 执行用时: 36ms, 内存消耗: 4960K, 提交时间: 2021-12-23 19:58:02
T=eval(input()) m=[] for i in range(T): i=input().split() m.append(i) for i in m: L=int(i[0]) W=int(i[1]) r=int(i[2]) px=int(i[3]) py=int(i[4]) vx=int(i[5]) vy=int(i[6]) t=int(i[7]) x2=L-2*r y2=W-2*r px=px-r py=py-r px=(px+vx*t)%(2*x2) py=(py+vy*t)%(2*y2) px=px+2*x2 if px<0 else px py=py+2*y2 if py<0 else py px=2*x2-px if px>x2 else px py=2*y2-py if py>y2 else py print('%d %d'%(px+r,py+r))
C++(g++ 7.5.0) 解法, 执行用时: 5ms, 内存消耗: 376K, 提交时间: 2023-07-17 15:38:57
#include<bits/stdc++.h> using namespace std; typedef long long ll; ll l,w,r,vx,vy,t,x,y; int main() { int n,k; scanf("%d",&n); while(n--) { cin>>l>>w>>r>>x>>y>>vx>>vy>>t; l-=2*r,w-=2*r,x-=r,y-=r; x+=t*vx; x=x%(2*l); if(x<0) x+=2*l; if(x>l) x=2*l-x; y+=t*vy; y=y%(2*w); if(y<0) y+=2*w; if(y>w) y=2*w-y; cout<<x+r<<" "<<y+r<<endl; } }
C(clang11) 解法, 执行用时: 3ms, 内存消耗: 352K, 提交时间: 2021-01-28 19:24:03
#include<stdio.h> int place(int L,int X0,int r,int v,int t){ int l=L-2*r; int m=2*l; int x0=X0-r; int x=(x0+(v%m)*(t%m))%m;//物理,直线方程,并注意用法 if(x<0){ x=-x; } if(x>l){ x=m-x; } return x; } int main(){ int i; scanf("%d",&i); for(int k=0;k<i;k++){ int L,W,r,x0,y0,vx,vy,t; scanf("%d %d %d %d %d %d %d %d",&L,&W,&r,&x0,&y0,&vx,&vy,&t);//注意有8个%d printf("%d %d\n",place(L,x0,r,vx,t)+r,place(W,y0,r,vy,t)+r); } }
C++(clang++11) 解法, 执行用时: 3ms, 内存消耗: 376K, 提交时间: 2020-12-28 20:16:05
#include<cstdio> int l,w,r,vx,vy,t; long long x,y; int main() { int n,k; scanf("%d",&n); while(n--) { scanf("%d %d %d %lld %lld %d %d %d",&l,&w,&r,&x,&y,&vx,&vy,&t); l-=2*r; w-=2*r; x-=r; y-=r; x+=t*vx; x=x%(2*l); if(x<0) x+=2*l; if(x>l) x=2*l-x; y+=t*vy; y=y%(2*w); if(y<0) y+=2*w; if(y>w) y=2*w-y; printf("%lld %lld\n",x+r,y+r); } }