1、求数组里比平均数大的数的个数。
int avgNum(int arg[])
package org.hhu.ztc;
public class Test1 {
public static void main(String args[]) {
System.out.println(new Test1().avgNum(new int[] { 1, 2, 3, 4, 5, 6, 7,
8, 9, 10 }));
}
public int avgNum(int s[]) {
int sum = 0;
int len = s.length;
int flag = 0;
double avg;
for (int i = 0; i < len; i++) {
sum = sum + s[i];
}
avg = (sum + 0.0) / len;
for (int i = 0; i < len; i++) {
if (s[i] >= avg)
flag++;
}
return flag;
}
}
2、两大数相加。
用java.math.BigInteger类
注:必须是正整数相加
package org.hhu.ztc;
import java.math.BigInteger;
public class Test2 {
public static void main(String args[])
{
System.out.println(new Test2().plusBigNum("123456789","987654321"));
}
public String plusBigNum(String arg1, String arg2)
{
BigInteger big1 = new BigInteger(arg1);
BigInteger big2 = new BigInteger(arg2);
return big1.add(big2).toString();
}
}
不用java.math.BigInteger类
package org.hhu.ztc;
public class BigIntegerOperation {
public static void main(String args[]) {
String num1 = "12345";
String num2 = "54321";
int len1 = num1.length();
int len2 = num2.length();
/**
* 将不等长的两输入字符串格式化,比如 num1 = 123 num2 = 1234 经格式化后将成为num1 = 0123 num2 =
* 1234为方便计算
*/
if (len1 > len2) {
for (int i = 0; i < len1 - len2; i++) {
num2 = "0" + num2;
}
} else if (len2 > len1) {
for (int i = 0; i < len2 - len1; i++) {
num1 = "0" + num1;
}
}
int[] arr1 = BigIntegerOperation.str2intArr(num1);
int[] arr2 = BigIntegerOperation.str2intArr(num2);
int[] result = BigIntegerOperation.add(arr1, arr2);
for (int i = result.length - 1; i >= 0; i--) {
if (i == result.length - 1 && result[i] == 0)
continue;
System.out.print(result[i]);
}
}
/**
* 将字符串转换成整型数组
*/
public static int[] str2intArr(String str) {
int len = str.length();
int[] arr = new int[len];
for (int i = 0; i < len; i++) {
arr[i] = str.charAt(i) - '0';
}
return arr;
}
/**
* 核心方法 两个整型数组相加
*/
public static int[] add(int a[], int b[]) {
int maxLen = a.length;
int[] sum = new int[maxLen + 1];
for (int i = 0; i < maxLen; i++) {
int tempSum = a[i] + b[i];
sum[i] += tempSum % 10;
int d = tempSum / 10; // 进位
sum[i + 1] += d;
}
return sum;
}
}
3、求字符串中最大回文子串。
如输入ababbac 输出abba
package org.hhu.ztc;
public class Test3 {
public static void main(String args[]) {
System.out.println(new Test3().getPalindromeString("ababbac"));
}
public String getPalindromeString(String inputString) {
int len = inputString.length();
for (int i = 0; i < len; i++) {
for (int j = 0; j < i + 1; j++) {
String sub = inputString.substring(j, len - i + j);
String subReverse = new StringBuffer(sub).reverse().toString();
if (sub.equals(subReverse))
return sub;
}
}
return "无回文子串";
}
}
3.补充,求出所有回文子串及最大回文子串
package org.hhu.ztc;
public class Test3 {
public static void main(String args[]) {
new Test3().getPalindromeString("ababbac");
}
public String[] getPalindromeString(String inputString) {
int len = inputString.length();
String s1[] = new String[len * (len + 1) / 2];
String s2[] = new String[len * (len + 1) / 2];
int n = 0;
for (int i = 0; i < len; i++) {
for (int j = 0; j < i + 1; j++) {
String sub = inputString.substring(j, len - i + j);
String subReverse = new StringBuffer(sub).reverse().toString();
if (sub.equals(subReverse)) {
s1[n] = sub;
n++;
}
}
}
int m = 0;
int maxlen = 0;
if (s1[0] != null) {
maxlen = s1[0].length();
for (int i = 0; i < n; i++) {
if (s1[i].length() == maxlen) {
s2[m] = s1[i];
m++;
}
}
}
//未考虑重复
System.out.println("所有回文子串:");
for(int i=0;i<n;i++)
{
System.out.println(s1[i]);
}
System.out.println("最大回文子串:");
for(int i=0;i<m;i++)
{
System.out.println(s2[i]);
}
return s2;
}
}