【4】求最大奇数公约数

求最大奇数公约数
//如输入10:求f(10)+f(11)+...+f(1)
//其中:10的最大奇数公约数为5,9为9,8为1;7为7,6为1,5为5,。。。。

 1 package com.oj.test;
 2 //求最大奇数公约数
 3 //如输入10:求f(10)+f(11)+...+f(1)
 4 //其中:10的最大奇数公约数为5,9为9,8为1;7为7,6为1,5为5,。。。。
 5 public class Test3 {
 6     public static int test(int input){
 7         StringBuilder sb=new StringBuilder(128);
 8         int i=1;
 9         while(i<=input){
10             if((i & 1)!=0){
11                 if(input % i==0){
12                     sb.append(i).append(",");
13                 }
14             }
15             i++;
16         }
17         String[] strs=sb.toString().split(",");
18         int max=Integer.parseInt(strs[0]);
19         for(int j=1;j<strs.length;j++){
20             if(max<Integer.parseInt(strs[j]))
21                 max=Integer.parseInt(strs[j]);
22         }
23         return max;
24     }
25     public static int test2(int input){
26         int sum=0;
27         while(input>0){
28             sum+=test(input);
29             input--;
30         }
31         return sum;
32     }
33     
34     public static void main(String[] args) {
35         System.out.println(test(7));
36         System.out.println(test2(7));
37     }
38 }
原文地址:https://www.cnblogs.com/noaman/p/5904993.html