[ 登录注册 ]

Java

HDU1215_因式分解

2016-08-25 11:31:33 admin 返回上一页

因式分解模板
•tn=n;
•for(i=2;i*i<=n;i++)//试除2~sqrt(n)
•      if(tn%i==0){//如果能被i整除
•p[++cnt]=i;//保存底数
•e[cnt]=0;//保存指数
•while(tn%i==0){//计算指数
•e[cnt]++;
•tn/=i;
•}
•}
•}
•if(tn>1){//存在大于sqrt(n)的素因子
•p[++cnt]=tn;
•e[cnt]=1;
•}
HDU1215
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1215
 
[java] 
package D0717; 
 
import java.io.*; 
 
public class HDU1215 { 
 
    public static void main(String[] args) throws IOException { 
        StreamTokenizer st = new StreamTokenizer(new BufferedReader( 
                new InputStreamReader(System.in))); 
        PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); 
        st.nextToken(); 
        int n = (int) st.nval; 
        while (n-- > 0) { 
            st.nextToken(); 
            int a = (int) st.nval; 
            int sum = 1; 
            for (int i = 2; i*i <= a; i++) { 
                if (a % i == 0) { 
                    sum += i; 
                    if(a/i!=i){ 
                        sum+=a/i; 
                    } 
                } 
            } 
            out.println(sum); 
        } 
        out.flush(); 
 
    } 
 

作者:lhfight
点击复制链接 与好友分享!回本站首页

文章来源:http://www.bozhiyue.com/java/2016/0825/426575.html
返回上一页    返回分类 上一篇:   下一篇:
相关