华为机试

1)输入

3

3 6 2 4 5 2

输出2

package sort;

import java.util.Scanner;

public class merge {

	public static void main(String[] args) {

		Scanner in = new Scanner(System.in);

		int m = Integer.valueOf(in.nextLine());
		String str = in.nextLine();
		String[] strs = str.split(" ");
		int array[] = new int[strs.length];
		for (int i = 0; i < strs.length; i++) {
			array[i] = Integer.valueOf(strs[i]);
		}

		System.out.println(getPiPei(array, array.length, m));
	}

	public static int getPiPei(int array[], int n, int m) {
		if (n != 2 * m)
			return -1;
		int i = 0, j = n - 1;
		int leftSum = array[0];
		int rightSum = array[n - 1];
		int result = 0;
		while (i < n && j >= 0) {

			if (leftSum == rightSum) {
				result++;
				i++;
				j--;
				if (i < n && j >= 0) {
					leftSum += array[i];
					rightSum += array[j];
				}

			} else if (leftSum < rightSum) {
				i++;
				if (i < n) {
					leftSum += array[i];
				}
			} else {
				j--;
				if (j >= 0) {
					rightSum += array[j];
				}
			}
		}
		return result;
	}
}

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

package sort;

import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;

public class Main2 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);

		int size1 = sc.nextInt();
		Set<Integer> arr1 = new TreeSet<Integer>();
		for (int i = 0; i < size1; i++) {
			arr1.add(sc.nextInt());
		}
		int size2 = sc.nextInt();
		Set<Integer> arr2 = new TreeSet<Integer>();
		for (int i = 0; i < size2; i++) {
			arr2.add(sc.nextInt());
		}
		int size3 = sc.nextInt();
		Set<Integer> arr3 = new TreeSet<Integer>();
		for (int i = 0; i < size3; i++) {
			arr3.add(sc.nextInt());
		}

		arr1.retainAll(arr2);
		arr1.retainAll(arr3);
		for (int i : arr1) {
			System.out.print(i + " ");
		}

	}

}


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

package sort;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;

public class ShuiXianHuaNumber {
	public static void main(String[] args) throws JsonGenerationException, JsonMappingException, IOException {
		Scanner cin = new Scanner(System.in);
		int m = cin.nextInt();
		int n = cin.nextInt();
		List<Integer> resultList = getShuiXianHuaNumber(m, n);
		for (int i = 0; i < resultList.size(); i++) {
			if (i != 0)
				System.out.print(" ");
			System.out.print(resultList.get(i));
		}
	}

	public static List<Integer> getShuiXianHuaNumber(int m, int n) {
		List<Integer> result = new ArrayList<>();
		for (int i = m; i <= n; i++) {
			if (verify(i)) {
				result.add(i);
			}
		}
		return result;
	}

	public static boolean verify(int i) {
		int sum = 0, j, tmp = i;
		while (i != 0) {
			j = i % 10;
			sum += (int) Math.pow(j, 3);
			i = i / 10;
		}
		if (sum == tmp)
			return true;
		return false;
	}
}



     

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值