日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

【PAT】A1060 Are They Equal *

發(fā)布時(shí)間:2025/3/20 编程问答 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【PAT】A1060 Are They Equal * 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

If a machine can save only 3 significant digits, the float numbers 12300 and 12358.9 are considered equal since they are both saved as?0.123×10?5???with simple chopping. Now given the number of significant digits on a machine and two float numbers, you are supposed to tell if they are treated equal in that machine.

Input Specification:

Each input file contains one test case which gives three numbers?N,?A?and?B, where?N?(<100) is the number of significant digits, and?A?and?B?are the two float numbers to be compared. Each float number is non-negative, no greater than?10?100??, and that its total digit number is less than 100.

Output Specification:

For each test case, print in a line?YES?if the two numbers are treated equal, and then the number in the standard form?0.d[1]...d[N]*10^k?(d[1]>0 unless the number is 0); or?NO?if they are not treated equal, and then the two numbers in their standard form. All the terms must be separated by a space, with no extra space at the end of a line.

Note: Simple chopping is assumed without rounding.

Sample Input 1:

3 12300 12358.9

Sample Output 1:

YES 0.123*10^5

Sample Input 2:

3 120 128

Sample Output 2:

NO 0.120*10^3 0.128*10^3

?解題思路:

見(jiàn)代碼注釋,摘自《算法筆記》

代碼:

#include <stdio.h> #include <string> #include <iostream> using namespace std;int n; //有效位數(shù) string deal(string s,int& e){ //e被改寫(xiě)了,就是在內(nèi)存中的地址上的數(shù)字在本函數(shù)中不斷被改寫(xiě),所以在主函數(shù)中讀取的時(shí)候是本函數(shù)修改過(guò)的值,這也是int& e,修改內(nèi)存中e的數(shù)值的用意所在 int k=0; //s的下標(biāo)while(s.length()>0&&s[0]=='0'){ //前導(dǎo)數(shù)是0要去掉,如0000234.23這種情況,不加以判斷可能會(huì)在e指數(shù)的數(shù)值上出錯(cuò) s.erase(s.begin()); } if(s[0]=='.'){ //去完了前導(dǎo)數(shù)為0遇到了小數(shù)點(diǎn),就意味著該數(shù)小于1 s.erase(s.begin()); //去掉小數(shù)點(diǎn)while(s.length()>0&&s[0]=='0'){//再去前導(dǎo)數(shù)為0的數(shù),但是由于是在小數(shù)點(diǎn)之后,每去掉一個(gè)前導(dǎo)數(shù)是0,e也就是指數(shù)要減一 s.erase(s.begin());e--; } }//理解為小于1的深度搜索 else{ //如果去掉前導(dǎo)數(shù)后不是小數(shù)點(diǎn),意味著大于1 while(k<s.length()&&s[k]!='.'){ //尋找小數(shù)點(diǎn),在尋找過(guò)程中呢,記錄下小數(shù)點(diǎn)的位置和經(jīng)過(guò)的數(shù)位(有利于得到指數(shù)) k++;e++;} if(k<s.length()){ //為什么是k<s.length時(shí)有小數(shù)點(diǎn),因?yàn)槿绻?k==s.length時(shí),就是k到最后也沒(méi)找到小數(shù)點(diǎn) s.erase(s.begin()+k); //把小數(shù)點(diǎn)刪掉 ,目的是循環(huán)輸出的時(shí)候不用判斷小數(shù)點(diǎn)了 } //可以理解為大于1的深度搜索 }if(s.length()==0){//如果刪除前導(dǎo)零后,s的長(zhǎng)度是0,也就是這個(gè)數(shù)是0,綜上:這是在處理前導(dǎo)零遇到的三種情況 ,分別是去掉前導(dǎo)零后大于1,小于1和等于0,三種情況 e=0; } int num=0;k=0;string res;while(num<n){//n這里是全局變量 if(k<s.length()) res += s[k++]; //只要還有數(shù)字就加到末尾else res += '0';//前面的數(shù)不夠有效位數(shù)的,補(bǔ)零num++; } return res; }int main(){string s1,s2,s3,s4;cin>>n>>s1>>s2;int e1=0,e2=0;s3=deal(s1,e1);s4=deal(s2,e2);if(s3==s4&&e1==e2){ cout<<"YES 0."<<s3<<"*10^"<<e1<<endl;} else{ cout<<"NO 0."<<s3<<"*10^"<<e1<<" 0."<<s4<<"*10^"<<e2<<endl;} return 0; }

?

總結(jié)

以上是生活随笔為你收集整理的【PAT】A1060 Are They Equal *的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。