话不多说,直接上代码。。
#include<bits/stdc++.h> #include<conio.h> #include<windows.h> #include<random> using namespace std; //素数判断 bool prime(unsigned long long aa) { for(unsigned long long i = 2;i<=sqrt(aa);i++) { if(aa%i==0) { return false; } } return true; } //辗转相除 int a(int aa,int bb) { return (aa%bb==0?bb:a(bb,aa%bb)); } //最小公倍 long long b(int aa,int bb) { return aa*bb/a(aa,bb); } //鸡(只因)数分解 int c(int aa,int ans[],int ii) { if(prime(aa)) { ans[ii] = aa; return ii; } for(int i = 2;i<=sqrt(aa);i++) { if(prime(i)&&aa%i==0) { ans[ii] = i; return c(aa/i,ans,ii+1); } } } //快速幂 long long d(long long a,long long b,long long m) { if(b==0) { return 1; } long long t = d(a,b/4,m)%m; if(b%4==0) { return t%m*t%m*t%m*t%m; } else if(b%4==1) { return t%m*t%m*t%m*t%m*a%m; } else if(b%4==2) { return t%m*t%m*t%m*t%m*a%m*a%m; } return t%m*t%m*t%m*t%m*a%m*a%m*a%m; } //start int main() { while(true) { cout<<"1、素数判断,2、最大公约,3、最小公倍,4、只因数分解,5、快速幂,6、退出,7、清屏,8、加减,9、乘除(输入数字请小于等于2147483647,大于等于-2147483648):\n"; char aa = getch(); switch(aa-'0') { case 6: cout<<"Goodbye!\n"; system("pause"); return 0; case 7: cout<<"清屏中... ..."; Sleep(1000*(rand()%5+1)); system("cls"); break; case 1: unsigned long long t; cout<<"请输入一个正整数:"; cin>>t; if(prime(t)) { cout<<"YES"; } else { cout<<"NO"; } break; case 2: int tt,ttt; cout<<"请输入两个正整数,空格隔开:"; cin>>tt>>ttt; cout<<a(tt,ttt); break; case 3: int tttt,ttttt; cout<<"请输入两个正整数,空格隔开:"; cin>>tttt>>ttttt; cout<<b(tttt,ttttt); break; case 4: // cout<<"开发中... ..."; int t1; cout<<"请输入一个正整数:"; cin>>t1; int ans[10000]; int t3 = c(t1,ans,0); cout<<t1<<" = "; for(int i = 0;i<=t3;i++) { cout<<ans[i]; if(i!=t3) { cout<<"*"; } } break; } if(aa-'0'==5) { int aaa,bbb,ccc; cout<<"请输入三个正整数,分别为底数、指数、答案模数,空格隔开:"; cin>>aaa>>bbb>>ccc; cout<<d(aaa,bbb,ccc); } if(aa-'0'==8) { cout<<"加or减(加1,减2):\n"; int a2 = getch()-'0'; if(a2==1) { long long a4,a5; cout<<"请输入两个正整数,空格隔开:"; cin>>a4>>a5; cout<<a4+a5; } else if(a2==2) { long long a4,a5; cout<<"请输入两个整数,空格隔开:"; cin>>a4>>a5; cout<<a4-a5; } } if(aa-'0'==9) { cout<<"乘or除(乘1,除2):\n"; int a2 = getch()-'0'; if(a2==1) { unsigned long long a4,a5; cout<<"请输入两个正整数,空格隔开:"; cin>>a4>>a5; cout<<a4*a5; } else if(a2==2) { unsigned long long a4,a5; cout<<"请输入两个正整数,空格隔开:"; cin>>a4>>a5; long double ans = a4*1.00000000000000000000000000000000000000000/a5; cout<<ans; } } if(aa-'0'!=7) { cout<<"\n"; } } return 0; }
再给你们打一遍
#include<bits/stdc++.h>
#include<conio.h>
#include<windows.h>
#include<random>
using namespace std;
//素数判断
bool prime(unsigned long long aa)
{
for(unsigned long long i = 2;i<=sqrt(aa);i++)
{
if(aa%i==0)
{
return false;
}
}
return true;
}
//辗转相除
int a(int aa,int bb)
{
return (aa%bb==0?bb:a(bb,aa%bb));
}
//最小公倍
long long b(int aa,int bb)
{
return aa*bb/a(aa,bb);
}
//鸡(只因)数分解
int c(int aa,int ans[],int ii)
{
if(prime(aa))
{
ans[ii] = aa;
return ii;
}
for(int i = 2;i<=sqrt(aa);i++)
{
if(prime(i)&&aa%i==0)
{
ans[ii] = i;
return c(aa/i,ans,ii+1);
}
}
}
//快速幂
long long d(long long a,long long b,long long m)
{
if(b==0)
{
return 1;
}
long long t = d(a,b/4,m)%m;
if(b%4==0)
{
return t%m*t%m*t%m*t%m;
}
else if(b%4==1)
{
return t%m*t%m*t%m*t%m*a%m;
}
else if(b%4==2)
{
return t%m*t%m*t%m*t%m*a%m*a%m;
}
return t%m*t%m*t%m*t%m*a%m*a%m*a%m;
}
//start
int main()
{
while(true)
{
cout<<"1、素数判断,2、最大公约,3、最小公倍,4、只因数分解,5、快速幂,6、退出,7、清屏,8、加减,9、乘除(输入数字请小于等于2147483647,大于等于-2147483648):\n";
char aa = getch();
switch(aa-'0')
{
case 6:
cout<<"Goodbye!\n";
system("pause");
return 0;
case 7:
cout<<"清屏中... ...";
Sleep(1000*(rand()%5+1));
system("cls");
break;
case 1:
unsigned long long t;
cout<<"请输入一个正整数:";
cin>>t;
if(prime(t))
{
cout<<"YES";
}
else
{
cout<<"NO";
}
break;
case 2:
int tt,ttt;
cout<<"请输入两个正整数,空格隔开:";
cin>>tt>>ttt;
cout<<a(tt,ttt);
break;
case 3:
int tttt,ttttt;
cout<<"请输入两个正整数,空格隔开:";
cin>>tttt>>ttttt;
cout<<b(tttt,ttttt);
break;
case 4:
// cout<<"开发中... ...";
int t1;
cout<<"请输入一个正整数:";
cin>>t1;
int ans[10000];
int t3 = c(t1,ans,0);
cout<<t1<<" = ";
for(int i = 0;i<=t3;i++)
{
cout<<ans[i];
if(i!=t3)
{
cout<<"*";
}
}
break;
}
if(aa-'0'==5)
{
int aaa,bbb,ccc;
cout<<"请输入三个正整数,分别为底数、指数、答案模数,空格隔开:";
cin>>aaa>>bbb>>ccc;
cout<<d(aaa,bbb,ccc);
}
if(aa-'0'==8)
{
cout<<"加or减(加1,减2):\n";
int a2 = getch()-'0';
if(a2==1)
{
long long a4,a5;
cout<<"请输入两个正整数,空格隔开:";
cin>>a4>>a5;
cout<<a4+a5;
}
else if(a2==2)
{
long long a4,a5;
cout<<"请输入两个整数,空格隔开:";
cin>>a4>>a5;
cout<<a4-a5;
}
}
if(aa-'0'==9)
{
cout<<"乘or除(乘1,除2):\n";
int a2 = getch()-'0';
if(a2==1)
{
unsigned long long a4,a5;
cout<<"请输入两个正整数,空格隔开:";
cin>>a4>>a5;
cout<<a4*a5;
}
else if(a2==2)
{
unsigned long long a4,a5;
cout<<"请输入两个正整数,空格隔开:";
cin>>a4>>a5;
long double ans = a4*1.00000000000000000000000000000000000000000/a5;
cout<<ans;
}
}
if(aa-'0'!=7)
{
cout<<"\n";
}
}
return 0;
}