Hello,everyone.Yesterday is Mid-Autumn Festival.And i am so sorry that i waste my time on computer games.But today i come back and keep fighting,which is my responsibility.And today i am here to solve some simple problems:Integer division,Factorization,Half set problem and Mod power.
Tomorrow i will give some harder problems solved by recursion & divide and conquer.And i will turn to review dynamic programming.
Have fun coding,i_human.Have fun coding,everyone!
Although today i still troubled by games,but i will adjust.I am the master of myself.
THE CODE:
integer division:
#include "stdafx.h"
#include<iostream>
using namespace std;
int f(int m,int n);
int main()
{
int a=6;
cout<<f(a,a)<<endl;
system("pause");
return 0;
}
int f(int m,int n)
{
if(m==1 || n==1)
return 1;
if(m<=n)
return f(m,m-1)+1;
if(m>n)
return f(m,n-1)+f(m-n,n);
else
{
cout<<"error."<<endl;
return 0;
}
}
Factorization:
#include "stdafx.h"
#include<iostream>
using namespace std;
int find(int n);
int main()
{
int n=12;
cout<<find(n);
system("pause");
return 0;
}
int find(int n)
{
int sum=0;
if(n==1)
sum=sum+1;
for(int i=1;i<=n-1;i++)
{
if(n%i==0)
sum=sum+find(i);
}
return sum;
}
Half set problem:
#include "stdafx.h"
#include<iostream>
using namespace std;
void count(int n);
int number;
int main()
{
int k=23;
count(k);
cout<<number;
system("pause");
return 0;
}
void count(int n)
{
if(n==1)
number++;
else
{
for(int i=1;i<=n/2;i++)
count(i);
number++;
}
}
Mod power:
#include "stdafx.h"
#include<iostream>
using namespace std;
int answear(int m,int n,int k);
int main()
{
int m=3,n=18132,k=17;
int l=answear(m,n,k);
cout<<l<<endl;
system("pause");
return 0;
}
int answear(int m,int n,int k)
{
if(n==1)
return m%k;
if(n==0)
return 1;
if(n%2==0)
return answear((m*m)%k,n/2,k);
if(n%2!=0)
return (answear(m,n-1,k)*m)%k;
else
{
cout<<"error"<<endl;
return 0;
}
}