题目描述:
给你两个正整数d和n,计算[d/1]+[d/2]+...+[d/n]的结果,
其中d,n<=2e9,[x]表示x的值需要四舍五入到整数
如9/5就是2,20/7就是3
输入:
多组测试数据,每组仅一行为d和n
当d=n=0时结束程序
输出:
计算并输出结果
样例输入:
1 1
1 25
15 27
0 0
样例输出:
1
2
63
其它信息:
Contest19竞赛题目
给你两个正整数d和n,计算[d/1]+[d/2]+...+[d/n]的结果,
其中d,n<=2e9,[x]表示x的值需要四舍五入到整数
如9/5就是2,20/7就是3
输入:
多组测试数据,每组仅一行为d和n
当d=n=0时结束程序
输出:
计算并输出结果
样例输入:
1 1
1 25
15 27
0 0
样例输出:
1
2
63
其它信息:
Contest19竞赛题目
难度:Easy
直接求解:
1 import java.util.Scanner;
2 import java.math.*;
3
4 public class Main {
5
6 public static void main(String[] args) {
7 Scanner cin = new Scanner(System.in);
8 BigDecimal i_BD, d_BD, sum_BD = BigDecimal.valueOf(0.0);
9 BigDecimal temp_BD;
10 int n,d;
11
12 while (cin.hasNextInt()) {
13 d = cin.nextInt();
14 n = cin.nextInt();
15
16 d_BD = BigDecimal.valueOf(d);//d转化成大整数
17
18 for (int i = 1; i <=n; i++) {
19 //求d/1+d/2+d/3++d/n
20 i_BD = BigDecimal.valueOf(i);
21 temp_BD = d_BD.divide(i_BD, RoundingMode.HALF_UP);
22 if (temp_BD.compareTo(BigDecimal.ZERO) > 0)
23 sum_BD = sum_BD.add(temp_BD);
24 else
25 break;
26
27 }
28 if (n == 0 || d == 0)
29 break;
30 System.out.println(sum_BD.intValue());
31 temp_BD = null;
32 sum_BD = BigDecimal.valueOf(0.0);
33
34 }
35
36 }
37 }
38
2 import java.math.*;
3
4 public class Main {
5
6 public static void main(String[] args) {
7 Scanner cin = new Scanner(System.in);
8 BigDecimal i_BD, d_BD, sum_BD = BigDecimal.valueOf(0.0);
9 BigDecimal temp_BD;
10 int n,d;
11
12 while (cin.hasNextInt()) {
13 d = cin.nextInt();
14 n = cin.nextInt();
15
16 d_BD = BigDecimal.valueOf(d);//d转化成大整数
17
18 for (int i = 1; i <=n; i++) {
19 //求d/1+d/2+d/3++d/n
20 i_BD = BigDecimal.valueOf(i);
21 temp_BD = d_BD.divide(i_BD, RoundingMode.HALF_UP);
22 if (temp_BD.compareTo(BigDecimal.ZERO) > 0)
23 sum_BD = sum_BD.add(temp_BD);
24 else
25 break;
26
27 }
28 if (n == 0 || d == 0)
29 break;
30 System.out.println(sum_BD.intValue());
31 temp_BD = null;
32 sum_BD = BigDecimal.valueOf(0.0);
33
34 }
35
36 }
37 }
38
改良后的求和(在求解比较大的数值时,已经比前一个算法快多了,可惜提交评测系统时仍然时限超时):
1 import java.math.BigDecimal;
2 import java.math.RoundingMode;
3 import java.util.ArrayList;
4 import java.util.Scanner;
5
6 public class Main {
7
8 /**
9 * @version 1.6 2009/11/16
10 * @author Eduardo
11 */
12 public static void main(String[] args) {
13 Scanner cin = new Scanner(System.in);
14 // Java动态数组的初始化
15 ArrayList<Integer> al = new ArrayList<Integer>();
16 BigDecimal d_BD, temp_BD, var_BD, sum_BD = BigDecimal.valueOf(0);
17 int n, d, record = 0;
18 long sum = 0;
19
20 var_BD = BigDecimal.valueOf(1.5);
21
22 /*
23 * 求出d/i=0.5的i的下标存入al(1),d/i=1.5的i的下标存入al(2).d/i=d-1的下标存入al(d)
24 * sum=(al(1)-al(2))*1+(al(2)-al(3))*2++(al(d-1)-al(d))*d
25 */
26 while (cin.hasNextInt()) {
27 d = cin.nextInt();
28 n = cin.nextInt();
29
30 d_BD = BigDecimal.valueOf(d);
31 al.add(0, 0);
32 al.add(1, n);
33 if (n > 2 * d) {
34 n = 2 * d;
35 }
36 for (int i = 2; i <= d; i++) {
37 al.add(i, d_BD.divide(var_BD, RoundingMode.HALF_UP).intValue());
38 if (al.get(i) - al.get(i - 1) < 10)
39 break;
40 var_BD = var_BD.add(BigDecimal.ONE);
41 }
42
43 for (int i = 1; i < al.size(); i++) {
44 sum += (al.get(i - 1) - al.get(i)) * (i - 1);
45 record = al.get(i);
46 }
47 for (long i = record; i > 0; i--) {
48 temp_BD = d_BD.divide(BigDecimal.valueOf(i),
49 RoundingMode.HALF_UP);
50 sum_BD = sum_BD.add(temp_BD);
51 }
52
53 if (n == 0 && d == 0)
54 break;
55 if (n == 1 && d == 1)
56 System.out.println(1);
57 else
58 System.out.println(sum_BD.intValue() + sum);
59 var_BD = BigDecimal.valueOf(1.5);
60 temp_BD = null;
61 sum_BD = BigDecimal.valueOf(0.0);
62 sum = 0;
63 record = 0;
64 al.clear();
65 }
66
67 }
68
69 }
70
2 import java.math.RoundingMode;
3 import java.util.ArrayList;
4 import java.util.Scanner;
5
6 public class Main {
7
8 /**
9 * @version 1.6 2009/11/16
10 * @author Eduardo
11 */
12 public static void main(String[] args) {
13 Scanner cin = new Scanner(System.in);
14 // Java动态数组的初始化
15 ArrayList<Integer> al = new ArrayList<Integer>();
16 BigDecimal d_BD, temp_BD, var_BD, sum_BD = BigDecimal.valueOf(0);
17 int n, d, record = 0;
18 long sum = 0;
19
20 var_BD = BigDecimal.valueOf(1.5);
21
22 /*
23 * 求出d/i=0.5的i的下标存入al(1),d/i=1.5的i的下标存入al(2).d/i=d-1的下标存入al(d)
24 * sum=(al(1)-al(2))*1+(al(2)-al(3))*2++(al(d-1)-al(d))*d
25 */
26 while (cin.hasNextInt()) {
27 d = cin.nextInt();
28 n = cin.nextInt();
29
30 d_BD = BigDecimal.valueOf(d);
31 al.add(0, 0);
32 al.add(1, n);
33 if (n > 2 * d) {
34 n = 2 * d;
35 }
36 for (int i = 2; i <= d; i++) {
37 al.add(i, d_BD.divide(var_BD, RoundingMode.HALF_UP).intValue());
38 if (al.get(i) - al.get(i - 1) < 10)
39 break;
40 var_BD = var_BD.add(BigDecimal.ONE);
41 }
42
43 for (int i = 1; i < al.size(); i++) {
44 sum += (al.get(i - 1) - al.get(i)) * (i - 1);
45 record = al.get(i);
46 }
47 for (long i = record; i > 0; i--) {
48 temp_BD = d_BD.divide(BigDecimal.valueOf(i),
49 RoundingMode.HALF_UP);
50 sum_BD = sum_BD.add(temp_BD);
51 }
52
53 if (n == 0 && d == 0)
54 break;
55 if (n == 1 && d == 1)
56 System.out.println(1);
57 else
58 System.out.println(sum_BD.intValue() + sum);
59 var_BD = BigDecimal.valueOf(1.5);
60 temp_BD = null;
61 sum_BD = BigDecimal.valueOf(0.0);
62 sum = 0;
63 record = 0;
64 al.clear();
65 }
66
67 }
68
69 }
70