JustOj 1032: 习题6.7 完数
生活随笔
收集整理的這篇文章主要介紹了
JustOj 1032: 习题6.7 完数
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
題目描述
一個數(shù)如果恰好等于它的因子之和,這個數(shù)就稱為"完數(shù)"。 例如,6的因子為1、2、3,而6=1+2+3,因此6是"完數(shù)"。 編程序找出N之內(nèi)的所有完數(shù),并按下面格式輸出其因子:
輸入
N
輸出
? its factors are ? ? ?
樣例輸入
1000樣例輸出
6 its factors are 1 2 3 28 its factors are 1 2 4 7 14 496 its factors are 1 2 4 8 16 31 62 124 248題解:枚舉即可 學校oj暫時不能測試但是樣例是對的附下圖 (歡迎路過的大佬指出錯誤)
1 #include <iostream> 2 #include <algorithm> 3 #include <cstring> 4 #include <cstdio> 5 #include <vector> 6 #include <cstdlib> 7 #include <iomanip> 8 #include <cmath> 9 #include <ctime> 10 #include <map> 11 #include <set> 12 using namespace std; 13 #define lowbit(x) (x&(-x)) 14 #define max(x,y) (x>y?x:y) 15 #define min(x,y) (x<y?x:y) 16 #define MAX 100000000000000000 17 #define MOD 1000000007 18 #define pi acos(-1.0) 19 #define ei exp(1) 20 #define PI 3.141592653589793238462 21 #define INF 0x3f3f3f3f3f 22 #define mem(a) (memset(a,0,sizeof(a))) 23 typedef long long ll; 24 ll gcd(ll a,ll b){ 25 return b?gcd(b,a%b):a; 26 } 27 const int N=2005; 28 const int mod=1e9+7; 29 void la(int n) 30 { 31 int s=n-1,flag=0; 32 for(int i=2;i*i<=n;i++){ 33 if(n%i==0) s-=i+n/i; 34 } 35 if(s==0) flag=1; 36 if(flag){ 37 cout<<n<<" its factors are"; 38 for(int i=1;i<n;i++){ 39 if(n%i==0){ 40 cout<<" "<<i; 41 } 42 } 43 cout<<endl; 44 } 45 } 46 int main() 47 { 48 std::ios::sync_with_stdio(false); 49 int n; 50 cin>>n; 51 for(int i=2;i<=n;i++){ 52 la(i); 53 } 54 return 0; 55 }
轉(zhuǎn)載于:https://www.cnblogs.com/shixinzei/p/7267104.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的JustOj 1032: 习题6.7 完数的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux命令大全(文件管理)
- 下一篇: 欧拉函数 euler