| |

VerySource

 Forgot password?
 Register
Search
View: 797|Reply: 8

a problem

[Copy link]

2

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-1-4 08:00:01
| Show all posts |Read mode
For any input number less than 1, find its number and output it separately. It is required to leave 2 spaces between the output numbers. For example, input 0.435 and the output is 0 4 3 5



#include <iostream>
using namespace std;
int main ()
{
float x;
int s;
cin >> x;
cout << 0 << "";
while (x! = 0)
{
s = x * 10;
cout << s << "";
x = x * 10-s;
}
return 0;
}





Why did I enter 0.435
The output is: 0 4 3 5 0 0 0 0 0 2 3 8 4

If I change the loop condition to while (x> 1e-6)
The output is the same. . .

what's up?
Reply

Use magic Report

0

Threads

17

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

Post time: 2020-1-4 12:06:01
| Show all posts
Because the 0.435 you entered becomes 0.435000002384 in the inner
Reply

Use magic Report

0

Threads

4

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 United States

Post time: 2020-1-4 12:15:01
| Show all posts
Still good after converting to a string
Reply

Use magic Report

0

Threads

17

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

Post time: 2020-1-4 12:18:01
| Show all posts
To determine whether a floating-point number is equal to 0, you cannot use the condition such as! = 0, but should use
x <eps&&x> -eps
Where eps is a sufficiently small value, you can take it as 0.000001 or other
Reply

Use magic Report

0

Threads

7

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-1-4 12:21:01
| Show all posts
The problem with floating point numbers
Change to
while (x <0.0001)
Reply

Use magic Report

0

Threads

4

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 United States

Post time: 2020-1-4 12:27:01
| Show all posts
char * pch = new char;

    sprintf (pch, "% f", x);
// Process the string pch. Read each character. Pay attention to the case of all zeros at the end.
...


delete pch;
Reply

Use magic Report

0

Threads

4

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 United States

Post time: 2020-1-4 14:21:01
| Show all posts
#include <iostream>
#include <string>
using namespace std;

int main ()
{

string str;
cin >> str;

for (string :: size_type ix = 0; ix! = str.size (); ++ ix)
{
    if (str [ix]! = '.')
    {
        cout << str [ix] << "";
    }
}

return 0;
}
Reply

Use magic Report

2

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-1-4 16:06:01
| Show all posts
Oh. . thank you all. . . But the program upstairs did not understand. . .

for (string :: size_type ix = 0; ix! = str.size (); ++ ix)



Ok. . . It seems to be just to convert it to a string. .
It would not work if the loop condition was changed to while (x <1e-6&&x> -1e-6). .
Reply

Use magic Report

0

Threads

17

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

Post time: 2020-1-4 16:48:01
| Show all posts
The landlord can change the condition to while (x <1e-4&&x> -1e-4) and try it, because according to your previous results,
while (x <1e-6&&x> -1e-6) will not exit. If not, you can change it to a bigger one.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list