去掉前导零
作者:
lvjj
,
2024-04-02 18:36:49
,
所有人可见
,
阅读 7
法一:比较麻烦。。。
#include <bits/stdc++.h>
using namespace std;
//用istringstream方法读入带有前导零的数,并对前导零筛除的效果
int main() {
string timeStr = "01:17";
istringstream iss(timeStr); //创建istringstream对象,名字叫iss,把准备读入的timestr放进去
int hours, minutes;
char colon;
int m;
iss >> hours >> colon >> minutes; //从iss开始读取,>>符号的作用是读取连续数字类的字符,遇到非数字字符就会停止读取(同时把数字类字符变成整型,忽略前导零)读取位置会往后移动
m = hours*60 + minutes;
cout << "Hours:" << hours << ", Minutes:" << minutes << endl;
return 0;
}
//运行结果:
//Hours: 1, Minutes: 1
//
//--------------------------------
//Process exited after 0.6152 seconds with return value 0
//请按任意键继续. . .
法二:scanf可自动去掉前导零
#include <bits/stdc++.h>
using namespace std;
int main() {
int hours, minutes;
scanf("%d:%d",&hours, &minutes);
printf("hours:%d, minutes:%d",hours, minutes);
return 0;
}
//01:09
//hours:1, minutes:9
//--------------------------------
//Process exited after 6.024 seconds with return value 0
//请按任意键继续. . .
其实用 scanf 格式化输入我感觉会方便点
确实hh