【问题描述】: 在计算机中,使用float或者double来存储小数是不能得到精确的。如果你希望得到精确计算结果,最好是用分数形式来表示小数。有限小数或者无限循环小数都可以转化为分数。比如: 0.9 = 9/10 0.333(3)= 1/3(括号中的数字表示是循环节) 当然
【问题描述】:
在计算机中,使用float或者double来存储小数是不能得到精确值的。如果你希望得到精确计算结果,最好是用分数形式来表示小数。有限小数或者无限循环小数都可以转化为分数。比如:
0.9 = 9/10
0.333(3)= 1/3(括号中的数字表示是循环节)
当然一个小数可以用好几种分数形式来表示。如:
0.333(3)= 1/3 = 3/9
给定一个有限小数或者无限循环小数,你能否以分母最小的分数形式来返回这个小数呢?如果输入为循环小数,循环节用括号标记出来。下面是一些可能的输入数据,如0.3、0.30、0.3(000)、0.3333(3333)、……
解法:
拿到这样一个问题,我们往往会从最简单的情况入手,因为所有的小数都可以分解成一个整数和一个纯小数之和,不妨只考虑大于0,小于1的纯小数,且暂时不考虑分子和分母的约分,先设法将其表示为分数形式,然后再进行约分。题目中输入的小数,要么为有限小数X=0.a1a2…an,要么为无限循环小数X=0.a1a2…an(b1b2…bm),X表示式中的字母a1a2…an,b1b2…bm都是0~9的数字,括号部分(b1b2…bm)表示循环节,我们需要处理的就是以上两种情况。
对于有限小数X=0.a1a2…an来说,这个问题比较简单,X就等于(a1a2…an)/10n。
对于无限循环小数X=0.a1a2…an(b1b2…bm)来说,其复杂部分在于小数点后同时有非循环部分和循环部分,我们可以做如下的转换:
X= 0.a1a2…an(b1b2…bm)
10n* X= a1a2…an.(b1b2…bm)
10n* X= a1a2…an+0.(b1b2…bm)
X =(a1a2…an+0.(b1b2…bm))/10n
对于整数部分a1a2…an,不需要做额外处理,只需要把小数部分转化为分数形式再加上这个整数即可。对于后面的无限循环部分,可以采用如下方式进行处理:
令Y=0. b1b2…bm,那么
10m *Y=b1b2…bm.(b1b2…bm)
10m *Y=b1b2…bm+0.(b1b2…bm)
10m *Y-Y=b1b2…bm
Y= b1b2…bm/(10m-1)
将Y代入前面的X的等式可得:
X=(a1a2…an+Y)/10n
=(a1a2…an+ b1b2…bm/(10m-1))/10n
=((a1a2…an)*(10m-1)+(b1b2…bm))/((10m-1)*10n)
至此,便可以得到任意一个有限小数或无限循环小数的分数表示,但是此时分母未必是最简的,接下来的任务就是让分母最小,即对分子和分母进行约分,这个相对比较简单。对于任意一个分数A/B,可以简化为(A/Gcd(A,B))/(B/Gcd(A,B)),其中Gcd函数为求A和B的最大公约数,这就涉及本书中的算法(2.7节“最大公约数问题”),其中有很巧妙的解法,请读者阅读具体的章节,这里就不再赘述。
综上所述,先求得小数的分数表示方式,再对其分子分母进行约分,便能够得到分母最小的分数表现形式。
例如,对于小数0.3(33),根据上述方法,可以转化为分数:
0.3(33)
=(3 *(102-1)+ 33)/((102-1)*10)
=(3*99+33)/990
= 1 / 3
对于小数0. 285714(285714),我们也可以算出:
0. 285714(285714)
= (285714 *(106-1)+ 285714)/ ((106-1)*106)
= (285714*999999 +285714)/ 999999000000
= 285714 / 999999
= 2/7
以下给出代码,简单实现:
void Cal(char* str) { char *p=str; char *q=str,*q1=str;//q和q1分别存储(和)的指针 while(*p!='\0') { if(*p=='(') { q=p; } if(*p==')') { q1=p; } p++; } if(q==q1) { cout<br> <br> <p><span>为了更完善,分两种情况:</span></p> <p><span>1、对于小数的情况,不用定义数组形式:</span></p> <p></p><pre class="brush:php;toolbar:false">#include <iostream> using namespace std; long long gcd(long long a, long long b) { if (a >1,b>>1) a >>= 1; b >>= 1; k++; } else // a为偶数,b为奇数,f(a,b)=f(a>>1,b) a >>= 1; } else { if ((b&1) == 0) // a为奇数,b为偶数,f(a,b)=f(a,b>>1) b >>= 1; else // a,b均是奇数,f(a,b)=f(a-b,b) a = a-b; } if (a <br> <br> <p><span>2、<span>用于大整数,定义了大整数类型,以及对应的加减乘除、比较移位运算</span></span></p> <p></p> <pre class="brush:php;toolbar:false">#include <iostream> #include <cstring> #include <string> using namespace std; // 大整数类型 #define MAXLEN 1000 struct HP {int len, s[MAXLEN];}; void PrintHP(HP x) { for (int i=x.len; i>=1; i--) cout 1 && !c.s[c.len]) c.len--; } // 大整数的比较 int HPCompare(const HP &x, const HP &y) { if (x.len > y.len) return 1; if (x.len 1 && (x.s[i]==y.s[i])) i--; return x.s[i] - y.s[i]; } // 大整数的乘法 void Multi(const HP a, const HP b, HP &c) { int i, j; // 对乘法结果赋初值,以方便之后的+=运算 c.len = a.len + b.len; for (i=1; i1 && !c.s[i]) i--; c.len = i; } // 大整数的除法 void Divide(const HP a, const HP b, HP &c, HP &d) { int i, j; // 用余数d存被除数a的前i位数据,用来多次减去除数b,以得到商c d.len = 1; d.s[1] = 0; for (i=a.len; i>0; i--) { if (!(d.len == 1 && d.s[1] == 0)) { // i没移一位,余数d也移位 for (j=d.len; j>0; j--) d.s[j+1] = d.s[j]; d.len++; } d.s[1] = a.s[i]; c.s[i] = 0; // 余数d大于除数b时,才可以进行减操作 while ((j=HPCompare(d,b)) >= 0) { Subtract(d, b, d); c.s[i]++; if (j == 0) break; } } c.len = a.len; while (c.len > 1 && c.s[c.len] == 0) c.len--; } // 十进位右移 void RightShift(HP &x, int k) { for (int i=1; i=1; i--) x.s[i+k] = x.s[i]; for (i=k; i>=1; i--) x.s[i] = 0; x.len += k; } // 求大整数的最大公约数 void GCD(HP a, HP b, HP &c) { if (b.len == 1 && b.s[1] == 0) { c.len = a.len; memcpy(c.s, a.s, (a.len+1)*sizeof(int)); } else { HP m, n; Divide(a, b, m, n); GCD(b, n, c); } } int main() { string str; string strc, stra, strb; cin >> str; int posc = str.find('.'); int posa = str.find('('); int posb = str.find(')'); strc = str.substr(0, posc); if (posc = 0) { strb = str.substr(posa+1, posb-posa-1); // 循环部分 Str2HP(strb.c_str(), b); HP m = tmp; LeftShift(m, strb.size()); Subtract(m, tmp, m); // 乘以10^(|b|-1) Multi(up, m, up); Plus(up, b, up); Multi(down, m, down); } // 求分子分母的最大公约数 GCD(down, up, tmp); HP h; Divide(down, tmp, down, h); Divide(up, tmp, up, h); PrintHP(up); cout <br> <br> <p><br> </p> </string></cstring></iostream>

Stored procedures are precompiled SQL statements in MySQL for improving performance and simplifying complex operations. 1. Improve performance: After the first compilation, subsequent calls do not need to be recompiled. 2. Improve security: Restrict data table access through permission control. 3. Simplify complex operations: combine multiple SQL statements to simplify application layer logic.

The working principle of MySQL query cache is to store the results of SELECT query, and when the same query is executed again, the cached results are directly returned. 1) Query cache improves database reading performance and finds cached results through hash values. 2) Simple configuration, set query_cache_type and query_cache_size in MySQL configuration file. 3) Use the SQL_NO_CACHE keyword to disable the cache of specific queries. 4) In high-frequency update environments, query cache may cause performance bottlenecks and needs to be optimized for use through monitoring and adjustment of parameters.

