90分求助 13 15测试点爆红
查看原帖
90分求助 13 15测试点爆红
1413897
wangyurun074楼主2024/10/13 14:40
#include <iostream>
#include <string>
using namespace std;
void changestr(char *str,int char_index,int end_index);
void delet(char *str,int char_index,int end_index)
{
	int i = 0; 
	char c = str[char_index];//'.' 和 '/' 
	if(char_index != 0)
	{
		while(1)
	    {
	    //覆盖删除 但是要判断是否还有多余0 
	      if((str[i] != '0')||(str[0]=='0'&& str[1] == c)) 
		  {
	    	break;
		  }else if(str[i] == '0') 
		  {
			for(int j = i;j < end_index+1;j++)
	        {str[j] = str[j+1];}
	        i--;
		  }
		  i++;
	    }
	}else{
		//注%不参与交换 
		for(i = 0;str[i+1] !='\0';i++)//最后一个字符不参与替换 
		{
			if(str[i] != '0')
			{
				break;		
			}else if(str[i] == '0' )
			{
			for(int j = i;j < end_index+1;j++)
			{
			    str[j] = str[j+1];
			}
			    i--;
			}
		}
	}
}
int main(void)
{
 	string str;
 	cin >> str;
 	int char_index = 0;
 	int end_index = str.size()-1;//减去后就恰好为最后一个字符的下标 
 	//判断数据类型 整数/小数/分数 
 	if(str.find('.') != string::npos)
 	{
        char_index = str.find('.');
		changestr(&str[0],char_index,end_index);
		//if(str.find('0') != 0 && end_index != 3)
		delet(&str[0],char_index,end_index); 
	}else if(str.find('%') != string::npos)
	{
	    //if(str.find('0') != 0 && end_index != 0)
        changestr(&str[0],char_index,end_index - 1);
        char_index = str.find('%');
        delet(&str[0],char_index,end_index);
	}else if(str.find('/') != string::npos)
	{
		char_index = str.find('/');
		changestr(&str[0],char_index,end_index);
		delet(&str[0],char_index,end_index);
	}else{
		changestr(&str[0],char_index,end_index);
        delet(&str[0],char_index,end_index);
	} 
	cout << str;
}
void changestr(char *str,int char_index,int end_index)
{ 
    //小数,分数分开反转 
	if(char_index != 0)
	{
		int i;
	    int j = char_index - 1;
	//第一部分反转
		for(i = 0;j-i >= 1;i++)
	{
		int temp = str[i];
		str[i] = str[j];
		str[j] = temp;
		j--;
	} 
	//第二部分反转
	j = end_index;
	int length_point = end_index - char_index; 
	for(i = char_index+1;j-i > 0;i++)
	{
		int temp = str[i];
		str[i] = str[j];
		str[j] = temp;
		j--;
	}
	//第二部分 删除0 
    if(str[end_index] == '0')
    {
    	for(i = end_index;i > char_index+1;i--)//不用考虑小数点后1位的删除 
	    {
		   if(str[i] == '0')
		   {
			str[i] = '\0';
		   }else{
		   	break;
		   }
	   }
	}
	}else{
		int j = end_index;
		for(int i = 0;j-i >= 1;i++)
		{
			int temp = str[i];
		    str[i] = str[j];
		    str[j] = temp;
		    j--;
		} 
    }
}

没下载次数了呜呜呜 我尽力把代码整理 标上注释了 大佬们轻喷

2024/10/13 14:40
加载中...