1016 Phone Bills (25分)(一个字短!)

1016 Phone Bills (25分)

分析

将所有记录按照名字以及记录时间排序,将两条相邻的前后同名且前为on后为off的记录提取为一对用于计算通话时长的有效记录,并保存在map中对应的用户的记录中,最后遍历map计算每一对记录的时长、费用,以及总费用即可
注意时长以及费用的计算可以从00:00:00开始计算然后相减

代码

#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<map>
using namespace std;
int d[24], n, momo, dd, hh, mm;
string id, con;
map<string, int> mon;
struct node {
 string iid;
 int tim, condi;
 node() {}
 node(string dd, int tt, int cc) :iid(dd), tim(tt), condi(cc) {}
};
vector<node> rcd;
bool cmp(node a, node b) {
 return (a.iid != b.iid) ? (a.iid < b.iid) : (a.tim < b.tim);
}
map<string, vector<pair<int, int> > >bill;
int getmoney(int t) {
 int s = 0, i = 0;
 while (t != 0) {
  if (t >= 60) {
   t -= 60;
   s += 60 * d[i % 24];
  }
  else{
   s += t * d[i % 24];
   t = 0;
  }
  ++i;
 }
 return s;
}
//另一种计算费用的方法
// int getmoney1(int t) {
//  int s = 0, dd = t / (24 * 60), hh = t / 60 % 24, mm = t % 60, ss = 0;
//  s += d[hh] * mm;
//  for (int i = 0; i < 24; ++i)ss += d[i];
//  s += ss * dd * 60;
//  for (int i = 0; i < hh; ++i)
//   s += d[i] * 60;
//  return s;
// }
int main() {
 for (int i = 0; i < 24; ++i)scanf("%d", &d[i]);
 scanf("%d", &n);
 while (--n >= 0) {
  cin >> id;
  scanf("%d:%d:%d:%d", &momo, &dd, &hh, &mm);
  mon[id] = momo;
  cin >> con;
  rcd.push_back(node(id, mm+hh*60+dd*24*60, con[1] == 'n'));
 }
 sort(rcd.begin(), rcd.end(), cmp);
 for (int i = 0; i < rcd.size() - 1; ++i)
  if (rcd[i].iid == rcd[i + 1].iid && rcd[i].condi == 1 && rcd[i + 1].condi == 0)
   bill[rcd[i].iid].push_back(pair<int, int>(rcd[i].tim, rcd[i + 1].tim));
 for (auto it : bill) {
  printf("%s %02d\n", it.first.c_str(), mon[it.first]);
  double s = 0;
  for (auto it1 : it.second) {
   int a = getmoney(it1.first), b = getmoney(it1.second);
                        //int a = getmoney1(it1.first), b = getmoney1(it1.second);
   double ss = (double)(b - a) / 100.0;
   printf("%02d:%02d:%02d %02d:%02d:%02d %d $%.2f\n", it1.first / (24 * 60), it1.first / 60 % 24, it1.first % 60, it1.second / (24 * 60), it1.second / 60 % 24, it1.second % 60, it1.second - it1.first, ss);
   s += ss;
  }
  printf("Total amount: $%.2f\n", s);
 }
 return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的记账Java代码,主要实现了以下功能:添加账单、删除账单、展示账单、按照时间和金额排序账单。 ```java import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Scanner; public class AccountBook { private ArrayList<Bill> bills; // 账单列表 public AccountBook() { bills = new ArrayList<>(); } // 添加账单 public void addBill(Bill bill) { bills.add(bill); } // 删除账单 public void deleteBill(int index) { if (index >= 0 && index < bills.size()) { bills.remove(index); } } // 展示账单 public void showBills() { if (bills.isEmpty()) { System.out.println("没有任何账单!"); return; } System.out.println("序号\t\t时间\t\t金额\t\t备注"); for (int i = 0; i < bills.size(); i++) { System.out.println(i + "\t\t" + bills.get(i)); } } // 按照时间排序账单 public void sortByTime() { Collections.sort(bills, new Comparator<Bill>() { @Override public int compare(Bill o1, Bill o2) { return o1.getTime().compareTo(o2.getTime()); } }); } // 按照金额排序账单 public void sortByAmount() { Collections.sort(bills, new Comparator<Bill>() { @Override public int compare(Bill o1, Bill o2) { return (int) (o1.getAmount() - o2.getAmount()); } }); } public static void main(String[] args) { AccountBook accountBook = new AccountBook(); Scanner scanner = new Scanner(System.in); while (true) { System.out.println("请选择操作:"); System.out.println("1. 添加账单"); System.out.println("2. 删除账单"); System.out.println("3. 展示账单"); System.out.println("4. 按时间排序账单"); System.out.println("5. 按金额排序账单"); System.out.println("0. 退出程序"); int choice = scanner.nextInt(); switch (choice) { case 1: System.out.print("输入时间(格式:yyyy-MM-dd):"); String time = scanner.next(); System.out.print("输入金额:"); double amount = scanner.nextDouble(); System.out.print("输入备注:"); String remark = scanner.next(); Bill bill = new Bill(time, amount, remark); accountBook.addBill(bill); System.out.println("添加账单成功!"); break; case 2: System.out.print("输入要删除的账单序号:"); int index = scanner.nextInt(); accountBook.deleteBill(index); System.out.println("删除账单成功!"); break; case 3: accountBook.showBills(); break; case 4: accountBook.sortByTime(); System.out.println("按时间排序成功!"); break; case 5: accountBook.sortByAmount(); System.out.println("按金额排序成功!"); break; case 0: System.out.println("退出程序。"); System.exit(0); break; default: System.out.println("输入错误,请重新输入!"); break; } } } } class Bill { private String time; // 时间 private double amount; // 金额 private String remark; // 备注 public Bill(String time, double amount, String remark) { this.time = time; this.amount = amount; this.remark = remark; } public String getTime() { return time; } public void setTime(String time) { this.time = time; } public double getAmount() { return amount; } public void setAmount(double amount) { this.amount = amount; } public String getRemark() { return remark; } public void setRemark(String remark) { this.remark = remark; } @Override public String toString() { return time + "\t\t" + amount + "\t\t" + remark; } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值