人人网三道题编程题代码分享

import java.util.Scanner;
//最大公约数
public class s3 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int p[] = new int[n];
		for (int i = 0; i < n; i++)
			p[i] = sc.nextInt();
		if (n == 1) {
			System.out.println(p[0]);
			return;
		}
		if (n == 2) {
			System.out.println(gcd(p[0], p[1]));
			return;
		}
		int m = gcdA(p, n);
		System.out.println(m);

	}

	private static int gcdA(int[] p, int n) {
		int a = p[0];
		int b = p[1];
		int c = gcd(a, b);
		int i;
		for (i = 2; i < n; i++) {
			c = gcd(c, p[i]);
		}
		return c;
	}
	private static int gcd(int n, int m) {
		if (n < m) {
			n = m + n;
			m = n - m;
			n = n - m;
		}
		if (m == 0)
			return n;
		return gcd(m, n % m);
	}
}
//边界问题
import java.util.Scanner;

public class s1 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while (sc.hasNext()) {
			long n = sc.nextLong();
			long m = sc.nextLong();
			long left = 240 - m;
			if (left <= 0) {
				System.out.println(0);
				return;
			}
			long res = 0;
			long i;
			long nn = 0;
			for (i = 1;; i++) {
				res = 5 * i + res;
				if (res == left) {
					nn = i;
					break;
				} else if (res > left) {
					nn=i-1;
					break;
				}
			}
			System.out.println(Math.min(nn, n));
		}
	}
}

//注意时间复杂度,全部秋和
import java.util.Scanner;

public class s2 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while (sc.hasNext()) {
			int n = sc.nextInt();
			int p[] = new int[n];
			int dp[] = new int[n];
			for (int i = 0; i < n; i++){
				p[i] = sc.nextInt();
				if(i==0)
					dp[i]=p[i];
				else
				dp[i] = dp[i - 1] + p[i];
			}
			int count = 0;
			for (int i = 0; i < n - 1; i++) {
				if (dp[i] * 2 == dp[n - 1]) {
					count++;
				}
			}
			System.out.println(count);
		}
	}
}


全部评论
import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner s = new Scanner(System.in); while (s.hasNext()) { int n = s.nextInt(); int[] lengths = new int[n]; int sum = 0; int countsTemp = 0; for (int i = 0; i lengths.length; ++i) { lengths[i] = s.nextInt(); sum += lengths[i]; if (lengths[i] == 0) { countsTemp++; } } if (countsTemp == n) { System.out.println(n-1); continue; } int preSum = 0; int counts = 0; int j = 0; while (j lengths.length) { for (; j lengths.length; ++j) { preSum += lengths[j]; if (preSum == sum - preSum && j != 0 && j != lengths.length - 1) { counts++; ++j; break; } } } System.out.println(counts); } } }
点赞 回复 分享
发布于 2017-04-09 21:36

相关推荐

不愿透露姓名的神秘牛友
11-26 18:54
说等下个版本吧的发呆爱好者很贪睡:佬最后去了哪家呀
点赞 评论 收藏
分享
叮咚鸭:群众里面有坏人
点赞 评论 收藏
分享
专心打鱼:互联网搬运工,贴子都要偷
点赞 评论 收藏
分享
评论
点赞
9
分享
牛客网
牛客企业服务