硬币分类


package com.main;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class CoinCount {
private int totalaccount = 200;
private int[] values;
private static final int SORT_DIRECTION_ASCENDING = 0x1000;
private static final int SORT_DIRECTION_DESCENDING = 0x1001;
private Map<Integer, Integer> resultList = new HashMap();

public int gettotalaccount() {
return totalaccount;
}

public void settotalaccount(int tOTALACCOUNT) {
totalaccount = tOTALACCOUNT;
}

public int[] getValues() {
return values;
}

public void setValues(int[] values) {
this.values = values;
}

public void calculate() {
initValues();
// values = new int[] { 46, 42, 42, 39, 27, 25, 9, 6, 1, 1 };
values = sort(values, SORT_DIRECTION_DESCENDING);
System.out
.println("the target is :" + totalaccount + "\nthe input is:");
for (int i = 0; i < values.length; i++) {
System.out.print(values[i] + " ");
}
System.out.println();
find(0, 0, 0);
if (check()) {
// 结果符合要求
System.out.println("the result is :");
for (int i = 0; i < resultList.size(); i++) {
System.out.println(values[i] + "==>" + resultList.get(i));
}
} else {
System.out.println("none result at all");
}
}

private int find(int depth, int count, int totalCount) {
// 边界条件
if (depth >= values.length)
return 0;
int value = values[depth];
if (value + totalCount == totalaccount) {
resultList.put(depth, ++count);
return 0;
} else if (value > totalaccount) {
return find(++depth, 0, totalCount);
} else if (value + totalCount > totalaccount) {
if (count < 1) {
// 向下回溯
resultList.put(depth, 0);
value = find(++depth, 0, totalCount);
return value;
} else {
resultList.put(depth, count);
return find(++depth, 0, totalCount);
}
}
totalCount += value;
return find(depth, ++count, totalCount);
}

/**
* 初始化价格数组——纯粹是为了演示方便
*/
private void initValues() {
values = new int[10];
for (int i = 0; i < values.length; i++) {
values[i] = (int) (Math.random() * 50 + 1);
}
}

/**
* 数组排序
*
* @param arr
* @param direction
* 排序方向 升序||降序
* @return
*/
public int[] sort(int arr[], int direction) {
for (int i = 0; i < arr.length; i++) {
for (int j = i + 1; j < arr.length; j++) {
// 判断是升序还是降序
boolean flag = false;
if (direction == SORT_DIRECTION_ASCENDING) {
flag = (values[j] < values[i]);
} else if (direction == SORT_DIRECTION_DESCENDING) {
flag = (values[j] > values[i]);
}
if (flag) {
int temp = values[i];
values[i] = values[j];
values[j] = temp;
}
}
}
return values;
}

/**
* 检查结果是否正确
*
* @return
*/
private boolean check() {
boolean flag = false;
int sum = 0;
for (int i = 0; i < resultList.size(); i++) {
sum += resultList.get(i) * values[i];
}
if (sum == totalaccount)
flag = true;
return flag;
}
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
%清空工作空间中的所有变量和命令窗口内容 clc; clear all; %打开文件选择对话框,选择需要处理的图片 [filename,pathname]=uigetfile({'*.jpg;*.tif;*.png;*gif','all imagine files';'*.*','all files'},'select your photo'); %获取图片路径 path=[pathname,filename]; %读取图片 image=imread(path); %显示图片 imshow(image); %图片处理 %将RGB图像转换为灰度图像 I=rgb2gray(image); %将灰度图像进行滤波操作 I=rangefilt(I); %使用形态学开运算估计背景 background = imopen(I,strel('disk',11)); %从原始图像中减去背景图像 I2 = I-background; %增强对比度 I3 = imadjust(I2); %阈值分割,生成二值图像 bw = imbinarize(I3); %降噪 bw = bwareaopen(bw,160); %进行边缘检测 bw=edge(bw,'canny'); %显示二值图像 imshow(bw); %生成结构元素 se=strel('square',15); %闭运算 bw1=imclose(bw,se); %膨胀 bw2=imdilate(bw1,se); %腐蚀 bw2=imerode(bw2,se); %填充孔洞 bw3=imfill(bw2,'holes'); %显示填充后的二值图像 imshow(bw3); %定义硬币半径取值范围 rmin = 20; rmax = 2500; radiusRange=[rmin rmax]; %使用Hough变换检测圆形目标,返回检测到的圆心坐标和半径大小 [center, rad] = imfindcircles(bw3,radiusRange,'EdgeThreshold',0.13); %显示检测到的圆形目标 imshow(bw3); viscircles(center, rad,'Color','b'); %初始化硬币个数 one=0; half=0; little=0; %对检测到的圆形目标进行分类 [m,n]=size(rad); num=m; i=1; j=num; min=rad(i); max=rad(j); while i<=j if rad(i)<rad(j) if rad(i)<min min=rad(i); else if rad(j)<max max=rad(j); end end else if rad(j)<min min=rad(j); else if rad(i)<max max=rad(i); end end end i=i+1; j=j-1; end sum=0; for i=1:num sum=rad(i)+sum; end ave=(sum-(min+max))/(num-2); for i=1:num if 0.6<(rad(i)/ave)&&(rad(i)/ave)<1.5 if rad(i)>ave one=one+1; else if 0.96<(rad(i)/ave) && rad(i)<=ave half=half+1; else little = little+1; end end end end %计算硬币总价值 sum=half*0.5+one+little*0.1; %显示硬币分类结果 one half little sum 这个程序的不足之处是什么
最新发布
06-12

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值