some math problems solved by Recursion & divide and conquer
来源:互联网 发布:微信公众号源码下载 编辑:程序博客网 时间:2024/04/25 13:16
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;
}
}
- some math problems solved by Recursion & divide and conquer
- Divide and conquer algorithm
- Divide-and-Conquer
- 分治 Divide and Conquer
- divide-and-conquer
- algorithm:divide and conquer
- Divide and Conquer
- Divide and Conquer
- Divide and conquer
- divide and conquer
- Divide and Conquer
- [leetcode] Divide and Conquer
- Divide and Conquer
- Divide and Conquer
- Divide and conquer 总结
- leetcode-Divide and Conquer
- LeetCode--Divide and Conquer
- Divide and Conquer
- POJ 2031
- Hive运行原理
- 【Java TCP/IP Socket】基于线程池的TCP服务器(含代码)
- SEO与标签的语义化
- 如何成为一名黑客(网络安全从业者)——网络攻击技术篇(3/8 网络协议欺骗)
- some math problems solved by Recursion & divide and conquer
- 【一步步学OpenGL 8】 -《缩放变换》
- 【Java TCP/IP Socket】TCP Socket通信中由read返回值造成的的死锁问题(含代码)
- 【POJ2385】Apple Catching(简单DP)
- 平台总线(二)
- Android面试基础
- 如何理解递归
- android中activuty的简单总结
- Linux - C预习内容(七)