Problem - 105A - Codeforces A.(精度问题)
链接:http://codeforces.com/problemset/problem/105/A
?
解题思路:题目要求输出单词必须按字典序输出,用STL中的map容器会比较简单
map容器实质上是一个二叉查找树,它可以做插入、查找、查询等操作。时间复杂度log(n);
n为map中元素的个数,再用迭代器去访问map中的元素就是按照字典序进行访问的
?
map<x1,x2>it1;
x1为键,x2为值,键是用来索引的,值就是其存储的信息
?
此题应注意的是精度问题
例如:
1 1 0.3
aaa 1000
b
输出为:
aaa 299
b 0
说明double存储数据的时候可能不是300.000....而是299.999999...
故类型转换的时候先加0.0001(0.001到0.000001都行)
?
#include<cstdio>#include<cstring>#include<iostream>#include<map>#include<string>using namespace std;map<string,int>::iterator it;map<string,int>val;int n,m;int exp;double k;char str[100];int main(){while(scanf("%d %d %lf", &n, &m, &k) != EOF){int cnt = 0;for(int i = 0; i < n; i++){scanf("%s",str);getchar();scanf("%d",&exp);//printf("k = %lf,exp = %d\n",k,exp);//printf("%lf\n",k*exp);exp = int(exp *1.0 * k + 0.0001);//printf("exp = %d\n", exp);if( exp < 100 ) continue; val[str] = exp;cnt++;}for(int i = 0; i < m; i++){scanf("%s",str);if(val[str] != 0) continue ;else {val[str] = 0;cnt++;}}printf("%d\n",cnt);for( it = val.begin(); it != val.end(); it++ ){cout<< it -> first <<" "<< it -> second << endl ;//printf("%s %d",it->first,it->second);}val.clear();//注释1}return 0;}
?注释1:竟然是有没有都可以,但是很明显的
5 4 0.75
a 300
b 250
c 200
d 150?
e 100
e
d
c
b
4 4 0.75
e 300
d 250
c 200
b 150
b
c
d
e
输出就会有问题,当初还以为这错了!改了好久
?