小米服务端编程题第一题(还差数字的情况)
发布于 2017-09-18 21:17 2513 次浏览 0 赞 最后一次编辑是 2017-09-18 21:18 来自 笔试面试  
#include <iostream>
#include <string>

using namespace std;

//思路是从后往前判断
void StrToH(string str)
{
	string tmp;

	if (str.size() == 1)
	{
		tmp.push_back('_');
		if (str[0] >= 'a')
		{
			tmp.push_back(str[0] - 32);

		}
		else
		{
			tmp.push_back(str[0]);
		}
		tmp.push_back('_');
		cout << tmp << endl;
		return;
	}

	for (int i = str.length() - 1; i >= 0; i--)
	{
		if (i == 0)
		{
			if (str[i] >= 'a')
			{
				tmp.push_back(str[i] - 32);
			}
			else
			{
				tmp.push_back(str[i]);
			}
			tmp.push_back('_');
			continue;
		}

		if (i == str.length() - 1)
		{
			tmp.push_back('_');
			if (str[i] >= 'a')
			{
				tmp.push_back(str[i] - 32);

			}
			else
			{
				tmp.push_back(str[i]);
			}
			continue;
		}

		if (str[i] == '.')
		{
			tmp.push_back('_');

			continue;
		}

		char tmpC = str[i + 1];
		if (tmpC == '.')
		{
			if (str[i] >= 'a')
			{
				tmp.push_back(str[i] - 32);
			}
			else
			{
				tmp.push_back(str[i]);
			}
			continue;
		}

		if ((tmpC >= 'a') && (str[i] <= 'Z'))
		{
			tmp.push_back(str[i]);
			tmp.push_back('_');
			continue;
		}

		if (tmpC <= 'Z' && str[i] <= 'Z')
		{
			tmp.push_back(str[i]);
			continue;
		}
		if (tmpC <= 'Z' && str[i] >= 'a')
		{
			tmp.push_back('_');
			tmp.push_back(str[i] - 32);
			continue;
		}

		tmp.push_back(str[i] - 32);
		
	}
	string result(tmp.rbegin(), tmp.rend());
	cout << result << endl;
}

int main()
{
	string str;
	cin >> str;

	StrToH(str);

	return 0;
}


3 条回复

哥们,我想知道的是他让输入多行数据,怎么终止啊?没有终止条件啊

2017-09-18 21:23
#include<stdio.h>
#include<iostream>
#include<stack>
#include<string.h>

using namespace std;

int main()
{
	char s[1024] ={0}, temp;
	fgets(s,1024,stdin);
	s[strlen(s)-1] = '\0';
	int len = strlen(s);
	stack<char> x;
//	cout<<s<<endl;
  	
	int i = 0, flag = 0;
   	x.push('_');
	for(i=len-1;i>=0;i--)
	{
		if(s[i] == '.')
			x.push('_');
		else
			x.push(s[i]);

		if(isdigit(s[i]) && isalpha(s[i-1]))
			x.push('_');
		else if(isalpha(s[i]) && isdigit(s[i-1]))
			x.push('_');
		else if(islower(s[i]) && isupper(s[i-1]))
			x.push('_');
		else if(isupper(s[i]) && islower(s[i-1]))
			x.push('_');
	}
   	x.push('_');

	while(!x.empty())
	{
		if(isalpha(x.top()))
			cout<<(char)toupper(x.top());
		else
			cout<<x.top();
		x.pop();
	}
	cout<<endl;
	return 0;
}


2017-09-18 21:29

#include<iostream>

#include<vector>

#include <stdio.h>

using namespace std;

int main()

{

    freopen("in.txt","r",stdin);

    vector<string> v_res;

    while(1)

    {

        string str;

        getline(cin,str);//¶ÁÈëstring

        if(str=="")

            break;

        string res="";

        res +='_';

        int count = 1;

        bool preIsupper = false;

        bool preIsdigit = false;

        bool preIspoint = false;

        for(int i =0;i<str.length();++i)

        {

            if(i==0 && isupper(str[i]))

            {

                res+=str[0];

                preIsupper =true;

                preIsdigit = false;

                preIspoint = false;

            }

            else if(str[i]=='.')

            {

                res+='_';

                preIsupper =false;

                preIsdigit = false;

                preIspoint = true;

            }

            else if(preIspoint)

            {

                res+=toupper(str[i]);

                if(isupper(str[i]))

                {

                    preIsupper =true;

                    preIsdigit = false;

                    preIspoint = false;

                }

                else if(isdigit(str[i]))

                {

                    preIsupper =false;

                    preIsdigit = true;

                    preIspoint = false;

                }

                else if(islower(str[i]))

                {

                    preIsupper =false;

                    preIsdigit = false;

                    preIspoint = false;

                }

            }

            else if(preIsupper)

            {

                if(isupper(str[i]))

                {

                    preIsupper =true;

                    preIsdigit = false;

                    preIspoint = false;

                    if(i==str.length()-1 || (i<str.length()-1 && (isupper(str[i+1]) || isdigit(str[i+1]) || str[i+1]=='.')))

                    {

                        res+=str[i];

                    }

                    else if(i<str.length()-1 && islower(str[i+1]))

                    {

                        res+='_';

                        res+=toupper(str[i]);

                    }

                    else

                    {

                        res+=str[i];

                    }


                }

                else if(islower(str[i]))

                {

                    res+=toupper(str[i]);

                    preIsupper = false;

                    preIsdigit = false;

                    preIspoint = false;

                }

                else if(isdigit(str[i]))

                {

                    res+='_';

                    res+=str[i];

                    preIsupper = false;

                    preIsdigit = true;

                    preIspoint = false;

                }

            }

            else if(preIsdigit)

            {

                if(isupper(str[i]) || islower(str[i]))

                {

                    res+='_';

                    res+=str[i];

                    preIsupper = false;

                    preIspoint = false;

                    preIsdigit = false;

                    if(isupper(str[i]))

                        preIsupper = true;

                }

                else

                {

                    res+=str[i];

                    preIsupper = false;

                    preIsdigit = true;

                    preIspoint = false;

                }

            }

            else

            {

                if(isupper(str[i]) )

                {

                    res+='_';

                    res+=str[i];

                    preIsupper = true;

                    preIsdigit = false;

                    preIspoint = false;

                }

                else if(isdigit(str[i]))

                {

                    res+='_';

                    res+=str[i];

                    preIsupper = false;

                    preIsdigit = true;

                    preIspoint = false;

                }

                else

                {

                    res+=toupper(str[i]);

                    preIsupper = false;

                    preIsdigit = false;

                    preIspoint = false;

                }

            }

        }

        res +='_';

        v_res.push_back(res);

    }

    for(unsigned int i=0;i<v_res.size();++i)

        cout<<v_res[i]<<endl;

    return 0;

}


2017-09-18 21:42
添加回复
回到顶部