编程练习:Transmitters
来源:互联网 发布:绝地求生优化太差 编辑:程序博客网 时间:2024/06/14 21:36
为何保持编程的兴趣,还是找一些代码来写
看了 http://poj.org/problem?id=1106, 分析思路如下:
1)把坐标系原点定位为圆心,把其他点转化为极坐标
2)从第一个点开始,逐个查找满足所有落在下面区域内的点:此点到原点为起始边,半径为参数所给的半圆围成区域
3)所有点都扫描后给出最大覆盖的点数
package com.pnp.hello;import java.awt.Color;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.geom.Arc2D;import javax.swing.JFrame;public class Hello {static class My extends JFrame { double circle_x = 250; double circle_y = 250; double circle_r = 35;double[][] in_points = new double[][]{{250, 280},{230, 270},{270, 270},{240, 230},{260, 230},{240, 290},{260, 290}};double[][] points = new double[in_points.length][2];double angles[] = new double[points.length];double dists[] = new double[points.length];int max_count=0; // max point countint begin_index=-1; // the begin point of half circleint draw_index=0; // for drawing animationvoid cal () {// trans coordinate to polar coordinatefor ( int i=0; i < points.length; i++ ) {points[i][0] = in_points[i][0] - circle_x;points[i][1] = in_points[i][1] - circle_y;angles[i] = Math.atan2(points[i][1], points[i][0]);dists[i] = Math.sqrt( points[i][0] * points[i][0] + points[i][1] * points[i][1] );}// countfor ( int i=0; i < points.length; i++ ) {int count =0;for ( int j=0; j < points.length; j++) {double begin_a = angles[i];double end_a = begin_a + Math.PI;if ( angles[j] >= begin_a && angles[j] <= end_a && dists[j] < circle_r)count++;}if ( count > max_count ) {max_count = count;begin_index = i;}// for animations effectdraw_index = i;this.repaint();try {Thread.sleep(5000);} catch (InterruptedException e) {e.printStackTrace();}}this.repaint();draw_index = begin_index;System.out.printf("max_count: " + max_count + " begin_index: " + begin_index);}public void paint(Graphics g) {g.clearRect(0, 0, 500, 500);double ag = angles[draw_index];Arc2D arc = new Arc2D.Double(circle_x-circle_r, circle_y-circle_r, circle_r*2, circle_r*2, ag/Math.PI * 180, 180 , Arc2D.PIE);Graphics2D g2d = (Graphics2D) g;g2d.fill(arc);g.setColor(Color.RED);for ( int i=0; i < in_points.length; i++ ) {g.drawLine((int)in_points[i][0],(int)in_points[i][1] ,(int)in_points[i][0] + 1 , (int)in_points[i][1] +1 );//g.drawLine((int)circle_x-circle_r,(int)circle_y,(int)circle_x + circle_r , (int)circle_y );}}}public static void main(String[] args) {My frame = new Hello.My();frame.setTitle("half circle cover points");frame.setBounds(0, 0, 500, 500);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setVisible(true);frame.cal();}}
0 0
- 编程练习:Transmitters
- Transmitters
- zoj1041 Transmitters
- ZOJ1041 Transmitters
- [ZOJ1041] Transmitters
- pku1106 transmitters
- POJ1106-Transmitters
- zoj1041 Transmitters
- ZOJ1041 Transmitters
- 1106 Transmitters
- 编程练习
- 编程练习
- 编程练习
- 编程练习
- 编程练习
- 编程练习
- 编程练习
- 编程练习
- RFC3550 RTP(real-time transport protocol)
- UITableView获取当前选中的cell
- 一点一点学xamarin.forms
- 第十四周OJ项目1-求矩阵对角线元素之和
- PowerCenter 使用日记
- 编程练习:Transmitters
- 五种开源协议的比较(BSD,Apache,GPL,LGPL,MIT)
- 使用NGUI 3.5.5创建高效的超大Scroll View
- ActionContext和ServletActionContext小结
- linux下进程间共享内存通信的问题
- Eclipse Tomcat配置/管理/调试指南
- Hduoj1312【广搜】
- SEL(去除警告)、Block(block深入研究、block回调--匿名函数)
- MACHINE_START-内核板级初始化实现机制(linux3.1.0)