重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
楼主你好
我们提供的服务有:做网站、网站建设、微信公众号开发、网站优化、网站认证、遂溪ssl等。为1000+企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的遂溪网站制作公司
应你的要求 我只做了加一位的和加两位的
算法类代码如下:
public class CountSumClass {
private String number;
private String result = new String("");
public CountSumClass() {
number = new String();
}
public CountSumClass(String str) {
number = str;
}
public String getResult() {
return result;
}
public void addOne() {
int x,y;
for (int i = 0; i number.length()-1; i++) {
x = (int)number.charAt(i) - 48;
for(int j = i+1; jnumber.length(); j++) {
y = (int)number.charAt(j) - 48;
result += x+"+"+y+"="+(x+y)+"\n";
}
}
y=0;
for (int i = 0; inumber.length(); i++) {
x = (int)number.charAt(i) - 48;
y += x;
if(i == number.length()- 1) {
result += x+"="+y;
} else {
result += x+"+";
}
}
}
public void addTwo() {
int x,y,z;
result += "\n";
for (int i = 0; inumber.length()-1; i++) {
String substr = number.substring(i,i+2);
x = Integer.parseInt(substr);
z = (x%10) * 10 + (x/10);
for(int j=0; jnumber.length(); j++) {
if(j!=i j!=i+1) {
y = (int)number.charAt(j) - 48;
result += x+"+"+y+"="+(x+y)+"\n";
result += z+"+"+y+"="+(z+y)+"\n";
}
}
}
}
public String toString() {
return result;
}
}
希望能帮助你哈
package huda.laogao.ON_20121216;
import java.util.*;
public class GetEquation {
ArrayListInteger params = null;// 进行加减的数,长度设为n
int result = 0;// 结果
public GetEquation(ArrayListInteger params, int result) {
this.params = params;
this.result = result;
}
public void run() {
int size = params.size();
if (size == 1) {
if (params.get(0) == result)
System.out.println(result + "=" + result);
else
System.out.println("Invalid");
return;
}
int[][] matrix = getMatrix(size - 1);
int m = matrix.length;// 行数
int count = 0;// 记录符合结果的组合数
for (int i = 0; i m; i++) {
int now = params.get(0);// 进行加减的结果,初始为第一个值
// 从第二个数开始进行加或减,加的话就相当于该数乘上1,然后加到结果上,
// 减的就相当于该数乘上-1,然后加到结果上
for (int j = 1; j size; j++) {
now += matrix[i][j - 1] * params.get(j);
}
// System.out.println(now);
// 判断结果
if (now == result) {
count++;// 组合数加1
System.out.print(params.get(0));
for (int j = 1; j size; j++) {
if (matrix[i][j - 1] == 1)
System.out.print("+" + params.get(j));
else
System.out.print("-" + params.get(j));
}// for j
System.out.println("=" + result);
}// if
}// for i
if (count == 0)
System.out.println("Invalid");
}
public int[][] getMatrix(int n) {
int m = (int) Math.pow(2, n);
int matrix[][] = new int[m][n];
for (int i = 0; i n; i++) {
int num = (int) Math.pow(2, i + 1);
// 总共分 2^(i+1)块,如i=0,即该矩阵的第一列,可分为2块,上一块取1,下一块取-1
int size = (int) Math.pow(2, n - i - 1);
// 每块的大小为2^(n-i-1),如n=3,i=0,每块的大小为4,即前4个为1,后4个为-1
int flag = 1;// 先为+
for (int j = 0; j num; j++) {
for (int k = 0; k size; k++) {
matrix[k + j * size][i] = flag;
}
flag *= -1;
}
}
return matrix;
}
public void print(int[][] matrix) {
for (int i = 0; i matrix.length; i++) {
for (int j = 0; j matrix[i].length; j++) {
System.out.print(matrix[i][j] + "\t");
}
System.out.println();
}
}
public static void main(String args[]) {
ArrayListInteger params = new ArrayListInteger();
params.add(1);
params.add(2);
params.add(3);
params.add(4);
int result = 10;
GetEquation ge = new GetEquation(params, result);
ge.run();
}
}
运行结果为:1+2+3+4=10
注:难点为获取加减矩阵,可辅助print()函数帮助理解
望采纳
参考下面代码:
import java.util.Random;
public class BpDeep{
public double[][] layer;//神经网络各层节点
public double[][] layerErr;//神经网络各节点误差
public double[][][] layer_weight;//各层节点权重
public double[][][] layer_weight_delta;//各层节点权重动量
public double mobp;//动量系数
public double rate;//学习系数
public BpDeep(int[] layernum, double rate, double mobp){
this.mobp = mobp;
this.rate = rate;
layer = new double[layernum.length][];
layerErr = new double[layernum.length][];
layer_weight = new double[layernum.length][][];
layer_weight_delta = new double[layernum.length][][];
Random random = new Random();
for(int l=0;llayernum.length;l++){
layer[l]=new double[layernum[l]];
layerErr[l]=new double[layernum[l]];
if(l+1layernum.length){
layer_weight[l]=new double[layernum[l]+1][layernum[l+1]];
layer_weight_delta[l]=new double[layernum[l]+1][layernum[l+1]];
for(int j=0;jlayernum[l]+1;j++)
for(int i=0;ilayernum[l+1];i++)
layer_weight[l][j][i]=random.nextDouble();//随机初始化权重
}
}
}
//逐层向前计算输出
public double[] computeOut(double[] in){
for(int l=1;llayer.length;l++){
for(int j=0;jlayer[l].length;j++){
double z=layer_weight[l-1][layer[l-1].length][j];
for(int i=0;ilayer[l-1].length;i++){
layer[l-1][i]=l==1?in[i]:layer[l-1][i];
z+=layer_weight[l-1][i][j]*layer[l-1][i];
}
layer[l][j]=1/(1+Math.exp(-z));
}
}
return layer[layer.length-1];
}
//逐层反向计算误差并修改权重
public void updateWeight(double[] tar){
int l=layer.length-1;
for(int j=0;jlayerErr[l].length;j++)
layerErr[l][j]=layer[l][j]*(1-layer[l][j])*(tar[j]-layer[l][j]);
while(l--0){
for(int j=0;jlayerErr[l].length;j++){
double z = 0.0;
for(int i=0;ilayerErr[l+1].length;i++){
z=z+l0?layerErr[l+1][i]*layer_weight[l][j][i]:0;
layer_weight_delta[l][j][i]= mobp*layer_weight_delta[l][j][i]+rate*layerErr[l+1][i]*layer[l][j];//隐含层动量调整
layer_weight[l][j][i]+=layer_weight_delta[l][j][i];//隐含层权重调整
if(j==layerErr[l].length-1){
layer_weight_delta[l][j+1][i]= mobp*layer_weight_delta[l][j+1][i]+rate*layerErr[l+1][i];//截距动量调整
layer_weight[l][j+1][i]+=layer_weight_delta[l][j+1][i];//截距权重调整
}
}
layerErr[l][j]=z*layer[l][j]*(1-layer[l][j]);//记录误差
}
}
}
public void train(double[] in, double[] tar){
double[] out = computeOut(in);
updateWeight(tar);
}
}