1016. Phone Bills (25)

本文介绍了一个基于时间的电话账单事件模拟程序,该程序通过记录电话的接通和挂断时间来计算通话费用及持续时间。利用C++实现,文章详细展示了如何处理电话账单数据、过滤无效记录并计算费用。

参考自:

考察以时间为核心的电话账单事件模拟以及统计

#include<iostream>
using namespace std;
#include<vector>
#include<string>
#include<algorithm>




struct Call
{
	string name;
	int month;
	int date;
	int hour;
	int minute;
	int total;
	string status;
};

int charge[24];
vector<Call> all_calls;
vector<Call> format_calls;

bool compare(Call a,Call b)
{
	if(a.name < b.name)
		return 1;
	else if(a.name == b.name && a.total < b.total)
		return 1;
	else
		return 0;
}

void show(Call c)
{
	cout<<c.name<<" ";
	cout<<c.month<<":"<<c.date<<":"<<c.hour<<":"<<c.minute<<" ";
	cout<<c.status<<endl;
}

//calculate money from the begin of month to the time given
int chargeByTime(int time)
{
	int hours = time/60;
	int minutes = time%60;
	int money = 0;
	int i;
	for(i = 0;i<hours;i++)
		money += charge[i%24]*60;
	money += charge[i%24]*minutes;
	return money;
}

double calCost(Call s,Call t)
{
	return (double)(chargeByTime(t.total)-chargeByTime(s.total))/100;
}

int calLast(Call s,Call t)
{
	return (t.date-s.date)*24*60+(t.hour-s.hour)*60+(t.minute-s.minute);
}



int main()
{
	for(int i = 0;i<24;i++)
		cin>>charge[i];
	int N;
	cin>>N;
	while(N--)
	{
		Call c;
		cin>>c.name;
		cin>>c.month;
		getchar();
		cin>>c.date;
		getchar();
		cin>>c.hour;
		getchar();
		cin>>c.minute;
		c.total = c.minute + 60*c.hour + 24*60*c.date;
		cin>>c.status;
		all_calls.push_back(c);
	}
	sort(all_calls.begin(),all_calls.end(),compare);

	//filter delete those bad record
	bool haveonline = false;
	string curname;
	for(int i=0;i<all_calls.size();i++)
	{
		if(haveonline == false && all_calls[i].status =="on-line" )
		{
			format_calls.push_back(all_calls[i]);
			haveonline = true;
			curname = all_calls[i].name;
		}
		else if(haveonline == true && all_calls[i].status =="on-line")
		{
			format_calls.pop_back();
			format_calls.push_back(all_calls[i]);
			haveonline = true;
			curname = all_calls[i].name;
		}
		else if(haveonline == true && all_calls[i].status =="off-line"&&all_calls[i].name ==curname)
		{
			format_calls.push_back(all_calls[i]);
			haveonline = false;
		}
	}
	//the last must be offline
	if((*(format_calls.end()-1)).status == "on-line")
		format_calls.pop_back();

	//output
	double totalcost = 0;
	curname = "";
	for(int i=0;i<format_calls.size();i+=2)
	{

		if(format_calls[i].name != curname)
		{
			if(curname!="")
			{
				printf("Total amount: $%.2f\n",totalcost);
				totalcost = 0;
				printf("%s %02d\n",format_calls[i].name.c_str(),format_calls[i].month);
			}
			else
			{
				printf("%s %02d\n",format_calls[i].name.c_str(),format_calls[i].month);
			}
			curname = format_calls[i].name;
		}
		printf("%02d:%02d:%02d",format_calls[i].date,format_calls[i].hour,format_calls[i].minute);
		printf(" ");
		printf("%02d:%02d:%02d",format_calls[i+1].date,format_calls[i+1].hour,format_calls[i+1].minute);
		printf(" ");
		printf("%d",calLast(format_calls[i],format_calls[i+1]));
		printf(" ");
		printf("$%.2f\n",calCost(format_calls[i],format_calls[i+1]));
		totalcost+=calCost(format_calls[i],format_calls[i+1]);
	}
	printf("Total amount: $%.2f\n",totalcost);
	
	
}


 

可以,`std::ios::sync_with_stdio(false);` 是完全合法且常用的 C++ 代码,用于**解除 C++ 标准(如 `cin`, `cout`)与 C 标准 I/O(如 `stdin`, `stdout`)之间的同步**。 --- ### ✅ 回答问题:`std::ios::sync_with_stdio(false)` 行吗? **答案是:行,而且在某些场景下推荐使用。** #### 作用: 调用 `std::ios_base::sync_with_stdio(false);` 会**关闭 C++ iostream 与 C stdio 之间的同步机制**。默认情况下,C++ 的 `cin/cout` 与 C 的 `scanf/printf` 是同步的,这意味着你可以安全地混用它们,例如: ```cpp printf("Enter a number: "); std::cin >> x; std::cout << "Got: " << x << std::endl; scanf("%d", &y); ``` 但由于这种同步带来了性能开销(每次操作都需要协调缓冲区),所以在需要高速输入输出的场合(如算法竞赛、大量数据读写),通常会关闭同步以提升性能。 --- ### 示例代码(C++): ```cpp #include <iostream> int main() { // 关闭 C 和 C++ 之间的同步 std::ios_base::sync_with_stdio(false); int n; std::cin >> n; std::cout << "You entered: " << n << '\n'; return 0; } ``` > ⚠️ 注意事项: 1. **必须在任何 I/O 操作之前调用!** - 一旦开始使用 `cin`、`cout` 或其他标准,再调用此函数的行为是**未定义的**。 2. **禁止混用 C 和 C++ 的输入输出函数**: - 如果你用了 `sync_with_stdio(false)`,就不要再同时使用 `scanf/printf` 和 `cin/cout`,否则可能导致输出顺序错乱或程序崩溃。 3. **性能提升显著**: - 尤其是在读取大量数据时(比如百万级整数),速度可提升数倍。 --- ### 常见搭配优化: ```cpp std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); // 解除 cin 和 cout 的绑定,避免每次 cin 都刷新 cout ``` 这两句经常一起出现于竞赛代码中,极大提高输入效率。 --- ### 解释总结: | 设置 | 说明 | |------|------| | `sync_with_stdio(false)` | 提高性能,但不能再混用 `cin/cout` 与 `scanf/printf` | | `cin.tie(nullptr)` | 解除 `cin` 对 `cout` 的绑定,防止每次输入前强制刷新输出 | 如果不关闭同步,`cin` 和 `cout` 会为了兼容 C 的 `stdio` 而频繁加锁、刷新,导致速度变慢。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI记忆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值