YTUOJ 2310: String Operation Four (String)

Question description

Enter a decimal in the form of a string, remove its precision, and make it accurate to 2 digits after the decimal point. There are multiple sets of test data, input and output at one time.

Sample input

20


1.23

2.555

2.534

1.22222222

Sample output

20.00

1.23

2.56

2.53

1.22

This question is not complicated. The main thing is to comprehensively consider various results.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
    
    
	char a[120];
	int i,n,point=0,l;
	while(gets(a))
	{
    
    
		l=strlen(a);
		for(i=0;i<l;i++) //寻找小数点位置
		{
    
    
			if(a[i]=='.')
			{
    
    
				point=i;
				break;
			}
		}
		if(i==l)
			point=0;
		printf("---%d\n",point);
		if(point==0) //最简单的情况//没有小数点的情况,直接输出,后加.00   
		{
    
    
			printf("%s.00\n",a);
		}
		else //有小数点需要分好几种情况处理
		{
    
    
			//当数字为XX.99Y这种情况,及进一位为XX.00/
			if(a[point+1]=='9' && a[point+2]=='9' && a[point+3]>='5') 
			{
    
    
				//a[point-1] = (a[point-1]+1)%10;
				for(i=point-1;i>=0;i--)
				{
    
    
					a[i] = a[i]+1;
					if(a[i]!='9'+1)
						break;
					else
						a[i]='0';
				}
				for(i=0;i<=point;i++)
					printf("%c",a[i]);
				printf("00\n");
			}
			
			else if(a[point+2]=='\0') // 即只有一位小数的情况
			{
    
    
				for(i=0;a[i]!='\0';i++)
					printf("%c",a[i]);
				printf("0\n");
			}
			else //普通情况
			{
    
    
				for(i=l-1;i>point+2;i--)  
				{
    
    
					if(a[i]>='5')
					{
    
    
						if(a[i-1]<='8')
							a[i-1] += 1;
						else
							a[i-1] = '0';
					}
				}
				for(i=0;i<=point+2;i++)
					printf("%c",a[i]);
				printf("\n");
			}
		}
		printf("\n");
		getchar();
	}
	system("pause");
	return 0;
}

Although AC is handed in, after thinking about it, I found that this code has a bug. If a number is x.994Yxxxxx, and if the value of Y is 5 after normal processing, it needs to be further increased. The correct answer should be (x+1) .00, while the above code outputs x.90.

After thinking about it, I came up with a solution. Since I didn’t use functions, recursion and other methods at the beginning, but just threw it all into the main function, it was a little troublesome here.

The idea is as follows:

After ordinary processing, add a step to determine whether it is x.90Y. If Y>=5, a carry is needed. The correct answer should be (x+1).00

code show as below:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
    
    
	char a[120];
	int i,n,point=0,l;
	while(gets(a))
	{
    
    
		l=strlen(a);
		for(i=0;i<l;i++) //寻找小数点位置
		{
    
    
			if(a[i]=='.')
			{
    
    
				point=i;
				break;
			}
		}
		if(i==l)
			point=0;
		//printf("---%d\n",point);
		if(point==0) //最简单的情况//没有小数点的情况,直接输出,后加.00   
		{
    
    
			printf("%s.00\n",a);
		}
		else //有小数点需要分好几种情况处理
		{
    
    
			//当数字为XX.99Y这种情况,及进一位为XX.00/
			if(a[point+1]=='9' && a[point+2]=='9' && a[point+3]>='5') 
			{
    
    
				//a[point-1] = (a[point-1]+1)%10;
				for(i=point-1;i>=0;i--)
				{
    
    
					a[i] = a[i]+1;
					if(a[i]!='9'+1)
						break;
					else
						a[i]='0';
				}
				for(i=0;i<=point;i++)
					printf("%c",a[i]);
				printf("00\n");
			}
			
			else if(a[point+2]=='\0') // 即只有一位小数的情况
			{
    
    
				for(i=0;a[i]!='\0';i++)
					printf("%c",a[i]);
				printf("0\n");
			}
			else //普通情况
			{
    
    
				for(i=l-1;i>point+2;i--)  
				{
    
    
					if(a[i]>='5')
					{
    
    
						if(a[i-1]<='8')
							a[i-1] += 1;
						else
							a[i-1] = '0';
					}
				}
				///
				//此部分为增加的判断
				if(a[point+1]=='9' && a[point+2]=='0' && a[point+3]>='5') 
				{
    
    
					//a[point-1] = (a[point-1]+1)%10;
					for(i=point-1;i>=0;i--)
					{
    
    
						a[i] = a[i]+1;
						if(a[i]!='9'+1)
							break;
						else
							a[i]='0';
					}
					for(i=0;i<=point;i++)
						printf("%c",a[i]);
					printf("00\n");
				}
				else
				{
    
    
					for(i=0;i<=point+2;i++)
						printf("%c",a[i]);
					printf("\n");
				}
				//
			}
		}
		printf("\n");
		getchar();
	}
	//system("pause");
	return 0;
}

The operation situation is as follows, all situations are consistent with the meaning of the question
Insert image description here

Guess you like

Origin blog.csdn.net/u014295602/article/details/103281175