#7169: 到底錯在哪裡啊啊啊


abrcdf102 (彥欣)

學校 : 不指定學校
編號 : 27571
來源 : [140.136.163.168]
最後登入時間 :
2013-02-25 17:55:47
a010. 因數分解 | From: [140.136.164.133] | 發表日期 : 2012-11-09 12:04


import java.util.Scanner;

public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {


        Scanner input = new Scanner(System.in);
        int n = input.nextInt();

       
        for (int i = 2; i <= n; i++) {
            int count = 0;
            while (n % i == 0) {
                n /= i;
                count++;
            }
            if (count > 0) {
                if (count == 1) {
                    System.out.print(i);
                } else {
                    System.out.print(i + "^" + count);
                }

                if (n > 1) {
                    System.out.print(" * ");
                }
            }
        }

    }
}

 
#7943: Re:到底錯在哪裡啊啊啊


bbence0411 (勤)

學校 : 國立中央大學
編號 : 33510
來源 : [140.115.207.145]
最後登入時間 :
2014-01-10 00:29:44
a010. 因數分解 | From: [220.137.110.46] | 發表日期 : 2013-07-11 16:59


import java.util.Scanner;

public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {


        Scanner input = new Scanner(System.in);
        int n = input.nextInt();

       
        for (int i = 2; i <= n; i++) {
            int count = 0;
            while (n % i == 0) {
                n /= i;
                count++;
            }
            if (count > 0) {
                if (count == 1) {
                    System.out.print(i);
                } else {
                    System.out.print(i + "^" + count);
                }

                if (n > 1) {
                    System.out.print(" * ");
                }
            }
        }

    }
}

String output = "";
int helf = input / 2;
for (int i = 2; i <= helf; i++) {
if (input % i == 0) {
int count = 0;
while (input % i == 0) {
count++;
input /= i;
}
if (count == 1) {
if (input == 1) {
output += i;
} else {
output += i + " * ";
}

} else {
if (input == 1) {
output += i + "^" + count;
} else {
output += i + "^" + count + " * ";
}
}
}
}
if (output.equals("")) {
output += input;
}
return output;

 
ZeroJudge Forum