PAT 1016. Phone Bills (25)

//1016. Phone Bills (25)
//accept
#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;

typedef struct 
{
	char name[21];
	int month;
	int day;
	int hour;
	int minute;
	bool state;//state = true: on-line         false: off-line
}Record;

bool cmp(Record r1, Record r2)
{
	if (strcmp(r1.name, r2.name) == 0)
	{
		if (r1.month == r2.month)
		{
			if (r1.day == r2.day)
			{
				if (r1.hour == r2.hour)
				{
					return r1.minute < r2.minute;
				}
				return r1.hour < r2.hour;
			}
			return r1.day < r2.day;
		}
		return r1.month < r2.month;
	}

	return (strcmp(r1.name, r2.name) < 0);
}

int main()
{
	Record r[1005];
	int toll[24];
	int tolladd = 0;
	int i;
	for (i = 0;i<24;i++)
	{
		scanf("%d", &toll[i]);
		tolladd += toll[i];
	}
	
	int n;
	scanf("%d", &n);
	char tmp[8];
	for (i = 0;i<n;i++)
	{
		scanf("%s %d:%d:%d:%d", &r[i].name, &r[i].month, &r[i].day, &r[i].hour, &r[i].minute);
		scanf("%s", &tmp);
		if (strncmp(tmp, "on-line", 7) == 0)
		{
			r[i].state = true;
		}
		else
			r[i].state = false;
	}

	sort(r, r+n, cmp);


	Record start = r[0];
	int j;

	int minute;
	float money;
	float total = 0;


	bool flag = false;
	for (i = 1;i<n;i++)
	{
		if (strcmp(r[i].name, start.name) == 0)
		{
			if (start.state == true)
			{
				if (r[i].state == false && r[i].month == start.month && r[i].day >= start.day)
				{
					//cal

					if (flag == false)
					{	
						printf("%s %02d\n",r[i].name, r[i].month);
						flag = true;
					}

					printf("%02d:%02d:%02d %02d:%02d:%02d", start.day, start.hour, start.minute, r[i].day, r[i].hour, r[i].minute);

					minute = r[i].day * 24 * 60 + r[i].hour * 60 + r[i].minute - start.day * 24 * 60 -start.hour* 60 - start.minute;
					cout << " " <<minute;

					money = 0;

					if (r[i].day == start.day)
					{
						for (j = start.hour; j<r[i].hour;j++)
						{
							money += toll[j] * 60;
						}
						money -= toll[start.hour]*start.minute;
						money += toll[r[i].hour]*r[i].minute;
					}
					else
					{
						for (j = start.day;j<r[i].day;j++)
						{
							money += tolladd * 60;
						}

						for (j = 0;j<start.hour;j++)
						{
							money -= (toll[j]*60);
						}
						money -= toll[start.hour]*start.minute;

						for (j = 0;j<r[i].hour; j++)
						{
							money += (toll[j]*60);
						}
						money += (toll[r[i].hour] * r[i].minute);
					}

					money = money / 100.0;
					total+=money;
					
					printf(" $%.2f\n", money);

					if (strcmp(start.name, r[i+1].name) != 0 && total > 0)
					{
						printf("Total amount: $%.2f\n", total);
						flag = false;
						total = 0;
					}

					start = r[i+1];
					i++;
				}
				else
				{

					if (strcmp(start.name, r[i].name) != 0 && total > 0)
					{
						printf("Total amount: $%.2f\n", total);
						flag = false;
						total = 0;
					}
					start = r[i];
					continue;
				}
			}
			else
			{
				if (strcmp(start.name, r[i].name) != 0 && total > 0)
				{
					flag = false;
					printf("Total amount: $%.2f\n", total);
					total = 0;
				}
				start = r[i];
			}
		}
		else
		{
			if (total > 0)
			{
				flag = false;
				printf("Total amount: $%.2f\n", total);
				total = 0;
			}
			start = r[i];
		}
	}

	if (total > 0)
	{
		printf("Total amount: $%.2f\n", total);

	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用以下的Java代码实现所描述的算法: ```java import com.google.common.collect.Lists; import java.util.List; public class BillLockAlgorithm { private static final int[] BATCH_SIZES = {100, 50, 10, 1}; public void lockBills(List<Bill> billList, int syncSettlementLockBillSize) { List<List<Bill>> batchBillList = Lists.partition(billList, syncSettlementLockBillSize); for (List<Bill> bills : batchBillList) { try { billTagApplyService.lockOrUnlockBill(bills, req.getProjectId(), true, LockTypeEnum.LOCK_TYPE_ENUM_16.getCode(), propertyUser, true); } catch (Exception e) { // 锁定失败尝试分批补推 retryLockingBills(bills); } } } private void retryLockingBills(List<Bill> bills) { for (int batchSize : BATCH_SIZES) { List<List<Bill>> batchBillList = Lists.partition(bills, batchSize); for (List<Bill> batch : batchBillList) { try { billTagApplyService.lockOrUnlockBill(batch, req.getProjectId(), true, LockTypeEnum.LOCK_TYPE_ENUM_16.getCode(), propertyUser, true); } catch (Exception e) { // 锁定失败记录失败的账单 recordFailedBills(batch); } } } } private void recordFailedBills(List<Bill> bills) { // 记录失败的账单逻辑 } } ``` 你可以在 `lockBills` 方法中调用 `billTagApplyService.lockOrUnlockBill()` 方法来锁定账单。如果锁定失败,会调用 `retryLockingBills` 方法进行分批补推。在 `retryLockingBills` 方法中,会尝试使用不同的批量大小来再次锁定账单,如果仍然失败,则记录失败的账单。 请注意,以上代码是基于你提供的信息进行假设的,确保在使用之前根据你的实际情况进行适当的调整和错误处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值