1. Judgment of prime numbers:
#include<bits/stdc++.h>
typedef long long LL;
const int MaxN = 1e5;
using namespace std;
int a[MaxN + 5],n;
int is_Prime(int x)
{
if(x==1 || x==2) return 0;
for(int i=2; i*i<=x; ++i)
if(x%i == 0)return 0;
return 1;
}
int main(){
while(cin>>n){
if(is_Prime(n))
cout<<n<<" is Prime"<<endl;
else cout<<n<<" not is Prime"<<endl;
}
return 0;
}
2. Maximum common divisor and minimum common multiple:
#include<bits/stdc++.h> typedef long long LL; const int MaxN = 1e5; using namespace std; int a[MaxN + 5],m,n; int main(){ while(cin>>n>>m){ int x = __gcd(m,n); //Greatest common divisor int y = m * n / x; //Minimum common multiple printf("%d and %d The greatest common divisor is %d\n",m,n,x); printf("%d and %d The lowest common multiple is %d\n",m,n,y); } return 0; }
3. Conversion between any binaries:
#include<bits/stdc++.h>
typedef long long LL;
const int MaxN = 1e5;
using namespace std;
int a[MaxN + 5],m,n;//Converting decimal m to n-ary
stack<int>s;
int main(){
while(cin>>m>>n){
int k = 0;
while(m>0){
int e = m%n;
s.push(e);
m /= n;
}
while(!s.empty()){
cout<<s.top();
s.pop();
}
cout<<endl;
}
return 0;
}
4. Implementation of atoi() function
Function of the atoi() function: converting strings to integers
Output upper or lower bounds: -2147483648~2147483647
#include<bits/stdc++.h> typedef long long LL; const int MaxN = 1e5; using namespace std; int a[MaxN + 5],m,n; char s1[10],s2[10]; int main(){ while(cin>>s1>>s2){ LL x = atoi(s1); LL y = atoi(s2); cout<<x<<endl<<y<<endl; } return 0; }
5. Determine if a number is a perfect square number:
#include<bits/stdc++.h>
typedef long long LL;
const int MaxN = 1e5;
using namespace std;
int a[MaxN + 5],m,n;
int main(){
while(cin>>n){
if(sqrt(n) == (int) sqrt(n)) //sqrt(x) == (int) sqrt(n) ? YES:NO;
cout<<n<<" is a Perfect Squares"<<endl;
else
cout<<n<<" not is a Perfect Squares"<<endl;
}
return 0;
}
6. Determine whether a number is a power of 2
When the power of 2 is written in binary form, it is easy to see that there is only one 1 in binary, followed by n zeros.If you subtract one from this number, the only one will become zero, and the original n zeros will become 1; therefore, when you run and run the original number and the number subtracted from one, you will find zero.
For example: 2 (10), 4 (100), 8 (1000), 16 (10000)...
(n & n-1) == 0