c++ 如何求几个时间平均值

Lee_2019 2018-01-10 07:24:30
例如: 15:21:38
15:30:25
14:58:39
时:分:秒格式的时间
如何就它们的中间值(平均值),方法有哪些?谢谢
...全文
779 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
百灵工作室 2018-01-12
  • 打赏
  • 举报
回复
引用 9 楼 qq_33302340 的回复:
[quote=引用 5 楼 zhao4zhong1 的回复:] [quote=引用 4 楼 lk_19930809 的回复:] 看了3个帖子,都有赵四老师贴的代码
看了从2007年赵四老师注册CSDN到现在的xxxxxxxxxxxx个帖子,几乎都有他贴的代码[/quote] 无敌是多么的寂寞 [/quote] 无敌是多么的寂寞
啊哈moment 2018-01-12
  • 打赏
  • 举报
回复
引用 5 楼 zhao4zhong1 的回复:
[quote=引用 4 楼 lk_19930809 的回复:] 看了3个帖子,都有赵四老师贴的代码
看了从2007年赵四老师注册CSDN到现在的xxxxxxxxxxxx个帖子,几乎都有他贴的代码[/quote] 无敌是多么的寂寞
x_xx_xxx_xxxx 2018-01-12
  • 打赏
  • 举报
回复
先求秒的平均值、再求分的平均值、最后求小时的平均值 。 然后再组合到一起。这样应该没问题。
赵4老师 2018-01-12
  • 打赏
  • 举报
回复
赵4老师 2018-01-11
  • 打赏
  • 举报
回复
引用 4 楼 lk_19930809 的回复:
看了3个帖子,都有赵四老师贴的代码
看了从2007年赵四老师注册CSDN到现在的xxxxxxxxxxxx个帖子,几乎都有他贴的代码
宁静致远93 2018-01-11
  • 打赏
  • 举报
回复
看了3个帖子,都有赵四老师贴的代码
要养萨摩耶 2018-01-11
  • 打赏
  • 举报
回复
//用户给定几个时间,时分秒格式,例如15时35分25秒 //求几个时间的平均值 #include<iostream> #include<windows.h> using namespace std; int main() { cout << "输入的时间的个数:" << endl; int num, k = 1, all, all_time = 0,average_time,average_time_hour,average_time_minnute,average_time_second; cin >> num; int *time; time = new int[num * 3]; cout << "你输入的时间依次是:" << endl; for (int i = 0; i < num*3; i++) { cin >> time[i]; } for (int i = 0; i < num*3-2; i = i + 3) { cout << "第" << k << "个时间为 " << time[i] << "时" << time[i + 1] << "分" << time[i + 2] << "秒" << endl; k = k + 1; } for (int i = 0; i < num *3- 2; i = i + 3) { all = 3600 * time[i] + 60 * time[i + 1] + time[i + 2]; all_time = all_time + all; } average_time = all_time / num; average_time_hour = average_time / 3600; average_time_minnute = (average_time - 3600 * average_time_hour) / 60; average_time_second = average_time - 3600 * average_time_hour - 60 * average_time_minnute; cout << "你所输入时间的平均时间是" << average_time_hour << "时" << average_time_minnute << "分" << average_time_second << "秒" << endl; system("pause"); }
赵4老师 2018-01-11
  • 打赏
  • 举报
回复
仅供参考:
#include <afxdisp.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, TCHAR* argv[]) {
    COleDateTime t;
    COleDateTimeSpan ts;
    CString s,fmt;
    int nYear;
    int nMonth;
    int nDay;
    int nHour;
    int nMin;
    int nSec;
    int lDays;
    int nHours;
    int nMins;
    int nSecs;
    int i,N;
    // initialize MFC and print and error on failure
    if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0)) {
        printf("Fatal Error: MFC initialization failed\n");
        return 1;
    }
    if (argc<13) {
        printf("Usage:%s sYYYY sMM sDD shh smm sss pDD phh pmm pss n {SQL|YYYY|YY}\n",argv[0]);
        return 2;
    }
    if (stricmp(argv[12],"SQL")==0) fmt="%Y-%m-%d %H:%M:%S";
    else if (stricmp(argv[12],"YYYY")==0) fmt="%Y%m%d %H%M%S";
    else if (stricmp(argv[12],"YY")==0) fmt="%y%m%d %H%M%S";
    else {
        printf("Usage:%s sYYYY sMM sDD shh smm sss pDD phh pmm pss n {SQL|YYYY|YY}\n",argv[0]);
        return 3;
    }
    nYear =atoi(argv[ 1]);
    nMonth=atoi(argv[ 2]);
    nDay  =atoi(argv[ 3]);
    nHour =atoi(argv[ 4]);
    nMin  =atoi(argv[ 5]);
    nSec  =atoi(argv[ 6]);
    lDays =atoi(argv[ 7]);
    nHours=atoi(argv[ 8]);
    nMins =atoi(argv[ 9]);
    nSecs =atoi(argv[10]);
    N     =atoi(argv[11]);
    if (N<=0) {
        printf("Usage:%s sYYYY sMM sDD shh smm sss pDD phh pmm pss n {SQL|YYYY|YY}\n",argv[0]);
        return 4;
    }
    t=COleDateTime( nYear, nMonth, nDay, nHour, nMin, nSec);
    ts=COleDateTimeSpan( lDays, nHours, nMins, nSecs );
    for (i=1;i<=N;i++) {
        s=t.Format(fmt);
        printf("%08d %s\n",i,s);
        t=t+ts;
    }
    return 0;
}
AlbertS 2018-01-11
  • 打赏
  • 举报
回复
先分析字符串,从每一条记录中取出时、分、秒,然后转化成秒,每条记录的秒数相加,然后除以记录条数,得到的秒数,再转化为时分秒格式的时间就好了
paschen 版主 2018-01-10
  • 打赏
  • 举报
回复
每个先转换成整数,如:15*60*60+21*60+38,之后对所有转换的整数求平均值,再将结果转换为时间的格式(如小时就是除3600的整数部位,分就是减去小时之后的值除60的整数部分,秒是最后剩下的)
狐狸小十 2018-01-10
  • 打赏
  • 举报
回复
你这格式如果指的字串的话 就先用strtok把数字拆分出来 再atoi转成int 求平均

64,282

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