H - Tickets

Jesus, what a great movie! Thousands of people are rushing to the cinema. However, this is really a tuff time for Joe who sells the film tickets. He is wandering when could he go back home as early as possible. 
A good approach, reducing the total time of tickets selling, is let adjacent people buy tickets together. As the restriction of the Ticket Seller Machine, Joe can sell a single ticket or two adjacent tickets at a time. 
Since you are the great JESUS, you know exactly how much time needed for every person to buy a single ticket or two tickets for him/her. Could you so kind to tell poor Joe at what time could he go back home as early as possible? If so, I guess Joe would full of appreciation for your help. 

Input

There are N(1<=N<=10) different scenarios, each scenario consists of 3 lines: 
1) An integer K(1<=K<=2000) representing the total number of people; 
2) K integer numbers(0s<=Si<=25s) representing the time consumed to buy a ticket for each person; 
3) (K-1) integer numbers(0s<=Di<=50s) representing the time needed for two adjacent people to buy two tickets together. 

Output

For every scenario, please tell Joe at what time could he go back home as early as possible. Every day Joe started his work at 08:00:00 am. The format of time is HH:MM:SS am|pm. 

Sample Input

2
2
20 25
40
1
8

Sample Output

08:00:40 am
08:00:08 am

给定n个人 
他们独自购票的时间 
和他们临近人买票的时间, 
问你如何选择,可以使时间最短。 
得到状态转移方程。 
dp[i]=min(dp[i-1]+单人i,dp[i-2],双人);


#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;
int a[2010];
int b[2010];
int dp[2010];
int main()
{
    int t;
    int x,y,c;
    scanf("%d",&t);
    while(t--)
    {
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        memset(dp,0,sizeof(dp));
        int n;
        scanf("%d",&n);
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&a[i]);
        }
        for(int i=2; i<=n; i++)
        {
            scanf("%d",&b[i]);
        }
        dp[1]=a[1];
        dp[2]=min(a[1]+a[2],b[2]);//前键
        for(int i=3; i<=n; i++)
        {
            dp[i]=min(dp[i-1]+a[i],dp[i-2]+b[i]);
        }
        int ans=dp[n];
        x=8,y=c=0;                              //时间转换
        y+=(ans/60);
        c=ans%60;
        x+=(y/60);
        y%=60;
        if(x>12||(x==12&&(y>0||c>0)))
            printf("%.2d:%.2d:%.2d pm\n",x,y,c);
        else
            printf("%.2d:%.2d:%.2d am\n",x,y,c);
    }
    return 0;
}

 

