23.11.15

题目:

问题描述:
小明很喜欢在一家超市消费,跟老板成了好朋友,所以每次付款时老板都会把小明本次消费的小数点部分抹掉。
如果小明本次消费金额大于10元,那么老板还会额外把金额的个位数也抹掉。
输入:
第一行为整数T,表示小明消费的次数
接下来T行,每行包含一个浮点数money,表示小明本次消费的金额。(0<money<=1e18,1<=T<=1000)
输出:
输出T行,每行包含一个整数表示小明本次消费的实付金额,然后换行。
样例输入:
3
35.7
6.2
20
样例输出:
30
6
20

解题思路:

利用strtoll函数将字符串转换为long long 类型。

注:

因为金额范围0<money<=1e18,且利用for循环输入long long 类型时,若输入了小数,会提前跳出循环,无法完成输入。

笔记:

stoi,stol,stoll ,stof,stod分别转int,long,long long,float,double。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;
int main(){
string s = "123.333";
int a = stoi(s);
long b = stol(s);
long long c = stoll(s);
float d = stof(s);
double e = stod(s);
cout<<a<<endl;
cout<<b<<endl;
cout<<c<<endl;
cout<<d<<endl;
cout<<e<<endl;
return 0;
}

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;
int T;
int main(){
cin>>T;
long long a[T];
for(int i =0;i<T;i++){
string n;
cin>>n;
a[i]=strtoll(n.c_str(),NULL,10);
}
for(int i = 0;i<T;i++){
if(a[i]>10){
a[i]=a[i]/10*10;
}
cout<<a[i]<<endl;
}
return 0;
}

运行结果:

image