### 懶癌患者簡易工具系列|C++

1.整數開2方

` 1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 using namespace std; 5 int a[200000]; 6 int main() 7 { 8     int n,i=0,now=2,ans=1,w=1; 9     scanf("%d",&n);10     while (n!=1) {11         while (n%now==0) {12             a[++i]=now;13             n=n/now;14         }15         now++;16     }17     int m=i,en;18     i=1;19     while (i<=m) {20         int p=a[i];21         en=0;22         while (a[i]==p) {23             i++;24             en++;25         }26         if (en%2!=0) {27             for (int j=1; j<=en/2; j++) w=w*p;28             ans*=p;29         } else for (int j=1; j<=en/2; j++) w=w*p;30     }31     if (ans==1) {32         cout<<w<<endl;33     } else {34         cout<<w<<"√"<<ans<<endl;35     }36     return 0;37 }`

2.計算眾數、中位數、平均數、方差

` 1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<algorithm> 5 #include<cmath> 6 using namespace std; 7 double ping,fangcha,mid,all=0,pk=0; 8 int a[20000],bo[200100],zhong[101]; 9 int main()10 {11     printf("首先輸入1或2,1表示接下來輸入的數據是普通數據，2表示輸入的數據是加權數據，");12     printf("加權數據輸入方式為先輸入w表示有w組，接下來輸入w組a b表示每組a個b\n"); 13     memset(bo,0,sizeof(bo));14     int n=0,maxn=-1,maxx=-1,t=0,b,w,v=0,l,r;15     cin>>b;16     if (b==1) {17         scanf("%d",&n);18         for (int i=1; i<=n; i++) {19             scanf("%d",&a[i]);20             if (a[i]>maxn) maxn=a[i];21             all+=a[i];22             bo[a[i]]++;23         }24     } else {25         scanf("%d",&w);26         for (int i=1; i<=w; i++) {27             scanf("%d %d",&l,&r);28             n+=l;29             for (int j=1; j<=l; j++) a[++v]=r;30             if (a[v]>maxn) maxn=a[v];31             all+=a[v]*l;32             bo[a[v]]=l;33         }34     }35     36     for (int i=1; i<=maxn; i++) if (bo[i]>maxx) maxx=bo[i];37     for (int i=1; i<=maxn; i++) if (bo[i]==maxx) {38         t++;39         zhong[t]=i;40     }41     sort(a+1,a+n+1);42     if (n%2!=0) mid=a[(n+1)/2]; else mid=(a[n/2]+a[n/2+1])/2.0;43     ping=all/n*1.0;44     for (int i=1; i<=n; i++) pk+=(ping-a[i])*(ping-a[i]);45     fangcha=pk/n*1.0;46     printf("眾  數為：");47     if (t!=n) for (int i=1; i<=t; i++)printf("%d ",zhong[i]); else cout<<"沒有眾數";48     printf("\n");49     printf("中位數為：%.2f\n",mid);50     printf("平均數為：%.2f\n",ping);51     printf("方  差為：%.2f\n",fangcha);52     getchar();53     return 0;54 }`