搜狐笔试两道算法题

1 排序数字字符串的数字(升序),遇到0时从数字字符串中删除,如"1324"排序后输出"1234","9002"排序后应该为"29"

#include<iostream>
#include<string>
using namespace std;

int cmp(const void* a,const void* b)
{
	return *(char *)a-*(char*)b;
}

void reverseAndOutput(char* str)
{
	if(str==NULL) return;

	char* preIndex = str;
	char* postIndex = str;

	while(*preIndex!='0'&&*preIndex!='\0')
		preIndex++;
	postIndex = preIndex;

	while(*postIndex!='\0')
	{
		if(*postIndex!='0')
			*preIndex++ = *postIndex;
		postIndex++;
	}
	*preIndex = '\0';
	qsort(str,preIndex-str,sizeof(char),cmp);
	cout<<str<<endl;
}


int main()
{
	char str[] = "01000007009020000500000300040";
	reverseAndOutput(str);
}


2 前后点到输入的英文中的单词位置,标点符号(只可以出现在句尾)位置不变,如输入"Hello how are you",输出应该为"you are how Hello!"。

#include<iostream>
#include<string>
using namespace std;

void reverse(char* head,char* tail)
{
	if(head==NULL||tail==NULL)
		return;

	while(head<tail)
	{
		char temp = *head;
		*head = *tail;
		*tail = temp;
		head++;
		tail--;
	}
}

void reverseAndOutput(char* str)
{
	char* preIndex = str;
	char* postIndex = str;

	while((*postIndex<='z'&&*postIndex>='a')
		||(*postIndex<='Z'&&*postIndex>='A')
		||*postIndex==' ') postIndex++;
	postIndex--;

	reverse(preIndex,postIndex);

	char* end = postIndex+1;
	preIndex = postIndex = str;


	while(preIndex!=end)
	{
		if(*preIndex==' ')
		{
			preIndex++;
			postIndex++;
		}
		else if(*postIndex==' '||postIndex==end)
		{
			reverse(preIndex,postIndex-1);
			preIndex=postIndex;
		}
		else
			postIndex++;

	}
	cout<<str<<endl;
}


int main()
{
	char str[] = "Hello my class mates!???";
	reverseAndOutput(str);
}


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值