注意点:
1
整型数字加减乘除还是整型,所以记得乘以1.00
2 四舍五入的函数为round(在math.h里面)
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
int c1,c2;
int h,m,s;
cin>>c1>>c2;
float c3;
c3=(c2-c1*1.00)/100; //c3=(c2-c1)/100; 错误,这样会导致c3为整数
h=c3/3600;
m=((c3-h*3600)/60);
s=round(c3-h*3600-m*60);
if(s==60){
m++;
s=0;
}
if(m==60){
h++;
m=0;
}
printf("%02d:%02d:%02d",h,m,s);
return 0;
}
方法2
稍微动点脑筋,取巧一点,可以大大简化步骤
#include <iostream>
using namespace std;
int main() {
int a, b;
cin >> a >> b;
int n = ((b - a) + 50) / 100;
int hour = n / 3600;
n = n % 3600;
int minute = n / 60, second = n % 60;
printf("%02d:%02d:%02d", hour, minute, second);
return 0;
}
最后
以上就是哭泣跳跳糖最近收集整理的关于C++ 1026 程序运行时间(15 分)的全部内容,更多相关C++内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复