#define HEADER1"--------------------BOOK TICKET-------------------\n" #define HEADER2"| number |start city|reach city|takeofftime|receivetime|price|ticketnumber|\n" #define HEADER3"|-------|------|-------|-------|-------|-------|-------|-------|-------|\n" #define FORMAT "|%-10s|%-10s|%-10s|%-10s |%-10s |%-5d| %5d |\n" #define DATA p->data.num,p->data.startcity,p->data.reachcity,p->data.takeofftime,p->data.receivetime,p->data.price,p->data.ticketnum //定义火车信息的结构体 struct train { char num[10];//列车号 char startcity[10];//出发城市 char reachcity[10];//目的城市 char takeofftime[10];//发车时间 char receivetime[10];//到达时间 int price;//票价 int ticketnum;//票数 }; //订票人的信息 struct man { char num[10];//ID char name[10];//姓名 int bookNum;//订的票数 }; //定义火车票信息链表的节点结构 typedef struct node { struct train data; struct node* next; }Node, * Link; //定义订票人链表的节点结构 typedef struct Man { struct man data; struct Man* next; }book,*bookLink; #include<conio.h> #include<stdio.h> #include<stdlib.h> #include<string.h> #include<dos.h> #include<windows.h> int saveflag = 0; void menu() { puts("\n\n"); puts("\t\t|-------------------------------------|"); puts("\t\t| Booking Tickets |"); puts("\t\t|-------------------------------------|"); puts("\t\t| 0:quit the system "); puts("\t\t| 1:Insert a train informattion "); puts("\t\t| 2:Search atrain information "); puts("\t\t| 3:Book a train ticket "); puts("\t\t| 4:Modify the train information "); puts("\t\t| 5:Show the train information "); puts("\t\t| 6:save information file "); puts("\t\t|------------------------------------_|"); } int main() { FILE* fp1, * fp2; Node* p, * r; char ch1, ch2; Link l; bookLink k; book* t, * h; int sel; l = (Node*)malloc(sizeof(Node)); l->next = NULL; r = l; k = (book*)malloc(sizeof(book)); k->next = NULL; h = k; fp1 = fopen("f:train.txt", "rb+"); if (fp1 == NULL) { printf("can't open the file!"); return 0; } while (!feof(fp1)) { p = (Node*)malloc(sizeof(Node)); if (fread(p, sizeof(Node), 1, fp1) == 1); { p->next = NULL; r->next = p; r = p; } } fclose(fp1); fp2 = fopen("f:\\man.txt", "ab+"); if (fp2 == NULL) { printf("can't open the file!"); return 0; } while (!feof(fp2)) { t = (book*)malloc(sizeof(book)); if (fread(t, sizeof(book), 1, fp2) == 1) { t->next = NULL; h->next = t; h = t; } } fclose(fp2); while (1) { system("CLS"); menu(); printf("\tplease choose(0-6): "); scanf_s("%d", &sel); system("CLS"); if (sel == 0) { if (saveflag == 1) { getchar(); printf("\nthe file have been changed!do you want to save it(y/n)?\n"); scanf_s("%c", &ch1); if (ch1 == 'y' || ch1 == 'Y') { SaveBookInfo(k); SaveTrainInfo(l); } } printf("\nThank you!!You are welcome too\n"); } switch (sel) { case 1: Traininfo(l); break; case 2: searchtrain(l); break; case 3: Bookticket(l, k); break; case 4: Modify(l); break; case 5: showtrain(l); break; case 6: SaveTrainInfo(l); SaveBookInfo; break; case 0: return 0; } printf("\nplease press anyy key to continue…………"); getch(); } } void Traininfo(Link linkhead) { struct node* p, * r, * s; char num[10]; r = linkhead; s = linkhead->next; while (r->next != NULL) r = r->next; while (1) { printf("please inpt the number of the train(0-return)"); scanf_s("%s", num); if (strcmp(num, "0") == 0) break; while (s) { if (strcmp(s->data.num, num) == 0) { printf("the train '%s'is existing!\n", num); return; } s = s->next; } p = (struct node*)malloc(sizeof(struct node)); strcpy(p->data.num, num); printf("Input the city where the trian will start:"); scanf_s("%s", p->data.startcity); printf("Input the city where the train reach:"); scanf_s("%s", p->data.reachcity); printf("Input the time which the train take off:"); scanf_s("%s", p->data.takeofftime); printf("Input the time which the train receive:"); scanf_s("%s", &p->data.price); printf("Input the number of booked tickets:"); scanf_s("%d", &p->data.ticketnum); p->next = NULL; r->next = p; r = p; saveflag = 1; } } void searchtrain(Link l) { Node* s[10], * r; int sel, k, i = 0; char str1[5], str2[10]; if (!l->next) { printf("There is not any record!"); return; } printf("Choose the way:\n1:according to the number of train;\n2:according to the city:"); scanf_s("%d", &sel); if (sel == 1) { printf("Input the nuber of train:"); scanf_s("%s", str1); r = l->next; while (r != NULL) if (strcmp(r->data.num, str1) == 0) { s[i] = r; i++; break; } else r = r->next; } else if (sel == 2) { printf("Input the city you want to go:"); scanf_s("%s", str2); r = l->next; while (r != NULL) if (strcmp(r->data.reachcity, str2) == 0) { s[i] = r; i++; r = r->next; } else r = r->next; } if (i == 0) printf("can not find!"); else { printheader(); for (k = 0; k < i; k++) printdata(s[k]); } } void printheader() { printf(HEADER1); printf(HEADER2); printf(HEADER3); } void printdata(Node *q) { Node* p; p = q; printf(FORMAT, DATA); } void Bookticket(Link l, bookLink k) { Node* r[10], * p; char ch[2], tnum[10], str[10], str1[10], str2[10]; book* q, * h; int i = 0, t = 0, flag = 0, dnum; q = k; while (q->next != NULL) q = q->next; printf("Input the city you want to go:"); scanf_s("%s", &str); p = l->next; while (p != NULL) { if (strcmp(p->data.reachcity, str) == 0) { r[i] = p; i++; } p = p->next; } printf("\n\nthe number of record have %d\n", i); printheader(); for (t = 0; t < i; t++) printdata(r[t]); if (i == 0) printf("\nSorry!Can't find the train for you!\n"); else { printf("\ndo you want to book it?<y/n>"); scanf_s("%s", ch); if (strcmp(ch, "Y") == 0 || strcmp(ch, "y") == 0) { h = (book*)malloc(sizeof(book)); printf("Input your name:"); scanf_s("%s", &str1); strcpy(h->data.name, str1); printf("Input your id:"); scanf_s("%s", &str2); strcpy(h->data.num, str2); printf("please input the number of the train:"); scanf_s("%s", tnum); for(t=0;t<i;t++) if (strcmp(r[t]->data.num, tnum) == 0) { if (r[t]->data.ticketnum < 1) { printf("sorry,no ticket!"); sleep(2); return; } printf("remain %d tickets\n", r[t]->data.ticketnum); flag = 1; break; } if (flag == 0) { printf("input error"); sleep(2); return; } printf("Input your bookNum:"); scanf_s("%d", &dnum); r[t]->data.ticketnum = r[t]->data.ticketnum - dnum; h->data.bookNum = dnum; h->next = NULL; q->next = h; q = h; printf("\nLucky!you have booked a tiket!"); getch(); saveflag = 1; } } } void Modify(Link l) { Node* p; char tnum[10], ch; p = l->next; if (!p) { printf("\nthere isn't record for you to modify!\n"); return; } else { printf("\nDo you want to modify it?(y/n)\n"); getchar(); scanf_s("%c", &ch); if (ch == 'y' || ch == 'Y') { printf("\nInput the number of the train:"); scanf_s("%S", tnum); while (p != NULL) if (strcmp(p->data.num, tnum) == 0) break; else p = p->next; if (p) { printf("Input new number of train:"); scanf(" % s", &p->data.num); printf("Input new city the train will start:"); scanf("%s", &p->data.startcity); printf("Input new city the train will reach:"); scanf("%s", &p->data.reachcity); printf("Input new time the train take off"); scanf("%s", &p->data.takeofftime); printf("Input new time the train reach:"); scanf("%s", &p->data.receivetime); printf("Input new price of the ticket:."); scanf("%d", &p->data.price); printf("Input new number of people who have booked ticket:"); scanf("%d", &p->data.ticketnum); printf("inmodifying record is sucessfulln"); saveflag = 1; } else printf("\tcan't find the record!"); } } } void showtrain(Link l) { Node* p; p = l->next; printheader(); if (l->next == NULL) printf("no records!"); else while (p != NULL) { printdata(p); p = p->next; } } void SaveTrainInfo(Link l) { FILE* fp; Node* p; int count = 0, flag = 1; fp = fopen("f:\\train.txt", "wb"); if (fp == NULL) { printf("the file can't be opened!"); return; } p = l->next; while (p) { if (fwrite(p, sizeof(Node), 1, fp) == 1) { p = p->next; count++; } else { flag = 0; break; } } if (flag) { printf("saved %d train records\n", count); saveflag = 0; } fclose(fp); } void SaveBookInfo(bookLink k) { FILE* fp; book* p; int count = 0, flag = 1; fp = fopen("f:\\man.txt", "wb"); if (fp == NULL) { printf("the file can't be opened!"); return; } p = k->next; while (p) { if (fwrite(p, sizeof(book), 1, fp) == 1) { p = p->next; count++; } else { flag = 0; break; } } if (flag) { printf("saved %d booking record\n", count); saveflag = 0; } fclose(fp); }
最新发布
12-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值