
例如,用一串二极管的开关变化,记录一段二进制数码。
这段数码记录了一幅图像,这就需要一个算法。是输入设备的算法。例如摄像头的算法。
然后这段数码转换为显示屏二极管整列的相应开关动作,就显示出了那段数码所记录的图像。
其中的转换又是一种算法。输出设备算法。
若没有这样的硬件动作变化,任何纸上写出来的算法都不能被应用。
按你说的“实现”,那就不能被实现。
而程序分两种:
一种叫做应用程序。依赖 *** 作系统,并不直接 *** 作硬件动作。尽管其中也有许多算法。
一种叫做 *** 作系统。它才是直接 *** 作硬件的程序。任何算法都必须落实在 *** 作系统上,
才可以得到最终的硬件变化结果。
所以,“算法必须最终由计算机程序实现”这句话也算对吧。 *** 作系统也是程序。
只是并不最终。
最终必须由硬件动作实现。
DBSCAN是一种基于密度的聚类算法 它的基本原理就是给定两个参数 ξ和minp 其中 ξ可以理解为半径 算法将在这个半径内查找样本 minp是一个以ξ为半径查找到的样本个数n的限制条件 只要n>=minp 查找到的样本点就是核心样本点 算法的具体描述见参考文件 下边是这个算法的java实现
首先定义一个Point类 代表样本点
<! [endif] >
package sunzhenxing
public class Point {
private int x
private int y
private boolean isKey
private boolean isClassed
public boolean isKey() {
return isKey
}
public void setKey(boolean isKey) {
this isKey = isKey
this isClassed=true
}
public boolean isClassed() {
return isClassed
}
public void setClassed(boolean isClassed) {
this isClassed = isClassed
}
public int getX() {
return x
}
public void setX(int x) {
this x = x
}
public int getY() {
return y
}
public void setY(int y) {
this y = y
}
public Point(){
x=
y=
}
public Point(int x int y){
this x=x
this y=y
}
public Point(String str){
String[] p=str split( )
this x=Integer parseInt(p[ ])
this y=Integer parseInt(p[ ])
}
public String print(){
return <+this x+ +this y+ >
}
}
然后定义一个工具类 为算法的实现服务
package sunzhenxing
import java io BufferedReader
import java io FileReader
import java io IOException
import java util *
public class Utility {
/**
* 测试两个点之间的距离
* @param p 点
* @param q 点
* @return 返回两个点之间的距离
*/
public static double getDistance(Point p Point q){
int dx=p getX() q getX()
int dy=p getY() q getY()
double distance=Math sqrt(dx*dx+dy*dy)
return distance
}
/**
* 检查给定点是不是核心点
* @param lst 存放点的链表
* @param p 待测试的点
* @param e e半径
* @param minp 密度阈值
* @return 暂时存放访问过的点
*/
public static List<Point>isKeyPoint(List<Point>lst Point p int e int minp){
int count=
List<Point>tmpLst=new ArrayList<Point>()
for(Iterator<Point>it=erator()it hasNext()){
Point q=it next()
if(getDistance(p q)<=e){
++count
if(!ntains(q)){
tmpLst add(q)
}
}
}
if(count>=minp){
p setKey(true)
return tmpLst
}
return null
}
public static void setListClassed(List<Point>lst){
for(Iterator<Point>it=erator()it hasNext()){
Point p=it next()
if(!p isClassed()){
p setClassed(true)
}
}
}
/**
* 如果b中含有a中包含的元素 则把两个集合合并
* @param a
* @param b
* @return a
*/
public static boolean mergeList(List<Point>a List<Point>b){
boolean merge=false
for(int index= index<b size()++index){
if(ntains(b get(index))){
merge=true
break
}
}
if(merge){
for(int index= index<b size()++index){
if(!ntains(b get(index))){
a add(b get(index))
}
}
}
return merge
}
/**
* 返回文本中的点集合
* @return 返回文本中点的集合
* @throws IOException
*/
public static List<Point>getPointsList() throws IOException{
List<Point>lst=new ArrayList<Point>()
String txtPath= src\\\\sunzhenxing\\points txt
BufferedReader br=new BufferedReader(new FileReader(txtPath))
String str=
while((str=br readLine())!=null &&str!= ){
lst add(new Point(str))
}
br close()
return lst
}
}
最后在主程序中实现算法 如下所示
package sunzhenxing
import java io *
import java util *
public class Dbscan {
private static List<Point>pointsList=new ArrayList<Point>()//存储所有点的集合
private static List<List<Point>>resultList=new ArrayList<List<Point>>()//存储DBSCAN算法返回的结果集
private static int e= //e半径
private static int minp= //密度阈值
/**
* 提取文本中的的所有点并存储在pointsList中
* @throws IOException
*/
private static void display(){
int index=
for(Iterator<List<Point>>it=erator()it hasNext()){
List<Point>lst=it next()
if(lst isEmpty()){
continue
}
System out println( 第 +index+ 个聚类 )
for(Iterator<Point>it =erator()it hasNext()){
Point p=it next()
System out println(p print())
}
index++
}
}
//找出所有可以直达的聚类
private static void applyDbscan(){
try {
pointsList=Utility getPointsList()
for(Iterator<Point>it=erator()it hasNext()){
Point p=it next()
if(!p isClassed()){
List<Point>tmpLst=new ArrayList<Point>()
if((tmpLst=Utility isKeyPoint(pointsList p e minp)) != null){
//为所有聚类完毕的点做标示
Utility setListClassed(tmpLst)
resultList add(tmpLst)
}
}
}
} catch (IOException e) {
// TODO Auto generated catch block
e printStackTrace()
}
}
//对所有可以直达的聚类进行合并 即找出间接可达的点并进行合并
private static List<List<Point>>getResult(){
applyDbscan()//找到所有直达的聚类
int length=resultList size()
for(int i= i<length++i){
for(int j=i+ j<length++j){
if(rgeList(resultList get(i) resultList get(j))){
resultList get(j) clear()
}
}
}
return resultList
}
/**
* 程序主函数
* @param args
*/
public static void main(String[] args) {
getResult()
display()
//System out println(Utility getDistance(new Point( ) new Point( )))
}
}
下边是一个小测试 即使用src\\\\sunzhenxing\\points txt文件的内容进行测试 points txt的文件内容是
最后算法的结果是
第 个聚类
<>
<>
<>
<>
<>
<>
<>
<>
<>
<>
<>
第 个聚类
<>
<>
<>
<>
<>
<>
<>
lishixinzhi/Article/program/Java/hx/201311/26957思路分析:本题考查进位制的换算步骤及注意事项.利用把k进制数转化为十进制数的一般方法就可以把8进制数314
706(8)化为十进制数,然后根据该算法,利用GRT函数,应用循环语句可以设计程序.
解:314
706(8)=3×85+1×84+4×83+7×82+0×81+6×80=104
902.
所以,314
706(8)化为十进制数是104
902.
8进制数314
706中共有6位,因此可令a=314
706,k=8,n=6.
程序如下:
INPUT
a,k,n
i=1
b=0
WHILE
i<=n
t=GET(a[i])
b=b+t*k^(i-1)
i=i+1
WEND
b
END
方法归纳
在上述程序中,输入a,k,n的值分别为314
706,8,6即可转换为十进制数b.将其部分改变一下就可以把任何一个k进位数a(共有n位)转化为十进制数b,只要输入相应的a,k,n的值即可.
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)