user-0 提问: C++语言小白求助，关于零钱计算

回复:
user: user-1
content: 引用 7 楼 qq_36483087 的回复:我输入 0.36的时候，结果不对呀。 第一轮n=35， 第二轮 n=10，第三轮n=0；  第一轮 n应该是36呀 这个是浮点数的存储导致的，浮点数是以二进制存储的，0.36实际上是以0.35999999的形式，所以浮点到整型的强制转换会导致个位数的改变  解决方法： int n=(int)(rmn*100); 改成 int n=round(rmn*100);  round()在math头文件中，前面顺便加上  #include<math.h>

user: user-2
content: #include <stdio.h> #include <stdlib.h> #include <string.h> float f; double d; void showf() {     char bs[65];     char b[65];     unsigned char *p;     char e[12];     char *t;     int ex;      printf("\nf=%.6f\n",f);     p=(unsigned char *)&f;     printf("hex=%02X %02X %02X %02X\n",p[3],p[2],p[1],p[0]);     ltoa(*(long *)&f,b,2);     sprintf(bs,"%032s",b);     printf("bin=%s\n",bs);     printf("bin=%.1s %.8s   %s\n",bs,bs+1,bs+9);     strncpy(e,bs+1,8);e[8]=0;     ex=strtol(e,&t,2);     printf("    %c %-4d-127 1.%s\n",(bs[0]=='0')?'+':'-',ex,bs+9);     ex-=127;     printf("    %c %-8d 1.%s\n",(bs[0]=='0')?'+':'-',ex,bs+9); } void showd() {     char bs[65];     char b[65];     unsigned char *p;     char e[12];     char *t;     int ex;      printf("\nd=%.15lf\n",d);     p=(unsigned char *)&d;     printf("hex=%02X %02X %02X %02X %02X %02X %02X %02X\n",p[7],p[6],p[5],p[4],p[3],p[2],p[1],p[0]);     _i64toa(*(__int64 *)&d,b,2);     sprintf(bs,"%064s",b);     printf("bin=%s\n",bs);     printf("bin=%.1s %.11s   %s\n",bs,bs+1,bs+12);     strncpy(e,bs+1,11);e[11]=0;     ex=strtol(e,&t,2);     printf("    %c %-6d-1023 1.%s\n",(bs[0]=='0')?'+':'-',ex,bs+12);     ex-=1023;     printf("    %c %-11d 1.%s\n",(bs[0]=='0')?'+':'-',ex,bs+12); } int main() {      for (f=0.0f;f<=2.2f;f+=0.2f) showf();     printf("------------\n");     f=2.0f;showf();     printf("-----------------------------------\n");     for (d=0.0 ;d<=2.2 ;d+=0.2 ) showd();     printf("------------\n");     d=2.0 ;showd();      return 0; } // //f=0.000000 //hex=00 00 00 00 //bin=00000000000000000000000000000000 //bin=0 00000000   00000000000000000000000 //  + 0   -127 1.00000000000000000000000 //  + -127     1.00000000000000000000000 // //f=0.200000 //hex=3E 4C CC CD //bin=00111110010011001100110011001101 //bin=0 01111100   10011001100110011001101 //  + 124 -127 1.10011001100110011001101 //  + -3       1.10011001100110011001101 // //f=0.400000 //hex=3E CC CC CD //bin=00111110110011001100110011001101 //bin=0 01111101   10011001100110011001101 //  + 125 -127 1.10011001100110011001101 //  + -2       1.10011001100110011001101 // //f=0.600000 //hex=3F 19 99 9A //bin=00111111000110011001100110011010 //bin=0 01111110   00110011001100110011010 //  + 126 -127 1.00110011001100110011010 //  + -1       1.00110011001100110011010 // //f=0.800000 //hex=3F 4C CC CD //bin=00111111010011001100110011001101 //bin=0 01111110   10011001100110011001101 //  + 126 -127 1.10011001100110011001101 //  + -1       1.10011001100110011001101 // //f=1.000000 //hex=3F 80 00 00 //bin=00111111100000000000000000000000 //bin=0 01111111   00000000000000000000000 //  + 127 -127 1.00000000000000000000000 //  + 0        1.00000000000000000000000 // //f=1.200000 //hex=3F 99 99 9A //bin=00111111100110011001100110011010 //bin=0 01111111   00110011001100110011010 //  + 127 -127 1.00110011001100110011010 //  + 0        1.00110011001100110011010 // //f=1.400000 //hex=3F B3 33 34 //bin=00111111101100110011001100110100 //bin=0 01111111   01100110011001100110100 //  + 127 -127 1.01100110011001100110100 //  + 0        1.01100110011001100110100 // //f=1.600000 //hex=3F CC CC CE //bin=00111111110011001100110011001110 //bin=0 01111111   10011001100110011001110 //  + 127 -127 1.10011001100110011001110 //  + 0        1.10011001100110011001110 // //f=1.800000 //hex=3F E6 66 68 //bin=00111111111001100110011001101000 //bin=0 01111111   11001100110011001101000 //  + 127 -127 1.11001100110011001101000 //  + 0        1.11001100110011001101000 // //f=2.000000 //hex=40 00 00 01 //bin=01000000000000000000000000000001 //bin=0 10000000   00000000000000000000001 //  + 128 -127 1.00000000000000000000001 //  + 1        1.00000000000000000000001 //------------ // //f=2.000000 //hex=40 00 00 00 //bin=01000000000000000000000000000000 //bin=0 10000000   00000000000000000000000 //  + 128 -127 1.00000000000000000000000 //  + 1        1.00000000000000000000000 //----------------------------------- // //d=0.000000000000000 //hex=00 00 00 00 00 00 00 00 //bin=0000000000000000000000000000000000000000000000000000000000000000 //bin=0 00000000000   0000000000000000000000000000000000000000000000000000 //  + 0     -1023 1.0000000000000000000000000000000000000000000000000000 //  + -1023       1.0000000000000000000000000000000000000000000000000000 // //d=0.200000000000000 //hex=3F C9 99 99 99 99 99 9A //bin=0011111111001001100110011001100110011001100110011001100110011010 //bin=0 01111111100   1001100110011001100110011001100110011001100110011010 //  + 1020  -1023 1.1001100110011001100110011001100110011001100110011010 //  + -3          1.1001100110011001100110011001100110011001100110011010 // //d=0.400000000000000 //hex=3F D9 99 99 99 99 99 9A //bin=0011111111011001100110011001100110011001100110011001100110011010 //bin=0 01111111101   1001100110011001100110011001100110011001100110011010 //  + 1021  -1023 1.1001100110011001100110011001100110011001100110011010 //  + -2          1.1001100110011001100110011001100110011001100110011010 // //d=0.600000000000000 //hex=3F E3 33 33 33 33 33 34 //bin=0011111111100011001100110011001100110011001100110011001100110100 //bin=0 01111111110   0011001100110011001100110011001100110011001100110100 //  + 1022  -1023 1.0011001100110011001100110011001100110011001100110100 //  + -1          1.0011001100110011001100110011001100110011001100110100 // //d=0.800000000000000 //hex=3F E9 99 99 99 99 99 9A //bin=0011111111101001100110011001100110011001100110011001100110011010 //bin=0 01111111110   1001100110011001100110011001100110011001100110011010 //  + 1022  -1023 1.1001100110011001100110011001100110011001100110011010 //  + -1          1.1001100110011001100110011001100110011001100110011010 // //d=1.000000000000000 //hex=3F F0 00 00 00 00 00 00 //bin=0011111111110000000000000000000000000000000000000000000000000000 //bin=0 01111111111   0000000000000000000000000000000000000000000000000000 //  + 1023  -1023 1.0000000000000000000000000000000000000000000000000000 //  + 0           1.0000000000000000000000000000000000000000000000000000 // //d=1.200000000000000 //hex=3F F3 33 33 33 33 33 33 //bin=0011111111110011001100110011001100110011001100110011001100110011 //bin=0 01111111111   0011001100110011001100110011001100110011001100110011 //  + 1023  -1023 1.0011001100110011001100110011001100110011001100110011 //  + 0           1.0011001100110011001100110011001100110011001100110011 // //d=1.400000000000000 //hex=3F F6 66 66 66 66 66 66 //bin=0011111111110110011001100110011001100110011001100110011001100110 //bin=0 01111111111   0110011001100110011001100110011001100110011001100110 //  + 1023  -1023 1.0110011001100110011001100110011001100110011001100110 //  + 0           1.0110011001100110011001100110011001100110011001100110 // //d=1.600000000000000 //hex=3F F9 99 99 99 99 99 99 //bin=0011111111111001100110011001100110011001100110011001100110011001 //bin=0 01111111111   1001100110011001100110011001100110011001100110011001 //  + 1023  -1023 1.1001100110011001100110011001100110011001100110011001 //  + 0           1.1001100110011001100110011001100110011001100110011001 // //d=1.800000000000000 //hex=3F FC CC CC CC CC CC CC //bin=0011111111111100110011001100110011001100110011001100110011001100 //bin=0 01111111111   1100110011001100110011001100110011001100110011001100 //  + 1023  -1023 1.1100110011001100110011001100110011001100110011001100 //  + 0           1.1100110011001100110011001100110011001100110011001100 // //d=2.000000000000000 //hex=3F FF FF FF FF FF FF FF //bin=0011111111111111111111111111111111111111111111111111111111111111 //bin=0 01111111111   1111111111111111111111111111111111111111111111111111 //  + 1023  -1023 1.1111111111111111111111111111111111111111111111111111 //  + 0           1.1111111111111111111111111111111111111111111111111111 // //d=2.200000000000000 //hex=40 01 99 99 99 99 99 99 //bin=0100000000000001100110011001100110011001100110011001100110011001 //bin=0 10000000000   0001100110011001100110011001100110011001100110011001 //  + 1024  -1023 1.0001100110011001100110011001100110011001100110011001 //  + 1           1.0001100110011001100110011001100110011001100110011001 //------------ // //d=2.000000000000000 //hex=40 00 00 00 00 00 00 00 //bin=0100000000000000000000000000000000000000000000000000000000000000 //bin=0 10000000000   0000000000000000000000000000000000000000000000000000 //  + 1024  -1023 1.0000000000000000000000000000000000000000000000000000 //  + 1           1.0000000000000000000000000000000000000000000000000000 //

