您的位置:首页 > 编程语言 > C语言/C++

【C】C++标准模板库(STL)介绍--string

2018-02-28 15:26 633 查看
[b]3.string[/b]注意cin>>与getline()的使用.c_str()---使用printf(“%s”,str.c_str());.insert(pos,string).insert(it,it2,it3).erase(it).erase(first,last)---起止迭代器.erase(pos,length).clear().substr(pos,len).find(str2).find(str2,pos)---从pos号开始匹配str2,返回第一次出现的位置.replace(pos,len,str2).replace(it1,it2,str2)

1060. Are They Equal (25)

时间限制 100 ms
内存限制 65536 kB
代码长度限制 16000 B
判题程序 Standard 作者 CHEN, Yue
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*105 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 10100, 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.d1...dN*10^k" (d1>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
/*用例:4 0000 0000.00
4 00123.5678 0001235
3 0.0520 0.0521
12 123456789012345 123456789012300
2 0.00000000000000012 0.00000000000000012354
*/
#include<string>
#include<iostream>
#include<stdio.h>
using namespace std;
string str1,str2;
string deal(string str,int n,int &zhi){
int i;
string strr;
string str0="0";
//不论是小数或是整数,先去掉前导0
while(str.length()>0&&str[0]=='0') str.erase(str.begin());
//如果将前导0都去掉后是小数点,表明是小数
//需要将小数点去除后继续消掉小数部分的前导0
if(str[0]=='.'){
str.erase(str.begin());
int len1=str.length();//记录消去小数点后小数长度
while(str.length()>0&&str[0]=='0') str.erase(str.begin());
int len2=str.length();//记录消去小数前导0后小数长度
if(len2==0) zhi=0;//这个数就是0,指数设为0,这是个陷阱!!!
else zhi=len2-len1;//小数的指数求法与整数不同
while(str.length()<n){//剩余部分长度不及n,需要在末尾补0
str.insert(str.end(),str0.begin(),str0.end());
}
//str.insert的用法:(1)str.insert(3,str2) (2)str.insert(str.begin()+3,str2.begin(),str2.end())
strr="0."+str.substr(0,n);//处理后的数值部分
}
//如果前导0去掉是整数
else if(str.length()!=0){
if(str.find(".")!=string::npos){//找到了小数点,说明指数应该是整数部分长度
zhi=str.find(".");//由于字符串以0号位置开始,所以小数点所在位置就是整数部分长度
str.erase(str.begin()+zhi);
}
else zhi=str.length();
//str.erase(str.begin()+zhi);//删除小数点***

//如果长度不及n,在末尾补0
while(str.length()<n) str.insert(str.end(),str0.begin(),str0.end());
strr="0."+str.substr(0,n);
}
//全0
else{
while(str.length()<n) str.insert(str.begin(),str0.begin(),str0.end());
strr="0."+str;
}
return strr;
}
int main(){
int n,zhi1=0,zhi2=0;
cin>>n>>str1>>str2;
string strr1=deal(str1,n,zhi1);
string strr2=deal(str2,n,zhi2);
if(strr1==strr2&&zhi1==zhi2){
cout<<"YES "<<strr1<<"*10^"<<zhi1<<endl;
}
else{
cout<<"NO "<<strr1<<"*10^"<<zhi1<<" "<<strr2<<"*10^"<<zhi2<<endl;
}
return 0;
}

/*//scanf("%d %s %s",&n,str1,str2);
cin>>n>>str1>>str2;
//使用string进行输入只能用cin方法
string str11=str1.substr(0,n);
string str22=str2.substr(0,n);
//substr只有一种用法:起始位置+长度,而且参数都是数字,而不是str.begin()之类的迭代器
if(str11==str22){
//printf("YES 0.%s*10^%d",str11.c_str(),len1);
cout<<"YES 0."<<str11<<"*10^"<<len1<<endl;
}
else{
//printf("NO 0.%s*10^%d 0.%s*10^%d",str11.c_str(),len1,str22.c_str(),len2);
cout<<"NO 0."<<str11<<"*10^"<<len1<<" 0."<<str22<<"*10^"<<len2<<endl;
}
//使用.c_str()方法不要忘记括号,或者使用cout<<*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: