首页 > 其他 > 详细

将一个字符串部分前置

时间:2015-05-15 09:07:58      阅读:238      评论:0      收藏:0      [点我收藏+]

输入10个整形数,输入一个整形m,将整形数m个数字前置,如 1 2 3 4 5 6 7 8 9 10,m=3,输出 8 9 10 1 2 3 4 5 6 7

解题思路:

使用两个堆栈即可。代码如下:

#include <iostream>
#include<stack>

using namespace std;

void main()
{
	int a[10]={0};
	for (int i=0;i<10;i++)
	{
		cin>>a[i];
	}
	int num;
	cin>>num;
	num%=10;
	stack<int> sta1,sta2;
	for (int i=0;i<10;i++)
	{
		if (i<(10-num))
		{
			sta1.push(a[i]);
		}
		else
			sta2.push(a[i]);
	}
	for (int i=9;i>=0;i--)
	{
		while (!sta1.empty())
		{
			int temp=sta1.top();
			a[i]=temp;
			i--;
			sta1.pop();
		}
		while (!sta2.empty())
		{
			int temp=sta2.top();
			a[i]=temp;
			i--;
			sta2.pop();
		}
	}
	for (int i=0;i<10;i++)
	{
		cout<<a[i]<<" ";
	}

}



将一个字符串部分前置

原文:http://blog.csdn.net/sinat_24520925/article/details/45728069

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!