求最大公约数和最小公倍数
- /**
- * 求最大公约数和最小公倍数
- */
- public class Convention {
- /**
- * 求两数的最大公约数
- */
- int divisor(int m,int n){
- if(m%n==0){
- return n;
- }else{
- return divisor(n,m%n);
- }
- }
- /**
- * 求两数的最小公倍数
- */
- int gbs(int a,int b){
- int gbs = 0;
- gbs = a*b/divisor(a,b);
- return gbs;
- }
- }
求一组数组的众数
- public static double mode(double[] array) {
- Arrays.sort(array);
- int count = 1;
- int longest = 0;
- double mode = 0;
- for (int i = 0; i < array.length - 1; i++) {
- if (array[i] == array[i + 1]) {
- count++;
- } else {
- count = 1;//如果不等于,就换到了下一个数,那么计算下一个数的次数时,count的值应该重新符值为一
- continue;
- }
- if (count > longest) {
- mode = array[i];
- longest = count;
- }
- }
- System.out.println(longest);//打印出这个数出现的次数已判断是否正确
- return mode;
- }
公司笔试题就1个,要求在10分钟内作完。
题目如下:用1、2、2、3、4、5这六个数字,用java写一个main函数,打印出所有不同的排列,如:512234、412345等,要求:"4"不能在第三位,"3"与"5"不能相连。
- package test;
/**
* 用1,2,2,3,4,5,这六个数字,用java写一个main函数,打印出所有不同的排列,如:512234,412345等,要求:“4”不能排第3位,“1”与“5”不能相连。
* @author kafka0102
*
*/
public class ATest {
private static boolean is1Or5(char a,char b){
if(a=='1' && b=='5')return true;
if(a=='5' && b=='1')return true;
return false;
}
private static int countOf2(char[] num,int index){
int n=0;
for(int i=0;i<index;i++){
if(num[i]=='2')n++;
}
return n;
}
private static boolean hasSameNumber(int index,char a, char[] num){
for(int i=0;i<index;i++){
if(num[i]==a)return true;
}
return false;
}
public static int testArrange(char[] number,char[] num,int index){
int size = 0;//组合的个数
int pos1=1,pos2=2;//该变量为重复的2的数组位置,可以提取出来作为参数
int emp = countOf2(num,index);//得到当前的数组中有几个2
for(int i=0;i<num.length;i++){
if(number[i]=='2'){//当前的数字为2
if(emp >= 2){//数组中2的个数多于2个
continue;
}else if(emp == 1){//数组中有一个2时,要求当前的2不能为位置1的2
if(i==pos1)continue;
}else{
if(i==pos2)continue;//数组中没有2时,要求当前的2不能为位置2的2
}
}else{
if(index==2 && number[i]=='4')continue;//去除4
if(index>0 && is1Or5(num[index-1],number[i]))continue;//去除相邻的1和5
if(hasSameNumber(index,number[i], num))continue;//去除重复数字
}
num[index] = number[i];
if(index==5){
System.out.println(num);
size++;
}else{
size = size + testArrange(number,num,index+1);
}
}
return size;
}
public static void aTest(){
char[] number = {'1','2','2','3','4','5'};
char[] num = new char[number.length];
int size =0;
size = testArrange(number,num,0);
System.out.println("size="+size);
}
public static void main(String[] args) {
aTest();
}
}
有一个String s="SDsBEaA"
要求产生这样的结果:s="AaBDESs"
public class MySort {
public static void main(String[] args) {
List<Character> result = new ArrayList<Character>();
String s = "SDsBEAa";
char[] strArray = s.toCharArray();
// strArray = ABDESas
Arrays.sort(strArray);
for (int i = 97; i < 122; i++) {
char tempLowCase = (char) i;
char tempUpperCase = (char) (i - 32);
for (int j = 0; j < strArray.length; j++) {
if (Character.isUpperCase(strArray[j])) {
if (tempUpperCase == strArray[j]) {
result.add(tempUpperCase);
}
}
if (tempLowCase == strArray[j]) {
result.add(strArray[j]);
}
}
}
System.out.println(result);
}
}
本文部分代码摘自:
http://ravi.iteye.com/blog/122255(最大公约数)
http://www.blogjava.net/kafka0102/archive/2007/03/13/103434.html(数字组合)