CodeWars 排队买票的人手持25,50,100,判断当前的零钱是否够找零

*Description:

The new “Avengers” movie has just been released! There are a lot of people at the cinema box office standing in a huge line. Each of them has a single 100, 50 or 25 dollars bill. A “Avengers” ticket costs 25 dollars.

Vasya is currently working as a clerk. He wants to sell a ticket to every single person in this line.

Can Vasya sell a ticket to each person and give the change if he initially has no money and sells the tickets strictly in the order people follow in the line?

Return YES, if Vasya can sell a ticket to each person and give the change. Otherwise return NO.

Examples:
// * Java *

Line.Tickets(new int[] {25, 25, 50}) // => YES
Line.Tickets(new int []{25, 100})
// => NO. Vasya will not have enough money to give change to 100 dollars
*

public class Line {
    public static String Tickets(int[] peopleInLine){
        int bill25 = 0, bill50 = 0;
        for (int payment : peopleInLine){
            if(payment==25){
                bill25++;
            } else if(payment==50){
                bill25--;
                bill50++;
            } else if(payment==100){
                if(bill50>0){
                    bill50--;
                    bill25--;
                } else{
                    bill25-=3;
                }
            }
            if(bill25<0 || bill50 <0){
                return "NO";
            }
        }
        return "YES";
    }
}

另一种方式:

public class Line {
  public static String Tickets(int[] peopleInLine)
  {
      int i, sum=0, change = 0;
      String a = "";
      for(i=0; i<peopleInLine.length; i++) {

          sum += 25;
          change = (peopleInLine[i] - 25);
          sum -= change;

          if(sum < change) {
            a = "NO";
          }
          else a = "YES";
      }
      return a;
  }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
排队买票可以用 C 语言来实现。一般来说,可以使用队列数据结构来解决这个问。具体实现步骤如下: 1. 定义一个队列数据结构,包括队列头指针、队列尾指针和队列元素个数等成员变量。 2. 定义一个函数来模拟售票员的行为,比如 sell_ticket() 函数。每当有人来买票时,就将其加入队列中。 3. 定义一个函数来模拟买票者的行为,比如 buy_ticket() 函数。每当有人来买票时,就从队列中取出一个人,并将其从队列中删除。 4. 在主函数中,可以模拟一定时间内的售票和买票过程。具体来说,可以使用循环来模拟时间的流逝,每隔一定时间调用 sell_ticket() 或 buy_ticket() 函数。 下面是一个简单的排队买票的实现示例: ```c #include <stdio.h> #include <stdlib.h> #define MAX_QUEUE_SIZE 100 typedef struct { int* data; int front; int rear; int size; } Queue; void init_queue(Queue* q) { q->data = (int*)malloc(sizeof(int) * MAX_QUEUE_SIZE); q->front = 0; q->rear = -1; q->size = 0; } int is_full(Queue* q) { return q->size == MAX_QUEUE_SIZE; } int is_empty(Queue* q) { return q->size == 0; } void enqueue(Queue* q, int x) { if (is_full(q)) { printf("Queue is full.\n"); return; } q->rear = (q->rear + 1) % MAX_QUEUE_SIZE; q->data[q->rear] = x; q->size++; } int dequeue(Queue* q) { if (is_empty(q)) { printf("Queue is empty.\n"); return -1; } int x = q->data[q->front]; q->front = (q->front + 1) % MAX_QUEUE_SIZE; q->size--; return x; } void sell_ticket(Queue* q) { int x; printf("Enter the number of tickets to sell: "); scanf("%d", &x); for (int i = 0; i < x; i++) { printf("Enter the ticket buyer's ID: "); scanf("%d", &x); enqueue(q, x); } } void buy_ticket(Queue* q) { if (is_empty(q)) { printf("No ticket to buy.\n"); return; } int x = dequeue(q); printf("Ticket buyer's ID: %d\n", x); } int main() { Queue q; init_queue(&q); int t; printf("Enter the simulation time (in seconds): "); scanf("%d", &t); for (int i = 0; i < t; i++) { if (i % 5 == 0) { sell_ticket(&q); } if (i % 3 == 0) { buy_ticket(&q); } } return 0; } ``` 在这个示例程序中,我们定义了一个 Queue 结构体来表示队列,包括队列元素数组、队列头指针、队列尾指针和队列元素个数等成员变量。我们使用 init_queue() 函数来初始化队列,使用 is_full() 和 is_empty() 函数来判断队列是否已满或已空,使用 enqueue() 和 dequeue() 函数来向队列中添加元素或删除元素。 在主函数中,我们通过循环来模拟一定时间内的售票和买票过程。在每个五秒钟时刻,我们调用 sell_ticket() 函数来模拟售票员的行为,将买票者的 ID 加入队列中。在每个三秒钟时刻,我们调用 buy_ticket() 函数来模拟买票者的行为,从队列中取出一个买票者的 ID。 这只是一个简单的示例程序,实际应用中可能还需要考虑更多的细节和复杂情况。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值