将字符串中的cvt(CVT)换成cvte(CVTE)
string fix(char* str, int n)
{
int i=0;
string newStr;
while(i<n)
{
while(str[i]!='c'&&str[i]!='C'&&i<n)
{
newStr+=str[i];
i++;
}
if(i+2<n && str[i]=='c' && str[i+1]=='v' && str[i+2]=='t')
{
newStr+="cvte";
i+=3;
}
else if(i+2<n && str[i]=='C' && str[i+1]=='V' && str[i+2]=='T')
{
newStr+="CVTE";
i+=3;
}
else
{
if(i==n)break;
newStr+=str[i];
i++;
}
}
return newStr;
}
求树的节点数
struct TreeNode
{
int m_value;
TreeNode* m_plchild;
TreeNode* m_prchild;
TreeNode():m_value(0), m_plchild(NULL), m_prchild(NULL){}
};
int countNode(const TreeNode* proot)
{
static int num=0;
if(proot == NULL) return 0;
countNode(proot->m_plchild);
countNode(proot->m_prchild);
return num;
}
斐波那契数列
循环实现
int fib(int n)
{
if(n==0)return 0;
if(n==1)return 1;
int sum1=0;
int sum2=1;
for(int i=2; i<n; i++)
{
int temp = sum2;
sum2 += sum1;
sum1 = temp;
}
return sum1+sum2;
}
递归实现
int fib2(int n)
{
if(n==0)return 0;
if(n==1)return 1;
return fib(n-1)+fib(n-2);
}