The reasons why MySQL is widely used in various projects include: 1. High performance and scalability, supporting multiple storage engines; 2. Easy to use and maintain, simple configuration and rich tools; 3. Rich ecosystem, attracting a large number of community and third-party tool support; 4. Cross-platform support, suitable for multiple operating systems.

The steps for upgrading MySQL database include: 1. Backup the database, 2. Stop the current MySQL service, 3. Install the new version of MySQL, 4. Start the new version of MySQL service, 5. Recover the database. Compatibility issues are required during the upgrade process, and advanced tools such as PerconaToolkit can be used for testing and optimization.

MySQL backup policies include logical backup, physical backup, incremental backup, replication-based backup, and cloud backup. 1. Logical backup uses mysqldump to export database structure and data, which is suitable for small databases and version migrations. 2. Physical backups are fast and comprehensive by copying data files, but require database consistency. 3. Incremental backup uses binary logging to record changes, which is suitable for large databases. 4. Replication-based backup reduces the impact on the production system by backing up from the server. 5. Cloud backups such as AmazonRDS provide automation solutions, but costs and control need to be considered. When selecting a policy, database size, downtime tolerance, recovery time, and recovery point goals should be considered.

MySQLclusteringenhancesdatabaserobustnessandscalabilitybydistributingdataacrossmultiplenodes.ItusestheNDBenginefordatareplicationandfaulttolerance,ensuringhighavailability.Setupinvolvesconfiguringmanagement,data,andSQLnodes,withcarefulmonitoringandpe

Optimizing database schema design in MySQL can improve performance through the following steps: 1. Index optimization: Create indexes on common query columns, balancing the overhead of query and inserting updates. 2. Table structure optimization: Reduce data redundancy through normalization or anti-normalization and improve access efficiency. 3. Data type selection: Use appropriate data types, such as INT instead of VARCHAR, to reduce storage space. 4. Partitioning and sub-table: For large data volumes, use partitioning and sub-table to disperse data to improve query and maintenance efficiency.

TooptimizeMySQLperformance,followthesesteps:1)Implementproperindexingtospeedupqueries,2)UseEXPLAINtoanalyzeandoptimizequeryperformance,3)Adjustserverconfigurationsettingslikeinnodb_buffer_pool_sizeandmax_connections,4)Usepartitioningforlargetablestoi


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.
