Created
October 21, 2023 09:15
-
-
Save ArianisTisfnel/32fdb047ec38d34869f20b68e92f1ffe to your computer and use it in GitHub Desktop.
zerojudge_a010.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int main(void){ | |
int iInput, i, iCounter, iInput_backup, if_number_exists; | |
do{ | |
scanf("%d",&iInput); | |
i = 2; | |
iInput_backup = iInput; | |
if_number_exists = 0; | |
while (iInput != 1){ // 當輸入 iInput 被整除到只剩下 1 時,結束迴圈。 | |
iCounter = 0; | |
while(iInput % i == 0){ | |
iCounter++; | |
iInput /= i; | |
} // 計算質因數次方的迴圈,並將輸入除以該質因數直到無法整除。 | |
if (iCounter > 1){ | |
printf("%d^%d", i, iCounter); | |
if_number_exists++; | |
} // 如果質因數次方 > 1,印出質因數與次方"i^iCounter" | |
else if (iCounter == 1){ | |
printf("%d", i); | |
if_number_exists++; | |
} // 如果質因數次方 = 1,印出質因數"i" | |
i++; | |
if (iInput % i == 0 && if_number_exists == 1){ | |
printf(" * "); | |
if_number_exists = 0; | |
} // 判定該數是否還存在質因數,如果是,重置 if_number_exist 並印出" * "承接下一個質因數。 | |
} | |
puts(""); | |
}while(iInput_backup > 1 && iInput_backup <= 100000000); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment