当前位置: 首页 > 工具软件 > go-freeling > 使用案例 >

Go for it !

公孙志
2023-12-01

NO.1

****************************************************************

package org.bian;

/*
 * 求数组中比平均数大的个数
 * 入口方法    int avgNum(int arg[])
 * @author Bian
*/
public class Test1 {
	
	public static void main(String[] args) {
		int []a={2,4,6,8};
		Test1 test =new Test1();
		System.out.println(" the num is "+ test.avgNum(a) );
	}

	public int avgNum(int[]arg) {
		int num = 0;
		double ave;
		int  sum = 0;
		for(int i=0;i<arg.length;i++){
			sum+=arg[i];
		}
		ave=sum/arg.length;
		for(int i=0;i<arg.length;i++){
			if(arg[i]>ave){
				num++;
			}
		}
		return num;
	}

}

NO.2

*******************************************************************

package org.bian;

import java.math.BigInteger;

/*
 * 两大数相加。
       用java.math.BigInteger类
        注:必须是正整数相加
 */

public class Test2 {

	public static void main(String[] args) {
		Test2 test =new Test2();
		System.out.println("the sum is"+test.plusBigData("17626673838393", "73634667489299"));
	}
	
	public String plusBigData(String arg1, String arg2){
		BigInteger big1 = new BigInteger(arg1);  // 类型怎么转换 
		BigInteger big2 = new BigInteger(arg2);
		return big1.add(big2).toString();
		
	}

}


NO.3

**********************************************************************

package org.bian;

/**求字符串中最大回文字符串
 * 
 * 如输入ababbac 输出abba
 * @author bian
 * 
 * 思路:
 * 
 * 外层循环,当字符串不是回文字时,逐个减少字符串,即  i取值范围[0,len)
 * 
 * 内层循环的边界确定:剪辑下来的substring的长度是 len-i,要遍历i+1种可能的substring,
 * 所以j取值范围 [0,i+1),  长度为len-i的substring (j,len-i+j)
 * 
 */
public class Test3 {


	public static void main(String [] args){
	String str ="ahbdbdkdbd";
	Test3 test = new Test3();
	System.out.println(test.getString(str));
}
	public String getString(String str){
		
		for(int i=0;i<str.length();i++){
			
			for(int j=0;j<i+1;j++){
				
				int len=str.length();
				String sub =str.substring(j, len-i+j);
				StringBuffer subtemp=new StringBuffer(sub);    //subtemp 是变长的,所以用StringBuffer
				String subreverse =subtemp.reverse().toString();  //调用StingBuffer中的reverse方法,再用toString转换成String类型
				if(sub.equals(subreverse)){   //if(sub==subreverse)条件不行,要调用equals方法
					return sub;
				}
			}
		}
		return "无回文字串";
		
	}

}


 类似资料:

相关阅读

相关文章

相关问答