UVa 11809 floating-point Numbers (floating-point number) __uva

Source: Internet
Author: User
Tags goto mixed pow stdin


Meaning

This paper first introduces the storage---of floating-point numbers by binary storage. A floating-point number (n) consists of the mantissa (Mantissa) and the Order (exponent). Suppose the digits of the mantissa and the order are M and E, respectively. In order from left to right, the first is the mantissa sign bit (0 for positive numbers, 1 for negative numbers), followed by the M-digit tail number part, because the storage time the tail number of points must belong to the interval [0.5,1), so the number of M-digit part of the first is the overall second must be 1. When the M-bit mantissa is stored, the m+1 bit is the sign bit of the order (0 for positive numbers and 1 for negative numbers). The remaining e bits are then used to hold the order values.

For example: When m=8,e=6, the maximum floating-point number (binary) that can be represented is;

Using binary and decimal conversion rules can be converted into:

Now give a maximum floating-point number (with scientific notation AEB, and a valid number 15 bits), ask how many digits m and order digits E in the store.

where m takes the value interval [the 0,9],e interval [1,30].

The test data is about 300 lines, and 0e0 ends the input.

Analysis:

Obviously for 300 lines of input data and E is likely to be very large, if one calculation, there will be a large number of repeated computations. You can use the table-punching method:

Preprocessing each m and each e corresponds to the maximum number of floating-point numbers, a total of 300 matches, and then for each input, find it.

A[I][J] represents the maximum number of floating-point numbers that are stored when the number of digits is J. The maximum floating-point number is 1 in addition to the remaining digits of the symbol bit.

The tail number is stored in the value of M[i], and the order part stores the value as E[i][j]. Binary and decimal conversions are available: m[i]=1-(1/2) ^ (i+1); e[j]=2^j-1; a[i][j]=m[i]*2^ (E[J)

However, if this direct storage value is too large, the logarithm can be obtained a[i][j]=>log10 (M[i]) +e[j]*log10 (2), so you can hit the table.

If the input is expressed in AEB-a*10^b, the logarithm can also be log10 (A) +b=t;

For each input just look for a[i][j]==t, output the corresponding m[i] and e[j].
As for a,b how to get to use the SSCANF function.

Use of the SSCANF function:

SSCANF ()-Reads data from a string that matches the specified format.
Function Prototypes:
Int sscanf (String str, String fmt, mixed var1, mixed var2 ...);
int scanf (const char *format [, argument] ...);
Description
SSCANF is similar to scanf and is used for input, except that the latter takes the screen (stdin) as the input source, with a fixed string as the input source.
The format can be one or more {%[*] [width] [{h | l | I64 | L}]type | ' ' | ' t ' | ' \ n ' | Non-% symbol}
Note:
1, * can also be used in the format, (that is,%*d and%*s) with an asterisk (*) indicates that skipping this data is not read. (that is, do not read this data into the parameter)
2. {A|b|c} indicates that A,b,c is selected, [d], that it can have D or not d.
3. Width represents the reading breadth.
4, {h | l | I64 | L}: The size of the parameter, usually H represents a single-byte Size,i 2 byte size,l represents 4 byte size (double exception), and L64 represents 8 byte size.
5, type: This is a lot of, is%s,%d and so on.
6, Special:%*[width] [{h | l | I64 | L}]type indicates that the condition is filtered out and the value is not written to the target parameter
Support for collection operations:
%[a-z] means to match any character in a to Z, greedy (as many matches as possible)
%[ab '] Match A, B, ' a member, greedy sex
%[^a] matches any character that is not a, greedy


Example:
1. Common usage.
char buf[512] =;
SSCANF ("123456", "%s", buf);
printf ("%s\n", buf);
Results are: 123456
2. Take the specified length of the string. In the following example, take a string with a maximum length of 4 bytes.
SSCANF ("123456", "%4s", buf);
printf ("%s\n", buf);
Results are: 1234
3. A string that takes the specified character. In the following example, the string is encountered as a space.
SSCANF ("123456 Abcdedf", "%[^]", buf);
printf ("%s\n", buf);
Results are: 123456
4. Takes a string containing only the specified character set. In the following example, take a string that contains only 1 to 9 and lowercase letters.
SSCANF ("123456abcdedfBCDEF", "%[1-9a-z]", buf);
printf ("%s\n", buf);
The result is: 123456ABCDEDF
5. A string that is taken to the specified character set. In the following example, take the string that is encountered in uppercase letters.
SSCANF ("123456abcdedfBCDEF", "%[^a-z]", buf);
printf ("%s\n", buf);
The result is: 123456ABCDEDF
6, given a string iios/12ddwdff@122, get/And the string between @, first the "iios/" filtered out, and then a string of not ' @ ' sent to the BUF
SSCANF ("iios/12ddwdff@122", "%*[^/]/%[^@]", buf);
printf ("%s\n", buf);
The result is: 12DDWDFF
7, given a string "Hello, World", just keep the world. (Note: "," after a space)
SSCANF ("Hello, World", "%*s%s", buf);
printf ("%s\n", buf);
The result: World
%*s indicates that the first match to%s was filtered out, that is, Hello was filtered
If there are no spaces, the result is null.
SSCANF functions are similar to regular expressions but do not have a strong regular expression, so it is recommended that you use regular expressions for more complex string processing.


#include <stdio.h>
#include <math.h> 
int i, J;
Double a[10][31],m[10],e[31];
void init () 
{
	for (i = 0;i < 10;i++)
		m[i] = 1-pow (0.5, (i + 1));
	for (j = 1;j < 31;j++)
		e[j] = POW (2,j)-1;
	for (i = 0;i < 10;i++) for
		(j = 1;j < 31;j++)
			a[i][j] = log10 (M[i]) + e[j] * LOG10 (2);
}
int main ()
{
	//freopen ("In.txt", "R", stdin);
	Freopen ("OUT.txt", "w", stdout);
	Init ();
	Double a,t;
	int B;
	Char s[25];
	while (~scanf ('%s ', s))
	{for
		(i = 0;;; i++)
			if (s[i] = = ' E ')
			{
				s[i] = ';
				break;
			E-Empty is to facilitate the reading of A and B
		sscanf (S, "%lf%d", &a, &b);
		if (a==0&&b==0) break;//can also be added at scanf read Time "&&strcmp (S," 0e0 ")!=0" to judge
		t = log10 (A) +b;
		for (i = 0;i < 10;i++) for
			(j = 1;j < 31;j++)
				if (Fabs (a[i][j)-T) < 1e-4) goto loop;//Allow actual error <10^ (1 e-4)
		Loop:
			printf ("%d%d\n", I, j);
	}
	return 0;
}

Of course, to limit the goto statement, you can write this:

for (i = 0;i < 10;i++)
{for
	(j = 1;j < 31;j++)
	{
		if (fabs (a[i][j)-T) < 1e-4)
		{
			flag = 1;
			break;
		}
	}
	if (falg) break;
printf ("%d%d\n", I, j);


Summarize:

① Play Table

② decimal and binary conversions

③ logarithm, use of the log10 () function

④sscanf function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.