Luo Gu P1553- * digit reversal upgraded version [string] Category talk

Meaning of the questions:

Given a number, please on the digital number obtained by inverting each bit of a new number.

The popularity of the group and NOIp2011 the first question is different: this number can be decimals, fractions, percentages, integer. The inversion is an integer number of all bit swap; fractional inversion is the integer portion of the inverted, then the fractional portion of the number of inversion, without exchanging the integer part and fractional part; fractional inversion inversion is the denominator, and then the number of molecules of the inversion, not to exchange the numerator and denominator; percent of molecules must be an integer, the percentage change only the digital section. New integer number should also meet common form of integers that, unless given the original number is zero, otherwise obtained by inverting the highest digit of the new number should not be zero; the end of the new decimal number is not 0 (unless the fractional part no other addition to the number 0, then retaining only a 0); do not score points about the numerator and denominator are not decimal (approximately droplet shoes Sorry, not too oh input data to ensure that the denominator is not 0), this does not. negative number.

Now given a number, the number of the digital please obtained by inverting each bit of a new number.

The popularity of the group and NOIp2011 the first question is different: this number can be decimals, fractions, percentages, integer.

Integer reverse swap is all digital.

Decimal integer part of the reversal is inverted, then the fractional portion of the number of inversion, without exchanging the integer portion and the decimal portion.

Score is the inverted denominator is reversed, then the number of molecules is reversed, without exchanging the numerator and denominator.

Percent of molecules must be an integer, the percentage change only the digital section.

The input format
a number ss

Output format
of a number, i.e. the number of inverted ss

Sample Input Output
Input # 1 replication
5,087,462
Output # 1 replication
2,647,805
Input Copy # 2
600.084
output copy # 2
6.48
Input # 3 replicate
700/27
output copy # 3
7/72
input copy # 4
8670%
output copy # 4
768%
Description / Tip
all the data: 25% s is an integer not greater than 20

25% s is a decimal fraction, integer and fractional parts are not larger than 10

25% s is a fraction, the numerator and denominator are not greater than 10

25% s are percent, no greater than 19 molecules

(20 data)

Data guarantee:

For integer flip, the original integer number and the number of new integer integer meet common form, that is unless given the original number is zero, otherwise obtained by inverting the highest-digit number of new and original figures should not be zero.

For decimal flip, the section above, form part of the front of the decimal point behind the decimal point, to ensure that meet common form of decimals, that is not the end of the extra 00 (except fractional part of 00 there is no other number, then retain only 11 00. If after the inversion number appears at the end of 00, omit redundant 00)

For scores Flip, the score is not about points, not the numerator and denominator of the fraction. Enter the denominator is not zero. Flip the relevant provisions of the integer see above.

For flip percentage terms, see the integer flip relevant content.

Negative data does not exist.

answer:

Divided into a total of four cases, are integers, decimals, fractions, percentages, we need to detect this figure type, you can start from the left most number of sweeping encountered \ 0, decimal point, in addition to numbers, do the percent sign after marking out of the loop, and the position of the mark.
1, if it is an integer, starting with the most sweeping left to the right, find the first non-zero digit, 0 output only if all are a zero, otherwise a non-zero starting from the first reverse digital output
2, if it is decimal decimal point as a boundary, the integer part of the left repeat step 1, the right of the decimal point of decimal is swept from right to find the first non-zero number, and the fact 1 Similarly
3, if the score is, and 2 methods as flip seemingly but there is a huge pit, the right is the digital division sign should be reversed in accordance with the integer program, rather than the reverse of fractional program. If the operation is a direct copy 2 will leave the pit (such as the flip is 21/31 120/130)
4, if the percentage is, the general idea and the same 1, the final output can Percent

Code:

#include<stdio.h>
#include<string.h>
int main()
{
	char ch[100];
	scanf("%s",ch);
	int k=0,kind=0,fir1=0,fir2=0;
	while(1)
	{
		if(ch[k]=='.')
		{
			kind=1;
			break;
		}
		if(ch[k]=='/')
		{
			kind=2;
			break;
		}
		if(ch[k]=='\0')
		{
			kind=0;
			break;
		}
		if(ch[k]=='%')
		{
			kind=3;
			break;
		}
		k++;
	}
	fir1=k-1;
	fir2=k+1;
	while(ch[fir1]=='0')
	{
		fir1--;
		if(fir1<0)
		{
			fir1=k-1;
			break;
		}
	}
	if(kind==1)while(ch[fir2]=='0')
	{
		fir2++;
		if(fir2>=strlen(ch))
		{
			fir2=k+1;
			break;
		}
	}
	if(kind==2)
	{
		fir2=strlen(ch)-1;
		while(ch[fir2]=='0')
		{
			fir2--;
			if(fir2<=k)
			{
				fir2=k+1;
				break;
			}
		}
	}
	for(int i=fir1;i>=0;i--)printf("%c",ch[i]);
	if(kind==3)printf("%%");
	else if(kind==1)printf(".");
	else if(kind==2)printf("/");
	if(kind==1)for(int i=strlen(ch)-1;i>=fir2;i--)printf("%c",ch[i]);
	if(kind==2)for(int i=fir2;i>=k+1;i--)printf("%c",ch[i]);
	printf("\n");
}
Released eight original articles · won praise 1 · views 326

Guess you like

Origin blog.csdn.net/weixin_42921101/article/details/104309613