map的使用,i.first,i.second
int a[23],声明0-22的数组,作为局部变量初始化a[23] ={0},a[24] 未知,全局变量都初始化为0;
格式化输出 %02d 按两位数输出
读入数据形式 aa:bb:cc:dd, scanf("%d:%d:%d:%d");
统计费用差,将所有时间转换成同一单位分钟,便于大小的判断
#include<iostream> #include<vector> #include<string> #include<algorithm> #include<map> using namespace std; int n; struct Node{ string name; int time,state,month,day,hour,minute; }; bool cmp(Node &a,Node &b){ return a.name!=b.name?a.name<b.name:a.time<b.time; } double cal(Node a,int *rate){ double ttt =0.0; ttt = a.day*rate[24]*60+rate[a.hour]*a.minute; for(int i=0;i<a.hour;i++){ ttt+=rate[i]*60; } ttt/=100.0; return ttt; } int main() { int rate[25]={0}; for(int i=0;i<24;i++){ cin>>rate[i]; rate[24] += rate[i]; } cin>>n; vector<Node>record(n+1); for(int i=1;i<=n;i++){ string na; cin>>na; record[i].name = na; scanf("%d:%d:%d:%d",&record[i].month,&record[i].day,&record[i].hour,&record[i].minute); string state; cin>>state; record[i].state =(state == "on-line")?1:0; record[i].time = record[i].day*24*60+record[i].hour*60+record[i].minute; } sort(record.begin(),record.end(),cmp); map<string,vector<Node> >ans; for(int i=1;i<=n;i++){ if(record[i].name == record[i-1].name &&record[i-1].state ==1&&record[i].state == 0) { ans[record[i-1].name].push_back(record[i-1]); ans[record[i].name].push_back(record[i]); } } for(auto i:ans){ vector<Node> it = i.second; double tot = 0.0; printf("%s %02d ",i.first.c_str(),it[0].month); for(int j=1;j<it.size();j+=2){ //printf(" cssss:%02d:%03d ",it[j].); double t =cal(it[j],rate)-cal(it[j-1],rate); printf("%02d:%02d:%02d %02d:%02d:%02d %d $%.2f ",it[j-1].day,it[j-1].hour,it[j-1].minute,it[j].day,it[j].hour,it[j].minute,it[j].time-it[j-1].time,t); tot+=t; } printf("Total amount: $%.2f ",tot); } return 0; }