user: user-2
content: 浮点数格式 http://bbs.csdn.net/topics/390676437

user: user-0
content: 我输入 0.36的时候，结果不对呀。 第一轮n=35， 第二轮 n=10，第三轮n=0；  第一轮 n应该是36呀

user: user-0
content: 引用 2 楼 u012021393 的回复:发错了，这个才是  void do_print(double rmn){      int i=0 ,ttd=0, td=0, fd=0,od=0,ftc=0,tfc=0,tc=0,fc=0,oc=0;     int n=(int)(rmn*100);     while(n>0){         cout << " before :ooooooo "<<rmn<<endl;          if(n>=2000){             n = n -2000;             ttd++;             //break;             }             else if(n<2000&&n>=1000){                 n = n -1000;                 td++;                // break;             }                 else if(n<1000&&n>=500){                     n = n -500;                     fd++;                     //break;                 }                     else if(n<500&&n>=100){                         n =n -100;                         od++;                        // break;                     }                         else if(n<100&&n>=50){                             n=n-50;                             ftc ++;                            // break;                         }                             else if(n<50&&n>=25){                                 n =n -25;                                 tfc ++;                                // break;                             }                                 else if(n<25&&n>=10){                                     n=n-10;                                     tc++;                                     //break;                                 }                                     else if(n<10&&n>=5){                                          n=n-5;                                         fc++;                                         cout << "74"<<n<<endl;                                        // break;                                     }                                         else if(n<5&&n>=1){                                                n=n-1;                                             oc++;                                             //break;                                         }                                         //else ;                            cout << " aaaaT:ooooooo "<<rmn<<endl;                         }      //cout << " T: "<<rmn<<endl;     cout << " Twenty US Dollar Bills: "<<ttd<<endl;     cout << " Ten US Dollar Bills: "<<td<<endl;     cout << " Five US Dollar Bills: "<<fd<<endl;     cout << " One US Dollar Bills: "<<od<<endl;     cout << " Fifty US Cents Coins: "<<ftc<<endl;     cout << " Twenty-Five US Cents Coins: "<<tfc<<endl;     cout << " Ten US Cents Coins: "<<tc<<endl;     cout << " Five US Cents Coins: "<<fc<<endl;     cout << " One US Cent Coins: "<<oc<<endl;  }  发错了，这个才是   我输入 0.36的时候，结果不对呀。 第一轮n=35， 第二轮 n=10，第三轮n=0；  第一轮 n应该是36呀

user: user-3
content: 二进制小数不能精确表示 1/10  就像  十进制小数不能精确表示 1/3

user: user-4
content: 大部分浮点数无法精确存储，只是存储一个近似值。 http://blog.chinaunix.net/uid-23204219-id-1761337.html

user: user-0
content: 引用 2 楼 u012021393 的回复:发错了，这个才是  void do_print(double rmn){      int i=0 ,ttd=0, td=0, fd=0,od=0,ftc=0,tfc=0,tc=0,fc=0,oc=0;     int n=(int)(rmn*100);     while(n>0){         cout << " before :ooooooo "<<rmn<<endl;          if(n>=2000){             n = n -2000;             ttd++;             //break;             }             else if(n<2000&&n>=1000){                 n = n -1000;                 td++;                // break;             }                 else if(n<1000&&n>=500){                     n = n -500;                     fd++;                     //break;                 }                     else if(n<500&&n>=100){                         n =n -100;                         od++;                        // break;                     }                         else if(n<100&&n>=50){                             n=n-50;                             ftc ++;                            // break;                         }                             else if(n<50&&n>=25){                                 n =n -25;                                 tfc ++;                                // break;                             }                                 else if(n<25&&n>=10){                                     n=n-10;                                     tc++;                                     //break;                                 }                                     else if(n<10&&n>=5){                                          n=n-5;                                         fc++;                                         cout << "74"<<n<<endl;                                        // break;                                     }                                         else if(n<5&&n>=1){                                                n=n-1;                                             oc++;                                             //break;                                         }                                         //else ;                            cout << " aaaaT:ooooooo "<<rmn<<endl;                         }      //cout << " T: "<<rmn<<endl;     cout << " Twenty US Dollar Bills: "<<ttd<<endl;     cout << " Ten US Dollar Bills: "<<td<<endl;     cout << " Five US Dollar Bills: "<<fd<<endl;     cout << " One US Dollar Bills: "<<od<<endl;     cout << " Fifty US Cents Coins: "<<ftc<<endl;     cout << " Twenty-Five US Cents Coins: "<<tfc<<endl;     cout << " Ten US Cents Coins: "<<tc<<endl;     cout << " Five US Cents Coins: "<<fc<<endl;     cout << " One US Cent Coins: "<<oc<<endl;  }  发错了，这个才是  聪明啊 好哒，谢谢拉。 电脑中0.1不是完全的0.1好像都是个约值。 您能说说这个吗？

user: user-5
content: 太复杂了，其实代码是这样的。至于浮点之类的，乘以100转换为整数就可以了。 int cc(int v, int *s, int count) { 	if (v == 0) return 1; 	if (v < 0 || count == 0) return 0; 	return cc(v, &s[1], count - 1) + cc(v - s[0], s, count); }  int test_cc() { /*把1元钱换成50分 20分 10分 5分 2分 1分的方法*/ 	int s[] = { 50, 20 , 10, 5, 2, 1 }; 	int v = 100; 	int count = 6; 	int sum = cc(v, s, count); 	printf("sum=%d\n", sum); }

user: user-1
content: 发错了，这个才是  void do_print(double rmn){      int i=0 ,ttd=0, td=0, fd=0,od=0,ftc=0,tfc=0,tc=0,fc=0,oc=0;     int n=(int)(rmn*100);     while(n>0){         cout << " before :ooooooo "<<rmn<<endl;          if(n>=2000){             n = n -2000;             ttd++;             //break;             }             else if(n<2000&&n>=1000){                 n = n -1000;                 td++;                // break;             }                 else if(n<1000&&n>=500){                     n = n -500;                     fd++;                     //break;                 }                     else if(n<500&&n>=100){                         n =n -100;                         od++;                        // break;                     }                         else if(n<100&&n>=50){                             n=n-50;                             ftc ++;                            // break;                         }                             else if(n<50&&n>=25){                                 n =n -25;                                 tfc ++;                                // break;                             }                                 else if(n<25&&n>=10){                                     n=n-10;                                     tc++;                                     //break;                                 }                                     else if(n<10&&n>=5){                                          n=n-5;                                         fc++;                                         cout << "74"<<n<<endl;                                        // break;                                     }                                         else if(n<5&&n>=1){                                                n=n-1;                                             oc++;                                             //break;                                         }                                         //else ;                            cout << " aaaaT:ooooooo "<<rmn<<endl;                         }      //cout << " T: "<<rmn<<endl;     cout << " Twenty US Dollar Bills: "<<ttd<<endl;     cout << " Ten US Dollar Bills: "<<td<<endl;     cout << " Five US Dollar Bills: "<<fd<<endl;     cout << " One US Dollar Bills: "<<od<<endl;     cout << " Fifty US Cents Coins: "<<ftc<<endl;     cout << " Twenty-Five US Cents Coins: "<<tfc<<endl;     cout << " Ten US Cents Coins: "<<tc<<endl;     cout << " Five US Cents Coins: "<<fc<<endl;     cout << " One US Cent Coins: "<<oc<<endl;  }  发错了，这个才是

user: user-1
content: 最好别直接用浮点数，转换成int再用  #include <iostream>   using namespace std;   void do_print(double rmn);   int main() {       double value, buy, rmn, tmp;       cout << " Enter a value: ";     cin >> value;     cout << " Enter an amount of purchases made against this amount: ";     cin >> buy;     rmn = value - buy;     cout << " The remaining change is: " << rmn <<endl;     do_print(rmn);                   return 0; }   void do_print(double rmn){       int i=0 ,ttd=0, td=0, fd=0,od=0,ftc=0,tfc=0,tc=0,fc=0,oc=0;       while(rmn>0){         cout << " before :ooooooo "<<rmn<<endl;           if(rmn>=20){             rmn = rmn -20;             ttd++;             //break;             }             else if(rmn<20&&rmn>=10){                 rmn = rmn -10;                 td++;                // break;             }                 else if(rmn<10&&rmn>=5){                     rmn = rmn -5;                     fd++;                     //break;                 }                     else if(rmn<5&&rmn>=1){                         rmn =rmn -1;                         od++;                        // break;                     }                         else if(rmn<1&&rmn>=0.5){                             rmn=rmn-0.5;                             ftc ++;                            // break;                         }                             else if(rmn<0.5&&rmn>=0.25){                                 rmn =rmn -0.25;                                 tfc ++;                                // break;                             }                                 else if(rmn<0.25&&rmn>=0.1){                                     rmn=rmn-0.1;                                     tc++;                                     //break;                                 }                                     else if(rmn<0.1&&rmn>=0.05){                                           rmn=rmn-0.05;                                         fc++;                                         cout << "74"<<rmn<<endl;                                        // break;                                     }                                         else /*if(rmn<0.05&&rmn>=0.01)*/{                                                rmn=rmn-0.01;                                             oc++;                                             //break;                                         }                                         //else ;                                                       cout << " aaaaT:ooooooo "<<rmn<<endl;                         }       //cout << " T: "<<rmn<<endl;     cout << " Twenty US Dollar Bills: "<<ttd<<endl;     cout << " Ten US Dollar Bills: "<<td<<endl;     cout << " Five US Dollar Bills: "<<fd<<endl;     cout << " One US Dollar Bills: "<<od<<endl;     cout << " Fifty US Cents Coins: "<<ftc<<endl;     cout << " Twenty-Five US Cents Coins: "<<tfc<<endl;     cout << " Ten US Cents Coins: "<<tc<<endl;     cout << " Five US Cents Coins: "<<fc<<endl;     cout << " One US Cent Coins: "<<oc<<endl;   }