有一个map<string,int>容器,其中已经存放了较多元素,设计一个算法,求出其中重复的value,并返回重复的value个数。

#include <iostream>
#include <map>
#include<string>
#include<iterator>
using namespace std;
int main()
{
	map<string, int> T;
	map<int, int> D;
	T["a"] = 1;
	T["b"] = 1;
	T["c"] = 1;
	T["e"] = 2;
	T["f"] = 3;
	T["g"] = 3;
	map<string, int>::iterator it = T.begin();
	map<int, int>::iterator its = D.begin();

	while (it != T.end())
	{
		if (it->second == (++it)->second)
		{
			D.insert(map<int, int>::value_type(it->second, its->second++));
		
		}
		
		
	}
	
	while (its != D.end())
	{
		cout << "重复的value为:" << its->first << "重复的次数为:" << its->second << endl;
	}
	return 0;
}

我不知道为什么,但是我感觉道理比较顺畅,我要去请教一下老师! 

 15题,与栈有关!下面是简单的我们设置了n=4,k=2,然后

#include<iostream>

#include <stack>

#include<iterator>
using namespace std;
int main()
{
	stack<int>st;
	stack<int>fuzhu;
	st.push(4);
	st.push(14);
	st.push(24);
	st.push(34);
	
	//n=4.k=2
	int i = 1;
	int k = 2;
	while (i < k)
	{
		fuzhu.push(st.top());
		st.pop();
		i++;
	}
	cout << st.top();
	st.pop();
	while (!(fuzhu.empty()))
	{
		st.push(fuzhu.top());
	}
	
	return 0;
}

现在我们定义一个函数,栈的情况给出,然后n与k为变量

#include<iostream>

#include <stack>

#include<iterator>
using namespace std;

void f(stack<int>A,  int k)
{
	stack<int>fuzhu;
	int i = 1;
	while (i < k)
	{
		fuzhu.push(A.top());
		A.pop();
		i++;
	}
	cout << A.top();
	A.pop();
	while (!(fuzhu.empty()))
	{
		A.push(fuzhu.top());
	}


}
int main()
{
	stack<int>st;
	
	st.push(4);
	st.push(14);
	st.push(24);
	st.push(34);
	
	
	int b = 0;
	cout << "请输入b" << endl;
	

	cin >> b;
	f(st, b);
	
	
	return 0;
}

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值