【无标题】数组创立二叉树,max,find

数组创立二叉树

输入样例:
7
1 2 3 4 5 6 7

输出样例:
4 2 5 1 6 3 7

 


#include <iostream>
using namespace std;
int a[1000];
void print(int a[], int k)
{
	if (a[k] != 0)             //如果此处为a[]!=0,则有下一注释
	{
		print(a, 2 * k + 1);
		cout << a[k] << " ";
		print(a, 2 * k + 2);
	}
}

int main()
{
	int i, n;   //a[]必须赋值为0,否则visual stdio会显示错误
	cin >> n;
	for (i = 0; i < n; i++)
		cin >> a[i];
	print(a, 0);
	cout << endl;
}

二 .max的使用

头文件#include<algorithm>

#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
	int a, b;
	cin >> a >> b;
	cout << max(a, b) << endl;
}

三 . find的使用


//find如果未找到自动返回string.npos;用int型数据代表后为-1;

/*
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
	string s1, s2;
	getline(cin, s1);
	getline(cin, s2);
	int x = s1.find(s2);
	int y = s1.npos;
	if (s1.find(s2) != s1.npos)
		cout << x<<endl<< y<< endl<<s1.npos << endl;
}
eg:     S1:abcdefghijk             S2=bcde
		 x=1    y=-1    s1.npos=乱码
*/


//cout<<s1.find(s2)<<endl;会乱码;必须用int型数据储存才行  如:int a=s1.find(s2);    cout<<a<<endl;
//string中的find()函数,若查找成功,返回找到的第一个字符或子串的位置;若查找失败,返回string.npos
//即if(string.find(string)!=string.npos) 表示如果查找到了;

#include <iostream>
#include <string>
using namespace std;
int main()
{

	string s1, s2;
	getline(cin, s1);
	getline(cin, s2);
	int a1, a2, a3, a4, a5;
	a1 = s1.find(s2);
	//在S1中查找第一个全匹配的字符串并返回其第一个字符的下角标
/*
	eg:    S1:abcdef        S2=bc
		return 的是1(b的下角标);
*/


	a2 = s1.find_first_of(s2);
	//在S1中查找第一个在S2中有的元素并返回其下角标

/*
	eg:    S1:abcdef        S2=cb
		return 的是1(b的下角标);
*/

	a3 = s1.find_last_of(s2);
	//从s1最后开始查找第一个在S2中有的元素并返回其下角标
/*
	eg:    S1:abcdef        S2=cb
		return 的是2(c的下角标);
*/

	a4 = s1.rfind(s2);
	//查找最后一个与S2相同的子串并返回其第一个字符的下角标
/*
	eg:    S1:acbdefcb      S2=cb
		return 的是6(b的下角标);
*/

	a5 = s1.find_first_not_of(s2);
	//查找S2内第一个不属于S1的字符并返回其下角标
/*
	eg:    S1:abczef      S2=abcdefg
		return 的是3(z的下角标);
*/

	cout << a1 << endl;
	cout << a2 << endl;
	cout << a3 << endl;
	cout << a4 << endl;
	cout << a5 << endl;
	system("pause");

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值