SDNU 1427.分解质因数(水题)
生活随笔
收集整理的這篇文章主要介紹了
SDNU 1427.分解质因数(水题)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Description
問題描述 求出區間[a,b]中所有整數的質因數分解。 輸入格式 輸入兩個整數a,b。 輸出格式 每行輸出一個數的分解,形如k=a1*a2*a3...(a1< =a2< =a3...,k也是從小到大的)(具體可看樣例) 樣例輸入 3? 10 樣例輸出 3=3 4=2*2 5=5 6=2*3 7=7 8=2*2*2 9=3*3 10=2*5 提示 先篩出所有素數,然后再分解。 數據規模和約定 2< =a< =b< =10000Input
Output
Sample Input
Sample Output
思路:明明就是一道水題,debug卻讓我吐血!一開始我用vector來存數據,但是爆了,也不知道為什么會爆,我明明開了很大的數。后來只能用簡化的辦法了。 #include <cstdio> #include <iostream> #include <cmath> #include <string> #include <cstring> #include <algorithm> #include <queue> #include <vector> #include <map> using namespace std; #define ll long long const int inf = 0x3f3f3f3f; const int mod = 1e9+7;int a, b, tot, pri[10000+8], en[10000+8]; bool is[10000+8];void E() {tot = 0;memset(is, 1, sizeof(is));is[0] = is[1] = 0;for(int i = 2; i<10000+8; i++){if(is[i]){pri[tot++] = i;for(int j = i+i; j<10000+8; j += i)is[j] = 0;}} }int main() {E();while(~scanf("%d%d", &a, &b)){for(int i = a; i <= b; i++){int id = 0, buffer = i;for(int j = 0; j < tot && pri[j]*pri[j] <= i; j++){if(buffer%pri[j] == 0){while(buffer%pri[j] == 0){en[id++] = pri[j];buffer /= pri[j];}}}if(buffer>1)en[id++] = buffer;printf("%d=", i);bool flag = 0;for(int j = 0; j<id; j++){if(flag)printf("*");flag = 1;printf("%d", en[j]);}printf("\n");}}return 0; }?
轉載于:https://www.cnblogs.com/RootVount/p/11252860.html
總結
以上是生活随笔為你收集整理的SDNU 1427.分解质因数(水题)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SDNU 1429.区间k大数查询(水题
- 下一篇: SDNU 1423.入学考试(01背包)