0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

paizaラーニングレベルアップ問題集の「クラス」をやってみた (2)

Posted at

paizaラーニングレベルアップ問題集の「クラス」をやってみました。


問題
クラスの継承

デフォルト引数

静的メンバ


2問目と3問目については、前問との差分を色付けしてあります。


C
クラスの継承
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>

typedef struct Customer {
	int amount;
	void (*take_food)(struct Customer*, int);
	void (*take_softdrink)(struct Customer*, int);
	void (*take_alcohol)(struct Customer*, int);
} Customer;

void Customer_take_food(Customer *self, int price) {
	self->amount += price;
}

void Customer_take_softdrink(Customer *self, int price) {
	self->amount += price;
}

void Customer_take_alcohol(Customer *self, int price) { }

void Customer_init(Customer *self) {
	self->amount = 0;
	self->take_food = Customer_take_food;
	self->take_softdrink = Customer_take_softdrink;
	self->take_alcohol = Customer_take_alcohol;
}

Customer* new_Customer() {
	Customer *obj = (Customer*) malloc(sizeof(Customer));
	Customer_init(obj);
	return obj;
}

typedef struct Adult {
	Customer super;
	bool alcohol;
	void (*super_take_food)(Customer*, int);
} Adult;

void Adult_take_food(Customer *self, int price) {
	Adult *adult = (Adult*) self;
	if (adult->alcohol) adult->super_take_food(self, price - 200);
	else adult->super_take_food(self, price);
}

void Adult_take_alcohol(Customer *self, int price) {
	self->amount += price;
	Adult *adult = (Adult*) self;
	adult->alcohol = true;
}

void Adult_init(Adult *self) {
	Customer_init(&self->super);
	self->super_take_food = self->super.take_food;
	self->super.take_food = Adult_take_food;
	self->super.take_alcohol = Adult_take_alcohol;
	self->alcohol = false;
}

Adult* new_Adult() {
	Adult *obj = (Adult*) malloc(sizeof(Adult));
	Adult_init(obj);
	return obj;
}

int main() {
	int N, k;
	scanf("%d %d", &N, &k);
	Customer *customers[N];
	for (int i = 0; i < N; i++) {
		int a;
		scanf("%d", &a);
		if (a < 20) customers[i] = new_Customer();
		else customers[i] = (Customer*) new_Adult();
	}
	while (k--) {
		int n;
		char o[10];
		int m;
		scanf("%d %s %d", &n, o, &m);
		n -= 1;
		if (!strcmp(o, "food")) customers[n]->take_food(customers[n], m);
		else if (!strcmp(o, "softdrink")) customers[n]->take_softdrink(customers[n], m);
		else if (!strcmp(o, "alcohol")) customers[n]->take_alcohol(customers[n], m);
	}
	for (int i = 0; i < N; i++) {
		printf("%d\n", customers[i]->amount);
		free(customers[i]);
		customers[i] = NULL;
	}
	return 0;
}
デフォルト引数
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>

typedef struct Customer {
	int amount;
	void (*take_food)(struct Customer*, int);
	void (*take_softdrink)(struct Customer*, int);
	void (*take_alcohol)(struct Customer*, int);
+	void (*take_beer)(struct Customer*);
} Customer;

void Customer_take_food(Customer *self, int price) {
	self->amount += price;
}

void Customer_take_softdrink(Customer *self, int price) {
	self->amount += price;
}

void Customer_take_alcohol(Customer *self, int price) { }

+void Customer_take_beer(Customer *self) {
+	self->take_alcohol(self, 500);
+}

void Customer_init(Customer *self) {
	self->amount = 0;
	self->take_food = Customer_take_food;
	self->take_softdrink = Customer_take_softdrink;
	self->take_alcohol = Customer_take_alcohol;
+	self->take_beer = Customer_take_beer;
}

Customer* new_Customer() {
	Customer *obj = (Customer*) malloc(sizeof(Customer));
	Customer_init(obj);
	return obj;
}

typedef struct Adult {
	Customer super;
	bool alcohol;
	void (*super_take_food)(Customer*, int);
} Adult;

void Adult_take_food(Customer *self, int price) {
	Adult *adult = (Adult*) self;
	if (adult->alcohol) adult->super_take_food(self, price - 200);
	else adult->super_take_food(self, price);
}

void Adult_take_alcohol(Customer *self, int price) {
	self->amount += price;
	Adult *adult = (Adult*) self;
	adult->alcohol = true;
}

void Adult_init(Adult *self) {
	Customer_init(&self->super);
	self->super_take_food = self->super.take_food;
	self->super.take_food = Adult_take_food;
	self->super.take_alcohol = Adult_take_alcohol;
	self->alcohol = false;
}

Adult* new_Adult() {
	Adult *obj = (Adult*) malloc(sizeof(Adult));
	Adult_init(obj);
	return obj;
}

int main() {
	int N, k;
	scanf("%d %d", &N, &k);
	Customer *customers[N];
	for (int i = 0; i < N; i++) {
		int a;
		scanf("%d", &a);
		if (a < 20) customers[i] = new_Customer();
		else customers[i] = (Customer*) new_Adult();
	}
	while (k--) {
		int n;
		char o[10];
-		int m;
-		scanf("%d %s %d", &n, o, &m);
+		scanf("%d %s", &n, o);
		n -= 1;
+		if (!strcmp(o, "0")) {
+			customers[n]->take_beer(customers[n]);
+		} else {
+			int m;
+			scanf("%d", &m);
			if (!strcmp(o, "food")) customers[n]->take_food(customers[n], m);
			else if (!strcmp(o, "softdrink")) customers[n]->take_softdrink(customers[n], m);
			else if (!strcmp(o, "alcohol")) customers[n]->take_alcohol(customers[n], m);
+		}
	}
	for (int i = 0; i < N; i++) {
		printf("%d\n", customers[i]->amount);
		free(customers[i]);
		customers[i] = NULL;
	}
	return 0;
}
静的メンバ
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>

+static int num_of_left = 0;

typedef struct Customer {
	int amount;
	void (*take_food)(struct Customer*, int);
	void (*take_softdrink)(struct Customer*, int);
	void (*take_alcohol)(struct Customer*, int);
	void (*take_beer)(struct Customer*);
+	void (*accounting)(struct Customer*);
} Customer;

void Customer_take_food(Customer *self, int price) {
	self->amount += price;
}

void Customer_take_softdrink(Customer *self, int price) {
	self->amount += price;
}

void Customer_take_alcohol(Customer *self, int price) { }

void Customer_take_beer(Customer *self) {
	self->take_alcohol(self, 500);
}

+void Customer_accounting(Customer *self) {
+	printf("%d\n", self->amount);
+	num_of_left++;
+}

void Customer_init(Customer *self) {
	self->amount = 0;
	self->take_food = Customer_take_food;
	self->take_softdrink = Customer_take_softdrink;
	self->take_alcohol = Customer_take_alcohol;
	self->take_beer = Customer_take_beer;
+	self->accounting = Customer_accounting;
}

Customer* new_Customer() {
	Customer *obj = (Customer*) malloc(sizeof(Customer));
	Customer_init(obj);
	return obj;
}

typedef struct Adult {
	Customer super;
	bool alcohol;
	void (*super_take_food)(Customer*, int);
} Adult;

void Adult_take_food(Customer *self, int price) {
	Adult *adult = (Adult*) self;
	if (adult->alcohol) adult->super_take_food(self, price - 200);
	else adult->super_take_food(self, price);
}

void Adult_take_alcohol(Customer *self, int price) {
	self->amount += price;
	Adult *adult = (Adult*) self;
	adult->alcohol = true;
}

void Adult_init(Adult *self) {
	Customer_init(&self->super);
	self->super_take_food = self->super.take_food;
	self->super.take_food = Adult_take_food;
	self->super.take_alcohol = Adult_take_alcohol;
	self->alcohol = false;
}

Adult* new_Adult() {
	Adult *obj = (Adult*) malloc(sizeof(Adult));
	Adult_init(obj);
	return obj;
}

int main() {
	int N, k;
	scanf("%d %d", &N, &k);
	Customer *customers[N];
	for (int i = 0; i < N; i++) {
		int a;
		scanf("%d", &a);
		if (a < 20) customers[i] = new_Customer();
		else customers[i] = (Customer*) new_Adult();
	}
	while (k--) {
		int n;
		char o[10];
		scanf("%d %s", &n, o);
		n -= 1;
		if (!strcmp(o, "0")) {
			customers[n]->take_beer(customers[n]);
+		} else if (!strcmp(o, "A")) {
+			customers[n]->accounting(customers[n]);
		} else {
			int m;
			scanf("%d", &m);
			if (!strcmp(o, "food")) customers[n]->take_food(customers[n], m);
			else if (!strcmp(o, "softdrink")) customers[n]->take_softdrink(customers[n], m);
			else if (!strcmp(o, "alcohol")) customers[n]->take_alcohol(customers[n], m);
		}
	}
+	printf("%d\n", num_of_left);
	for (int i = 0; i < N; i++) {
-		printf("%d\n", customers[i]->amount);
		free(customers[i]);
		customers[i] = NULL;
	}
	return 0;
}

C++
クラスの継承
#include <iostream>
#include <vector>

using namespace std;

class Customer {
protected:
	int amount;
public:
	Customer() {
		amount = 0;
	}

	int get_amount() {
		return amount;
	}

	virtual void take_food(int m) {
		amount += m;
	}

	void take_softdrink(int m) {
		amount += m;
	}

	virtual void take_alcohol(int m) { }

	virtual ~Customer() { }
};

class Adult: public Customer {
	bool alcohol;

public:
	Adult() : Customer() {
		alcohol = false;
	}

	void take_food(int m) {
		if (alcohol) Customer::take_food(m - 200);
		else Customer::take_food(m);
	}

	void take_alcohol(int m) {
		amount += m;
		alcohol = true;
	}
};

int main() {
	int N, k;
	cin >> N >> k;
	vector<Customer*> customers(N);
	for (int i = 0; i < N; i++) {
		int a;
		cin >> a;
		customers[i] = (a < 20 ? new Customer() : new Adult());
	}
	while (k--) {
		int n;
		string o;
		int m;
		cin >> n >> o >> m;
		n -= 1;
		if (o == "food") customers[n]->take_food(m);
		else if (o == "softdrink") customers[n]->take_softdrink(m);
		else if (o == "alcohol") customers[n]->take_alcohol(m);
	}
	for (int i = 0; i < N; i++) {
		cout << customers[i]->get_amount() << endl;
		delete customers[i];
	}
}
デフォルト引数
#include <iostream>
#include <vector>

using namespace std;

class Customer {
protected:
	int amount;
public:
	Customer() {
		amount = 0;
	}

	int get_amount() {
		return amount;
	}

	virtual void take_food(int m) {
		amount += m;
	}

	void take_softdrink(int m) {
		amount += m;
	}

-	virtual void take_alcohol(int m) { }
+	virtual void take_alcohol(int m = 500) { }

	virtual ~Customer() { }
};

class Adult: public Customer {
	bool alcohol;

public:
	Adult() : Customer() {
		alcohol = false;
	}

	void take_food(int m) {
		if (alcohol) Customer::take_food(m - 200);
		else Customer::take_food(m);
	}

-	void take_alcohol(int m) {
+	void take_alcohol(int m = 500) {
		amount += m;
		alcohol = true;
	}
};

int main() {
	int N, k;
	cin >> N >> k;
	vector<Customer*> customers(N);
	for (int i = 0; i < N; i++) {
		int a;
		cin >> a;
		customers[i] = (a < 20 ? new Customer() : new Adult());
	}
	while (k--) {
		int n;
		string o;
-		int m;
-		cin >> n >> o >> m;
		cin >> n >> o;
		n -= 1;
+		if (o == "0") {
+			customers[n]->take_alcohol();
+		} else {
+			int m;
+			cin >> m;
			if (o == "food") customers[n]->take_food(m);
			else if (o == "softdrink") customers[n]->take_softdrink(m);
			else if (o == "alcohol") customers[n]->take_alcohol(m);
+		}
	}
	for (int i = 0; i < N; i++) {
		cout << customers[i]->get_amount() << endl;
		delete customers[i];
	}
}
静的メンバ
#include <iostream>
#include <vector>

using namespace std;

class Customer {
protected:
	int amount;
public:
+	static int num_of_left;

	Customer() {
		amount = 0;
	}

	int get_amount() {
		return amount;
	}

	virtual void take_food(int m) {
		amount += m;
	}

	void take_softdrink(int m) {
		amount += m;
	}

	virtual void take_alcohol(int m = 500) { }

+	void accounting() {
+		cout << amount << endl;
+		num_of_left++;
+	}

	virtual ~Customer() { }
};

class Adult: public Customer {
	bool alcohol;

public:
	Adult() : Customer() {
		alcohol = false;
	}

	void take_food(int m) {
		if (alcohol) Customer::take_food(m - 200);
		else Customer::take_food(m);
	}

	void take_alcohol(int m = 500) {
		amount += m;
		alcohol = true;
	}
};

+int Customer::num_of_left = 0;

int main() {
	int N, k;
	cin >> N >> k;
	vector<Customer*> customers(N);
	for (int i = 0; i < N; i++) {
		int a;
		cin >> a;
		customers[i] = (a < 20 ? new Customer() : new Adult());
	}
	while (k--) {
		int n;
		string o;
		cin >> n >> o;
		n -= 1;
		if (o == "0") {
			customers[n]->take_alcohol();
+		} else if (o == "A") {
+			customers[n]->accounting();
		} else {
			int m;
			cin >> m;
			if (o == "food") customers[n]->take_food(m);
			else if (o == "softdrink") customers[n]->take_softdrink(m);
			else if (o == "alcohol") customers[n]->take_alcohol(m);
		}
	}
+	cout << Customer::num_of_left << endl;
	for (int i = 0; i < N; i++) {
-		cout << customers[i]->get_amount() << endl;
		delete customers[i];
	}
}

C#
クラスの継承
using System;

class Customer
{
	protected int amount;
	public int Amount{
		get { return amount; }
	}

	public Customer()
	{
		amount = 0;
	}

	public virtual void TakeFood(int price)
	{
		amount += price;
	}

	public void TakeSoftdrink(int price)
	{
		amount += price;
	}

	public virtual void TakeAlcohol(int price) { }
}

class Adult : Customer
{
	private bool alcohol;

	public Adult() : base()
	{
		alcohol = false;
	}

	public override void TakeFood(int price)
	{
		if (alcohol)
		{
			base.TakeFood(price - 200);
		}
		else
		{
			base.TakeFood(price);
		}
	}

	public override void TakeAlcohol(int price)
	{
		alcohol = true;
		amount += price;
	}
}

class Program
{
	public static void Main()
	{
		int[] nk = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
		int N = nk[0], k = nk[1];
		Customer[] customers = new Customer[N];
		for (int i = 0; i < N; i++) customers[i] = int.Parse(Console.ReadLine()) < 20 ? new Customer() : new Adult();
		for (; k > 0; k--)
		{
			string[] query = Console.ReadLine().Split();
			int n = int.Parse(query[0]) - 1;
			string o = query[1];
			int m = int.Parse(query[2]);
			if ("food".Equals(o))
			{
				customers[n].TakeFood(m);
			}
			else if ("softdrink".Equals(o))
			{
				customers[n].TakeSoftdrink(m);
			}
			else if ("alcohol".Equals(o))
			{
				customers[n].TakeAlcohol(m);
			}
		}
		for (int i = 0; i < N; i++) Console.WriteLine(customers[i].Amount);
	}
}
デフォルト引数
using System;

class Customer
{
	protected int amount;
	public int Amount{
		get { return amount; }
	}

	public Customer()
	{
		amount = 0;
	}

	public virtual void TakeFood(int price)
	{
		amount += price;
	}

	public void TakeSoftdrink(int price)
	{
		amount += price;
	}

-	public virtual void TakeAlcohol(int price) { }
+	public virtual void TakeAlcohol(int price = 500) { }
}

class Adult : Customer
{
	private bool alcohol;

	public Adult() : base()
	{
		alcohol = false;
	}

	public override void TakeFood(int price)
	{
		if (alcohol)
		{
			base.TakeFood(price - 200);
		}
		else
		{
			base.TakeFood(price);
		}
	}

	public override void TakeAlcohol(int price)
	{
		alcohol = true;
		amount += price;
	}
}

class Program
{
	public static void Main()
	{
		int[] nk = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
		int N = nk[0], k = nk[1];
		Customer[] customers = new Customer[N];
		for (int i = 0; i < N; i++) customers[i] = int.Parse(Console.ReadLine()) < 20 ? new Customer() : new Adult();
		for (; k > 0; k--)
		{
			string[] query = Console.ReadLine().Split();
			int n = int.Parse(query[0]) - 1;
			string o = query[1];
+			if ("0".Equals(o))
+			{
+				customers[n].TakeAlcohol();
+			}
+			else
+			{
				int m = int.Parse(query[2]);
				if ("food".Equals(o))
				{
					customers[n].TakeFood(m);
				}
				else if ("softdrink".Equals(o))
				{
					customers[n].TakeSoftdrink(m);
				}
				else if ("alcohol".Equals(o))
				{
					customers[n].TakeAlcohol(m);
				}
+			}
		}
		for (int i = 0; i < N; i++) Console.WriteLine(customers[i].Amount);
	}
}
静的メンバ
using System;

class Customer
{
+	private static int numOfLeft = 0;
+	public static int NumOfLeft {
+		get { return numOfLeft; }
+	}

	protected int amount;
-	public int Amount{
-		get { return amount; }
-	}

	public Customer()
	{
		amount = 0;
	}

	public virtual void TakeFood(int price)
	{
		amount += price;
	}

	public void TakeSoftdrink(int price)
	{
		amount += price;
	}

	public virtual void TakeAlcohol(int price = 500) { }

+	public void Accounting()
+	{
+		Console.WriteLine(amount);
+		numOfLeft++;
+	}
}

class Adult : Customer
{
	private bool alcohol;

	public Adult() : base()
	{
		alcohol = false;
	}

	public override void TakeFood(int price)
	{
		if (alcohol)
		{
			base.TakeFood(price - 200);
		}
		else
		{
			base.TakeFood(price);
		}
	}

	public override void TakeAlcohol(int price)
	{
		alcohol = true;
		amount += price;
	}
}

class Program
{
	public static void Main()
	{
		int[] nk = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
		int N = nk[0], k = nk[1];
		Customer[] customers = new Customer[N];
		for (int i = 0; i < N; i++) customers[i] = int.Parse(Console.ReadLine()) < 20 ? new Customer() : new Adult();
		for (; k > 0; k--)
		{
			string[] query = Console.ReadLine().Split();
			int n = int.Parse(query[0]) - 1;
			string o = query[1];
			if ("0".Equals(o))
			{
				customers[n].TakeAlcohol();
			}
+			else if ("A".Equals(o))
+			{
+				customers[n].Accounting();
+			}
			else
			{
				int m = int.Parse(query[2]);
				if ("food".Equals(o))
				{
					customers[n].TakeFood(m);
				}
				else if ("softdrink".Equals(o))
				{
					customers[n].TakeSoftdrink(m);
				}
				else if ("alcohol".Equals(o))
				{
					customers[n].TakeAlcohol(m);
				}
			}
		}
-		for (int i = 0; i < N; i++) Console.WriteLine(customers[i].Amount);
+		Console.WriteLine(Customer.NumOfLeft);
	}
}

Go
クラスの継承
package main
import "fmt"

type CustomerInterface interface {
	GetAmount() int
	TakeFood(int)
	TakeSoftdrink(int)
	TakeAlcohol(int)
}

type Customer struct {
	amount int
}

func NewCustomer() *Customer {
	return &Customer{
		amount: 0,
	}
}

func (self *Customer) GetAmount() int {
	return self.amount
}

func (self *Customer) TakeFood(price int) {
	self.amount += price
}

func (self *Customer) TakeSoftdrink(price int) {
	self.amount += price
}

func (self *Customer) TakeAlcohol(price int) {}

type Adult struct {
	Customer
	alcohol bool
}

func NewAdult() *Adult {
	return &Adult{
		Customer: Customer{amount: 0},
		alcohol:  false,
	}
}

func (self *Adult) TakeFood(price int) {
	if self.alcohol {
		self.Customer.TakeFood(price - 200)
	} else {
		self.Customer.TakeFood(price)
	}
}

func (self *Adult) TakeAlcohol(price int) {
	self.alcohol = true
	self.amount += price
}

func main() {
	var N, K int
	fmt.Scan(&N, &K)
	customers := make([]CustomerInterface, N)
	for i := 0; i < N; i++ {
		var a int
		fmt.Scan(&a);
		if a < 20 {
			customers[i] = NewCustomer()
		} else {
			customers[i] = NewAdult()
		}
	}
	for i := 0; i < K; i++ {
		var n int
		var o string
		var m int
		fmt.Scan(&n, &o, &m)
		n -= 1
		if o == "food" {
			customers[n].TakeFood(m)
		} else if o == "softdrink" {
			customers[n].TakeSoftdrink(m)
		} else if o == "alcohol" {
			customers[n].TakeAlcohol(m)
		}
	}
	for i := 0; i < N; i++ {
		fmt.Println(customers[i].GetAmount())
	}
}
デフォルト引数
package main
import "fmt"

type CustomerInterface interface {
	GetAmount() int
	TakeFood(int)
	TakeSoftdrink(int)
	TakeAlcohol(int)
+	TakeBeer()
}

type Customer struct {
	amount int
}

func NewCustomer() *Customer {
	return &Customer{
		amount: 0,
	}
}

func (self *Customer) GetAmount() int {
	return self.amount
}

func (self *Customer) TakeFood(price int) {
	self.amount += price
}

func (self *Customer) TakeSoftdrink(price int) {
	self.amount += price
}

func (self *Customer) TakeAlcohol(price int) {}

+func (self *Customer) TakeBeer() {
+	self.TakeAlcohol(500)
+}

type Adult struct {
	Customer
	alcohol bool
}

func NewAdult() *Adult {
	return &Adult{
		Customer: Customer{amount: 0},
		alcohol:  false,
	}
}

func (self *Adult) TakeFood(price int) {
	if self.alcohol {
		self.Customer.TakeFood(price - 200)
	} else {
		self.Customer.TakeFood(price)
	}
}

func (self *Adult) TakeAlcohol(price int) {
	self.alcohol = true
	self.amount += price
}

+func (self *Adult) TakeBeer() {
+	self.TakeAlcohol(500)
+}

func main() {
	var N, K int
	fmt.Scan(&N, &K)
	customers := make([]CustomerInterface, N)
	for i := 0; i < N; i++ {
		var a int
		fmt.Scan(&a);
		if a < 20 {
			customers[i] = NewCustomer()
		} else {
			customers[i] = NewAdult()
		}
	}
	for i := 0; i < K; i++ {
		var n int
		var o string
-		var m int
-		fmt.Scan(&n, &o, &m)
		fmt.Scan(&n, &o)
		n -= 1
+		if o == "0" {
+			customers[n].TakeBeer()
+		} else {
+			var m int
+			fmt.Scan(&m)
			if o == "food" {
				customers[n].TakeFood(m)
			} else if o == "softdrink" {
				customers[n].TakeSoftdrink(m)
			} else if o == "alcohol" {
				customers[n].TakeAlcohol(m)
			}
		}
+	}
	for i := 0; i < N; i++ {
		fmt.Println(customers[i].GetAmount())
	}
}
静的メンバ
package main
import "fmt"

+var numOfLeft int = 0

type CustomerInterface interface {
-	GetAmount() int
	TakeFood(int)
	TakeSoftdrink(int)
	TakeAlcohol(int)
	TakeBeer()
+	Accounting()
}

type Customer struct {
	amount int
}

func NewCustomer() *Customer {
	return &Customer{
		amount: 0,
	}
}

func (self *Customer) TakeFood(price int) {
	self.amount += price
}

func (self *Customer) TakeSoftdrink(price int) {
	self.amount += price
}

func (self *Customer) TakeAlcohol(price int) {}

func (self *Customer) TakeBeer() {
	self.TakeAlcohol(500)
}

+func (self *Customer) Accounting() {
+	fmt.Println(self.amount)
+	numOfLeft++
+}

type Adult struct {
	Customer
	alcohol bool
}

func NewAdult() *Adult {
	return &Adult{
		Customer: Customer{amount: 0},
		alcohol:  false,
	}
}

func (self *Adult) TakeFood(price int) {
	if self.alcohol {
		self.Customer.TakeFood(price - 200)
	} else {
		self.Customer.TakeFood(price)
	}
}

func (self *Adult) TakeAlcohol(price int) {
	self.alcohol = true
	self.amount += price
}

func (self *Adult) TakeBeer() {
	self.TakeAlcohol(500)
}

func main() {
	var N, K int
	fmt.Scan(&N, &K)
	customers := make([]CustomerInterface, N)
	for i := 0; i < N; i++ {
		var a int
		fmt.Scan(&a);
		if a < 20 {
			customers[i] = NewCustomer()
		} else {
			customers[i] = NewAdult()
		}
	}
	for i := 0; i < K; i++ {
		var n int
		var o string
		fmt.Scan(&n, &o)
		n -= 1
		if o == "0" {
			customers[n].TakeBeer()
+		} else if o == "A" {
+			customers[n].Accounting()
		} else {
			var m int
			fmt.Scan(&m)
			if o == "food" {
				customers[n].TakeFood(m)
			} else if o == "softdrink" {
				customers[n].TakeSoftdrink(m)
			} else if o == "alcohol" {
				customers[n].TakeAlcohol(m)
			}
		}
	}
-	for i := 0; i < N; i++ {
-		fmt.Println(customers[i].GetAmount())
-	}
+	fmt.Println(numOfLeft)
}

Java
クラスの継承
import java.util.*;

class Customer {
	protected int amount;

	public Customer() {
		amount = 0;
	}

	public int getAmount() {
		return amount;
	}

	public void takeFood(int price) {
		amount += price;
	}

	public void takeSoftdrink(int price) {
		amount += price;
	}

	public void takeAlcohol(int price) { }
}

class Adult extends Customer {

	private boolean alcohol;

	public Adult() {
		super();
		alcohol = false;
	}

	public void takeFood(int price) {
		if (alcohol) super.takeFood(price - 200);
		else super.takeFood(price);
	}

	public void takeAlcohol(int price) {
		alcohol = true;
		amount += price;
	}
}

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int K = sc.nextInt();
		Customer[] customers = new Customer[N];
		for (int i = 0; i < N; i++) customers[i] = sc.nextInt() < 20 ? new Customer() : new Adult();
		for (int i = 0; i < K; i++) {
			int n = sc.nextInt() - 1;
			String s = sc.next();
			int m = sc.nextInt();
			if ("food".equals(s)) {
				customers[n].takeFood(m);
			} else if ("softdrink".equals(s)) {
				customers[n].takeSoftdrink(m);
			} else if ("alcohol".equals(s)) {
				customers[n].takeAlcohol(m);
			}
		}
		sc.close();
		for (int i = 0; i < N; i++) System.out.println(customers[i].getAmount());
	}
}
デフォルト引数
import java.util.*;

class Customer {
	protected int amount;

	public Customer() {
		amount = 0;
	}

	public int getAmount() {
		return amount;
	}

	public void takeFood(int price) {
		amount += price;
	}

	public void takeSoftdrink(int price) {
		amount += price;
	}

	public void takeAlcohol(int price) { }

+	public void takeAlcohol() {
+		takeAlcohol(500);
+	}
}

class Adult extends Customer {

	private boolean alcohol;

	public Adult() {
		super();
		alcohol = false;
	}

	public void takeFood(int price) {
		if (alcohol) super.takeFood(price - 200);
		else super.takeFood(price);
	}

	public void takeAlcohol(int price) {
		alcohol = true;
		amount += price;
	}
}

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int K = sc.nextInt();
		Customer[] customers = new Customer[N];
		for (int i = 0; i < N; i++) customers[i] = sc.nextInt() < 20 ? new Customer() : new Adult();
		for (int i = 0; i < K; i++) {
			int n = sc.nextInt() - 1;
			String s = sc.next();
+			if ("0".equals(s)) {
+				customers[n].takeAlcohol();
+			} else {
				int m = sc.nextInt();
				if ("food".equals(s)) {
					customers[n].takeFood(m);
				} else if ("softdrink".equals(s)) {
					customers[n].takeSoftdrink(m);
				} else if ("alcohol".equals(s)) {
					customers[n].takeAlcohol(m);
				}
+			}
		}
		sc.close();
		for (int i = 0; i < N; i++) System.out.println(customers[i].amount);
	}
}
静的メンバ
import java.util.*;

class Customer {
+	private static int numOfLeft = 0;
	protected int amount;

	public Customer() {
		amount = 0;
	}

-	public int getAmount() {
-		return amount;
-	}

+	public static int getNumOfLeft() {
+		return numOfLeft;
+	}

	public void takeFood(int price) {
		amount += price;
	}

	public void takeSoftdrink(int price) {
		amount += price;
	}

	public void takeAlcohol(int price) { }

	public void takeAlcohol() {
		takeAlcohol(500);
	}

	public void accounting() {
		System.out.println(amount);
		numOfLeft++;
	}
}

class Adult extends Customer {

	private boolean alcohol;

	public Adult() {
		super();
		alcohol = false;
	}

	public void takeFood(int price) {
		if (alcohol) super.takeFood(price - 200);
		else super.takeFood(price);
	}

	public void takeAlcohol(int price) {
		alcohol = true;
		amount += price;
	}
}

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int K = sc.nextInt();
		Customer[] customers = new Customer[N];
		for (int i = 0; i < N; i++) customers[i] = sc.nextInt() < 20 ? new Customer() : new Adult();
		for (int i = 0; i < K; i++) {
			int n = sc.nextInt() - 1;
			String s = sc.next();
			if ("0".equals(s)) {
				customers[n].takeAlcohol();
+			} else if ("A".equals(s)) {
+				customers[n].accounting();
			} else {
				int m = sc.nextInt();
				if ("food".equals(s)) {
					customers[n].takeFood(m);
				} else if ("softdrink".equals(s)) {
					customers[n].takeSoftdrink(m);
				} else if ("alcohol".equals(s)) {
					customers[n].takeAlcohol(m);
				}
			}
		}
		sc.close();
-		for (int i = 0; i < N; i++) System.out.println(customers[i].getAmount());
+		System.out.println(Customer.getNumOfLeft());
	}
}

JavaScript
クラスの継承
class Customer {
	constructor() {
		this.amount = 0;
	}

	takeFood(price) {
		this.amount += price;
	}

	takeSoftdrink(price) {
		this.amount += price;
	}

	takeAlcohol(price) { }
}

class Adult extends Customer {
	constructor() {
		super();
		this.alcohol = false;
	}

	takeFood(price) {
		if (this.alcohol) super.takeFood(price - 200);
		else super.takeFood(price);
	}

	takeAlcohol(price) {
		this.alcohol = true;
		this.amount += price;
	}
}

const lines = require("fs").readFileSync("/dev/stdin", "utf8").trim().split('\n');
const [N, K] = lines.shift().split(' ').map(Number);
const customers = [];
for (var i = 0; i < N; i++) {
	const a = Number(lines.shift());
	customers.push(a < 20 ? new Customer() : new Adult());
}
for (var i = 0; i < K; i++) {
	const query = lines.shift().split(' ');
	const n = Number(query[0]) - 1;
	const o = query[1];
	const m = Number(query[2]);
	if (o === "food") customers[n].takeFood(m);
	else if (o === "softdrink") customers[n].takeSoftdrink(m);
	else if (o === "alcohol") customers[n].takeAlcohol(m);
}
for (var i = 0; i < N; i++) console.log(customers[i].amount);
デフォルト引数
class Customer {
	constructor() {
		this.amount = 0;
	}

	takeFood(price) {
		this.amount += price;
	}

	takeSoftdrink(price) {
		this.amount += price;
	}

-	takeAlcohol(price) { }
+	takeAlcohol(price = 500) { }
}

class Adult extends Customer {
	constructor() {
		super();
		this.alcohol = false;
	}

	takeFood(price) {
		if (this.alcohol) super.takeFood(price - 200);
		else super.takeFood(price);
	}

-	takeAlcohol(price) { }
+	takeAlcohol(price = 500) {
		this.alcohol = true;
		this.amount += price;
	}
}

const lines = require("fs").readFileSync("/dev/stdin", "utf8").trim().split('\n');
const [N, K] = lines.shift().split(' ').map(Number);
const customers = [];
for (var i = 0; i < N; i++) {
	const a = Number(lines.shift());
	customers.push(a < 20 ? new Customer() : new Adult());
}
for (var i = 0; i < K; i++) {
	const query = lines.shift().split(' ');
	const n = Number(query[0]) - 1;
	const o = query[1];
+	if (o === "0") {
+		customers[n].takeAlcohol();
+	} else {
		const m = Number(query[2]);
		if (o === "food") customers[n].takeFood(m);
		else if (o === "softdrink") customers[n].takeSoftdrink(m);
		else if (o === "alcohol") customers[n].takeAlcohol(m);
	}
+}
for (var i = 0; i < N; i++) console.log(customers[i].amount);
静的メンバ
class Customer {
+	static numOfLeft = 0;

	constructor() {
		this.amount = 0;
	}

	takeFood(price) {
		this.amount += price;
	}

	takeSoftdrink(price) {
		this.amount += price;
	}

	takeAlcohol(price = 500) { }

+	accounting() {
+		console.log(this.amount);
+		Customer.numOfLeft++;
+	}
}

class Adult extends Customer {
	constructor() {
		super();
		this.alcohol = false;
	}

	takeFood(price) {
		if (this.alcohol) super.takeFood(price - 200);
		else super.takeFood(price);
	}

	takeAlcohol(price = 500) {
		this.alcohol = true;
		this.amount += price;
	}
}

const lines = require("fs").readFileSync("/dev/stdin", "utf8").trim().split('\n');
const [N, K] = lines.shift().split(' ').map(Number);
const customers = [];
for (var i = 0; i < N; i++) {
	const a = Number(lines.shift());
	customers.push(a < 20 ? new Customer() : new Adult());
}
for (var i = 0; i < K; i++) {
	const query = lines.shift().split(' ');
	const n = Number(query[0]) - 1;
	const o = query[1];
	if (o === "0") {
		customers[n].takeAlcohol();
+	} else if (o === "A") {
+		customers[n].accounting();
	} else {
		const m = Number(query[2]);
		if (o === "food") customers[n].takeFood(m);
		else if (o === "softdrink") customers[n].takeSoftdrink(m);
		else if (o === "alcohol") customers[n].takeAlcohol(m);
	}
}
-for (var i = 0; i < N; i++) console.log(customers[i].amount);
+console.log(Customer.numOfLeft);

Kotlin
クラスの継承
open class Customer {

	var amount: Int = 0

	open fun takeFood(price: Int) {
		amount += price
	}

	open fun takeSoftdrink(price: Int) {
		amount += price
	}

	open fun takeAlcohol(price: Int) { }
}

class Adult : Customer() {
	var alcohol: Boolean = false

	override fun takeFood(price: Int) {
		if (alcohol) {
			super.takeFood(price - 200)
		} else {
			super.takeFood(price)
		}
	}

	override fun takeAlcohol(price: Int) {
		alcohol = true
		amount += price
	}
}

fun main() {
	val (N, K) = readln().split(' ').map { it.toInt() }
	val customers = mutableListOf<Customer>()
	repeat (N) {
		val a = readln().toInt()
		customers.add(if (a < 20) Customer() else Adult())
	}
	repeat (K) {
		val query = readln().split(' ')
		val n = query[0].toInt() - 1
		val o = query[1]
		val m = query[2].toInt()
		if ("food".equals(o)) {
			customers[n].takeFood(m)
		} else if ("softdrink".equals(o)) {
			customers[n].takeSoftdrink(m)
		} else if ("alcohol".equals(o)) {
			customers[n].takeAlcohol(m)
		}
	}
	for (customer in customers) println(customer.amount)
}
デフォルト引数
open class Customer {

	var amount: Int = 0

	open fun takeFood(price: Int) {
		amount += price
	}

	open fun takeSoftdrink(price: Int) {
		amount += price
	}

	open fun takeAlcohol(price: Int) { }

+	fun takeAlcohol() {
+		takeAlcohol(500)
+	}
}

class Adult : Customer() {
	var alcohol: Boolean = false

	override fun takeFood(price: Int) {
		if (alcohol) {
			super.takeFood(price - 200)
		} else {
			super.takeFood(price)
		}
	}

	override fun takeAlcohol(price: Int) {
		alcohol = true
		amount += price
	}
}

fun main() {
	val (N, K) = readln().split(' ').map { it.toInt() }
	val customers = mutableListOf<Customer>()
	repeat (N) {
		val a = readln().toInt()
		customers.add(if (a < 20) Customer() else Adult())
	}
	repeat (K) {
		val query = readln().split(' ')
		val n = query[0].toInt() - 1
		val o = query[1]
+		if ("0".equals(o)) {
+			customers[n].takeAlcohol()
+		} else {
			val m = query[2].toInt()
			if ("food".equals(o)) {
				customers[n].takeFood(m)
			} else if ("softdrink".equals(o)) {
				customers[n].takeSoftdrink(m)
			} else if ("alcohol".equals(o)) {
				customers[n].takeAlcohol(m)
			}
+		}
	}
	for (customer in customers) println(customer.amount)
}
静的メンバ
open class Customer {
+	companion object {
+		var numOfLeft: Int = 0
+	}

	var amount: Int = 0

	open fun takeFood(price: Int) {
		amount += price
	}

	open fun takeSoftdrink(price: Int) {
		amount += price
	}

	open fun takeAlcohol(price: Int) { }

	fun takeAlcohol() {
		takeAlcohol(500)
	}

+	fun accounting() {
+		println(amount)
+		numOfLeft++
+	}
}

class Adult : Customer() {
	var alcohol: Boolean = false

	override fun takeFood(price: Int) {
		if (alcohol) {
			super.takeFood(price - 200)
		} else {
			super.takeFood(price)
		}
	}

	override fun takeAlcohol(price: Int) {
		alcohol = true
		amount += price
	}
}

fun main() {
	val (N, K) = readln().split(' ').map { it.toInt() }
	val customers = mutableListOf<Customer>()
	repeat (N) {
		val a = readln().toInt()
		customers.add(if (a < 20) Customer() else Adult())
	}
	repeat (K) {
		val query = readln().split(' ')
		val n = query[0].toInt() - 1
		val o = query[1]
		if ("0".equals(o)) {
			customers[n].takeAlcohol()
+		} else if ("A".equals(o)) {
+			customers[n].accounting()
		} else {
			val m = query[2].toInt()
			if ("food".equals(o)) {
				customers[n].takeFood(m)
			} else if ("softdrink".equals(o)) {
				customers[n].takeSoftdrink(m)
			} else if ("alcohol".equals(o)) {
				customers[n].takeAlcohol(m)
			}
		}
	}
-	for (customer in customers) println(customer.amount)
+	println(Customer.numOfLeft)
}

PHP
クラスの継承
<?php
	class Customer {
		public int $amount;
	
		public function __construct() {
			$this->amount = 0;
		}
	
		public function take_food(int $price): void {
			$this->amount += $price;
		}
	
		public function take_softdrink(int $price): void {
			$this->amount += $price;
		}
	
		public function take_alcohol(int $price): void { }
	}
	
	class Adult extends Customer {
		private bool $alcohol;
	
		public function __construct() {
			parent::__construct();
			$this->alcohol = false;
		}
	
		public function take_food(int $price): void {
			if ($this->alcohol) {
				parent::take_food($price - 200);
			} else {
				parent::take_food($price);
			}
		}
	
		public function take_alcohol(int $price): void {
			$this->alcohol = true;
			$this->amount += $price;
		}
	}

	[$N, $k] = array_map("intval", explode(' ', fgets(STDIN)));
	$customers = [];
	for ($i = 0; $i < $N; $i++) $customers[] = intval(fgets(STDIN)) < 20 ? new Customer() : new Adult();
	while ($k--) {
		$query = explode(' ', trim(fgets(STDIN)));
		$n = intval($query[0]) - 1;
		$o = $query[1];
		$m = intval($query[2]);
		if ($o === "food") $customers[$n]->take_food($m);
		else if ($o === "softdrink") $customers[$n]->take_softdrink($m);
		else if ($o === "alcohol") $customers[$n]->take_alcohol($m);
	}
	foreach ($customers as $customer) echo $customer->amount, PHP_EOL;
?>
デフォルト引数
<?php
	class Customer {
		public int $amount;
	
		public function __construct() {
			$this->amount = 0;
		}
	
		public function take_food(int $price): void {
			$this->amount += $price;
		}
	
		public function take_softdrink(int $price): void {
			$this->amount += $price;
		}
	
		public function take_alcohol(int $price): void { }
	
+		public function take_beer(): void {
+			$this->take_alcohol(500);
+		}
	}
	
	class Adult extends Customer {
		private bool $alcohol;
	
		public function __construct() {
			parent::__construct();
			$this->alcohol = false;
		}
	
		public function take_food(int $price): void {
			if ($this->alcohol) {
				parent::take_food($price - 200);
			} else {
				parent::take_food($price);
			}
		}
	
		public function take_alcohol(int $price): void {
			$this->alcohol = true;
			$this->amount += $price;
		}
	}

	[$N, $k] = array_map("intval", explode(' ', fgets(STDIN)));
	$customers = [];
	for ($i = 0; $i < $N; $i++) $customers[] = intval(fgets(STDIN)) < 20 ? new Customer() : new Adult();
	while ($k--) {
		$query = explode(' ', trim(fgets(STDIN)));
		$n = intval($query[0]) - 1;
		$o = $query[1];
+		if ($o === "0") {
+			$customers[$n]->take_beer();
+		} else {
			$m = intval($query[2]);
			if ($o === "food") $customers[$n]->take_food($m);
			else if ($o === "softdrink") $customers[$n]->take_softdrink($m);
			else if ($o === "alcohol") $customers[$n]->take_alcohol($m);
		}
+	}
	foreach ($customers as $customer) echo $customer->amount, PHP_EOL;
?>
静的メンバ
<?php
	class Customer {
+		public static int $num_of_left = 0;
		public int $amount;
	
		public function __construct() {
			$this->amount = 0;
		}
	
		public function take_food(int $price): void {
			$this->amount += $price;
		}
	
		public function take_softdrink(int $price): void {
			$this->amount += $price;
		}
	
		public function take_alcohol(int $price): void { }
	
		public function take_beer(): void {
			$this->take_alcohol(500);
		}
	
+		public function accounting(): void {
+			echo $this->amount, PHP_EOL;
+			self::$num_of_left++;
+		}
	}
	
	class Adult extends Customer {
		private bool $alcohol;
	
		public function __construct() {
			parent::__construct();
			$this->alcohol = false;
		}
	
		public function take_food(int $price): void {
			if ($this->alcohol) {
				parent::take_food($price - 200);
			} else {
				parent::take_food($price);
			}
		}
	
		public function take_alcohol(int $price): void {
			$this->alcohol = true;
			$this->amount += $price;
		}
	}

	[$N, $k] = array_map("intval", explode(' ', fgets(STDIN)));
	$customers = [];
	for ($i = 0; $i < $N; $i++) $customers[] = intval(fgets(STDIN)) < 20 ? new Customer() : new Adult();
	while ($k--) {
		$query = explode(' ', trim(fgets(STDIN)));
		$n = intval($query[0]) - 1;
		$o = $query[1];
		if ($o === "0") {
			$customers[$n]->take_beer();
+		} else if ($o === "A") {
+			$customers[$n]->accounting();
		} else {
			$m = intval($query[2]);
			if ($o === "food") $customers[$n]->take_food($m);
			else if ($o === "softdrink") $customers[$n]->take_softdrink($m);
			else if ($o === "alcohol") $customers[$n]->take_alcohol($m);
		}
	}
-	foreach ($customers as $customer) echo $customer->amount, PHP_EOL;
+	echo Customer::$num_of_left, PHP_EOL;
?>

Perl
クラスの継承
package Customer;

sub new {
	my $class = shift;
	my $self = {
		amount => 0,
	};
	bless $self, $class;
	return $self;
}

sub take_food {
	my ($self, $price) = @_;
	$self->{amount} += $price;
}

sub take_softdrink {
	my ($self, $price) = @_;
	$self->{amount} += $price;
}

sub take_alcohol {
	my ($self, $price) = @_;
}

package Adult;
our @ISA = qw(Customer); 

sub new {
	my $class = shift;
	my $self = $class->SUPER::new();
	$self->{alcohol} = 0;
	bless $self, $class;
	return $self;
}

sub take_food {
	my ($self, $price) = @_;
	if ($self->{alcohol}) {
		$self->SUPER::take_food($price - 200);
	} else {
		$self->SUPER::take_food($price);
	}
}

sub take_alcohol {
	my ($self, $price) = @_;
	$self->{alcohol} = 1;
	$self->{amount} += $price;
}

my ($N, $K) = map { int($_) } split ' ', <STDIN>;
my @customers;
for (1..$N) {
	push @customers, int(<STDIN>) < 20 ? Customer->new() : Adult->new();
}
for (1..$K) {
	my @query = split ' ', <STDIN>;
	my $n = int($query[0]) - 1;
	chomp(my $o = $query[1]); 
	my $m = int($query[2]);
	if ($o eq "food") {
		$customers[$n]->take_food($m);
	} elsif ($o eq "softdrink") {
		$customers[$n]->take_softdrink($m);
	} elsif ($o eq "alcohol") {
		$customers[$n]->take_alcohol($m);
	}
}
foreach $customer (@customers) {
	print $customer->{amount}, $/;
}
デフォルト引数
package Customer;

sub new {
	my $class = shift;
	my $self = {
		amount => 0,
	};
	bless $self, $class;
	return $self;
}

sub take_food {
	my ($self, $price) = @_;
	$self->{amount} += $price;
}

sub take_softdrink {
	my ($self, $price) = @_;
	$self->{amount} += $price;
}

sub take_alcohol {
	my ($self, $price) = @_;
+	if (!defined $price) {
+		$self->take_alcohol(500);
+	}
}

package Adult;
our @ISA = qw(Customer); 

sub new {
	my $class = shift;
	my $self = $class->SUPER::new();
	$self->{alcohol} = 0;
	bless $self, $class;
	return $self;
}

sub take_food {
	my ($self, $price) = @_;
	if ($self->{alcohol}) {
		$self->SUPER::take_food($price - 200);
	} else {
		$self->SUPER::take_food($price);
	}
}

sub take_alcohol {
	my ($self, $price) = @_;
+	if (defined $price) {
		$self->{alcohol} = 1;
		$self->{amount} += $price;
+	} else {
+		$self->take_alcohol(500);
+	}
}

my ($N, $K) = map { int($_) } split ' ', <STDIN>;
my @customers;
for (1..$N) {
	push @customers, int(<STDIN>) < 20 ? Customer->new() : Adult->new();
}
for (1..$K) {
	my @query = split ' ', <STDIN>;
	my $n = int($query[0]) - 1;
	chomp(my $o = $query[1]); 
	if ($o eq "0") {
		$customers[$n]->take_alcohol();
	} elsif ($o eq "A") {
		$customers[$n]->accounting();
	} else {
		my $m = int($query[2]);
		if ($o eq "food") {
			$customers[$n]->take_food($m);
		} elsif ($o eq "softdrink") {
			$customers[$n]->take_softdrink($m);
		} elsif ($o eq "alcohol") {
			$customers[$n]->take_alcohol($m);
		}
	}
}
foreach $customer (@customers) {
	print $customer->{amount}, $/;
}
静的メンバ
package Customer;
+our $num_of_left = 0;

sub new {
	my $class = shift;
	my $self = {
		amount => 0,
	};
	bless $self, $class;
	return $self;
}

sub take_food {
	my ($self, $price) = @_;
	$self->{amount} += $price;
}

sub take_softdrink {
	my ($self, $price) = @_;
	$self->{amount} += $price;
}

sub take_alcohol {
	my ($self, $price) = @_;
	if (!defined $price) {
		$self->take_alcohol(500);
	}
}

+sub accounting {
+	my ($self) = @_;
+	print $self->{amount}, $/;
+	$num_of_left++;
+}

package Adult;
our @ISA = qw(Customer); 

sub new {
	my $class = shift;
	my $self = $class->SUPER::new();
	$self->{alcohol} = 0;
	bless $self, $class;
	return $self;
}

sub take_food {
	my ($self, $price) = @_;
	if ($self->{alcohol}) {
		$self->SUPER::take_food($price - 200);
	} else {
		$self->SUPER::take_food($price);
	}
}

sub take_alcohol {
	my ($self, $price) = @_;
	if (defined $price) {
		$self->{alcohol} = 1;
		$self->{amount} += $price;
	} else {
		$self->take_alcohol(500);
	}
}

my ($N, $K) = map { int($_) } split ' ', <STDIN>;
my @customers;
for (1..$N) {
	push @customers, int(<STDIN>) < 20 ? Customer->new() : Adult->new();
}
for (1..$K) {
	my @query = split ' ', <STDIN>;
	my $n = int($query[0]) - 1;
	chomp(my $o = $query[1]); 
	if ($o eq "0") {
		$customers[$n]->take_alcohol();
	} elsif ($o eq "A") {
		$customers[$n]->accounting();
	} else {
		my $m = int($query[2]);
		if ($o eq "food") {
			$customers[$n]->take_food($m);
		} elsif ($o eq "softdrink") {
			$customers[$n]->take_softdrink($m);
		} elsif ($o eq "alcohol") {
			$customers[$n]->take_alcohol($m);
		}
	}
}
-foreach $customer (@customers) {
-	print $customer->{amount}, $/;
-}
+print $Customer::num_of_left, $/;

Python3
クラスの継承
class Customer:
	def __init__(self):
		self.amount = 0

	def take_food(self, price):
		self.amount += price

	def take_softdrink(self, price):
		self.amount += price

	def take_alcohol(self, price):
		pass


class Adult(Customer):
	def __init__(self):
		super().__init__()
		self.alcohol = False

	def take_food(self, price):
		if self.alcohol:
			super().take_food(price - 200)
		else:
			super().take_food(price)

	def take_alcohol(self, price):
		self.amount += price
		self.alcohol = True


N, K = map(int, input().split())
customers = [Customer() if int(input()) < 20 else Adult() for _ in range(N)]
for _ in range(K):
	query = input().split()
	n = int(query[0]) - 1
	o = query[1]
	m = int(query[2])
	if o == "food":
		customers[n].take_food(m)
	elif o == "softdrink":
		customers[n].take_softdrink(m)
	elif o == "alcohol":
		customers[n].take_alcohol(m)

for i in range(N):
	print(customers[i].amount)
デフォルト引数
class Customer:
	def __init__(self):
		self.amount = 0

	def take_food(self, price):
		self.amount += price

	def take_softdrink(self, price):
		self.amount += price

-	def take_alcohol(self, price):
+	def take_alcohol(self, price=500):
		pass


class Adult(Customer):
	def __init__(self):
		super().__init__()
		self.alcohol = False

	def take_food(self, price):
		if self.alcohol:
			super().take_food(price - 200)
		else:
			super().take_food(price)

-	def take_alcohol(self, price):
+	def take_alcohol(self, price=500):
		self.amount += price
		self.alcohol = True


N, K = map(int, input().split())
customers = [Customer() if int(input()) < 20 else Adult() for _ in range(N)]
for _ in range(K):
	query = input().split()
	n = int(query[0]) - 1
	o = query[1]
+	if o == "0":
+		customers[n].take_alcohol()
+	else:
		m = int(query[2])
		if o == "food":
			customers[n].take_food(m)
		elif o == "softdrink":
			customers[n].take_softdrink(m)
		elif o == "alcohol":
			customers[n].take_alcohol(m)

for i in range(N):
	print(customers[i].amount)
静的メンバ
class Customer:
+	num_of_left = 0

	def __init__(self):
		self.amount = 0

	def take_food(self, price):
		self.amount += price

	def take_softdrink(self, price):
		self.amount += price

	def take_alcohol(self, price=500):
		pass

+	def accounting(self):
+		print(self.amount)
+		Customer.num_of_left += 1


class Adult(Customer):
	def __init__(self):
		super().__init__()
		self.alcohol = False

	def take_food(self, price):
		if self.alcohol:
			super().take_food(price - 200)
		else:
			super().take_food(price)

	def take_alcohol(self, price=500):
		self.amount += price
		self.alcohol = True


N, K = map(int, input().split())
customers = [Customer() if int(input()) < 20 else Adult() for _ in range(N)]
for _ in range(K):
	query = input().split()
	n = int(query[0]) - 1
	o = query[1]
	if o == "0":
		customers[n].take_alcohol()
+	elif o == "A":
+		customers[n].accounting()
	else:
		m = int(query[2])
		if o == "food":
			customers[n].take_food(m)
		elif o == "softdrink":
			customers[n].take_softdrink(m)
		elif o == "alcohol":
			customers[n].take_alcohol(m)

-for i in range(N):
-	print(customers[i].amount)
+print(Customer.num_of_left)

Ruby
クラスの継承
class Customer
	attr_accessor :amount

	def initialize
		@amount = 0
	end

	def take_food(price)
		@amount += price
	end

	def take_softdrink(price)
		@amount += price
	end

	def take_alcohol(price)
		
	end
end

class Adult < Customer
	def initialize
		super()
		@alcohol = false
	end

	def take_food(x)
		if @alcohol
			super(x - 200)
		else
			super(x)
		end
	end

	def take_alcohol(price)
		@alcohol = true
		@amount += price
	end
end

N, K = gets.split.map(&:to_i)
customers = []
N.times do
	customers << (gets.to_i < 20 ? Customer.new : Adult.new)
end
K.times do
	query = gets.split
	n = query[0].to_i - 1
	o = query[1]
	m = query[2].to_i
	if o == "food"
		customers[n].take_food(m)
	elsif o == "softdrink"
		customers[n].take_softdrink(m)
	elsif o == "alcohol"
		customers[n].take_alcohol(m)
	end
end
customers.each do |customer|
	p customer.amount
end
デフォルト引数
class Customer
	attr_accessor :amount

	def initialize
		@amount = 0
	end

	def take_food(price)
		@amount += price
	end

	def take_softdrink(price)
		@amount += price
	end

-	def take_alcohol(price)
+	def take_alcohol(price = 500)
		
	end
end

class Adult < Customer
	def initialize
		super()
		@alcohol = false
	end

	def take_food(x)
		if @alcohol
			super(x - 200)
		else
			super(x)
		end
	end

-	def take_alcohol(price)
+	def take_alcohol(price = 500)
		@alcohol = true
		@amount += price
	end
end

N, K = gets.split.map(&:to_i)
customers = []
N.times do
	customers << (gets.to_i < 20 ? Customer.new : Adult.new)
end
K.times do
	query = gets.split
	n = query[0].to_i - 1
	o = query[1]
+	if o == "0"
+		customers[n].take_alcohol
+	else
		m = query[2].to_i
		if o == "food"
			customers[n].take_food(m)
		elsif o == "softdrink"
			customers[n].take_softdrink(m)
		elsif o == "alcohol"
			customers[n].take_alcohol(m)
		end
+	end
end
customers.each do |customer|
	p customer.amount
end
静的メンバ
class Customer
+	@@num_of_left = 0
	attr_accessor :amount

	def initialize
		@amount = 0
	end

	def take_food(price)
		@amount += price
	end

	def take_softdrink(price)
		@amount += price
	end

	def take_alcohol(price = 500)
		
	end

+	def accounting
+		p @amount
+		@@num_of_left += 1
+	end
+
+	def self.num_of_left
+		@@num_of_left
+	end
end

class Adult < Customer
	def initialize
		super()
		@alcohol = false
	end

	def take_food(x)
		if @alcohol
			super(x - 200)
		else
			super(x)
		end
	end

	def take_alcohol(price = 500)
		@alcohol = true
		@amount += price
	end
end

N, K = gets.split.map(&:to_i)
customers = []
N.times do
	customers << (gets.to_i < 20 ? Customer.new : Adult.new)
end
K.times do
	query = gets.split
	n = query[0].to_i - 1
	o = query[1]
	if o == "0"
		customers[n].take_alcohol
+	elsif o == "A"
+		customers[n].accounting
	else
		m = query[2].to_i
		if o == "food"
			customers[n].take_food(m)
		elsif o == "softdrink"
			customers[n].take_softdrink(m)
		elsif o == "alcohol"
			customers[n].take_alcohol(m)
		end
	end
end
-customers.each do |customer|
-	p customer.amount
-end
+p Customer.num_of_left

Scala
クラスの継承
import scala.io.StdIn._
import scala.collection.mutable.ArrayBuffer

class Customer {
	var amount: Int = 0

	def takeFood(price: Int): Unit = {
		amount += price
	}

	def takeSoftdrink(price: Int): Unit = {
		amount += price
	}

	def takeAlcohol(price: Int): Unit = { }
}

class Adult extends Customer {
	var alcohol: Boolean = false

	override def takeFood(price: Int): Unit = {
		if (alcohol) super.takeFood(price - 200)
		else super.takeFood(price)
	}

	override def takeAlcohol(price: Int): Unit = {
		alcohol = true
		amount += price
	}
}

object Main extends App {
	val nk = readLine().split(' ').map { _.toInt }
	val N = nk(0)
	val K = nk(1)
	val customers: ArrayBuffer[Customer] = ArrayBuffer()
	for (_ <- 0 until N) customers += (if (readInt() < 20) new Customer else new Adult)
	for (_ <- 0 until K) {
		val query = readLine().split(' ')
		val n = query(0).toInt - 1
		val o = query(1)
		val m = query(2).toInt
		if ("food".equals(o)) customers(n).takeFood(m)
		else if ("softdrink".equals(o)) customers(n).takeSoftdrink(m)
		else if ("alcohol".equals(o)) customers(n).takeAlcohol(m)
	}
	for (customer <- customers) println(customer.amount)
}
デフォルト引数
import scala.io.StdIn._
import scala.collection.mutable.ArrayBuffer

class Customer {
	var amount: Int = 0

	def takeFood(price: Int): Unit = {
		amount += price
	}

	def takeSoftdrink(price: Int): Unit = {
		amount += price
	}

	def takeAlcohol(price: Int): Unit = { }

+	def takeAlcohol(): Unit = {
+		takeAlcohol(500)
+	}
}

class Adult extends Customer {
	var alcohol: Boolean = false

	override def takeFood(price: Int): Unit = {
		if (alcohol) super.takeFood(price - 200)
		else super.takeFood(price)
	}

	override def takeAlcohol(price: Int): Unit = {
		alcohol = true
		amount += price
	}
}

object Main extends App {
	val nk = readLine().split(' ').map { _.toInt }
	val N = nk(0)
	val K = nk(1)
	val customers: ArrayBuffer[Customer] = ArrayBuffer()
	for (_ <- 0 until N) customers += (if (readInt() < 20) new Customer else new Adult)
	for (_ <- 0 until K) {
		val query = readLine().split(' ')
		val n = query(0).toInt - 1
		val o = query(1)
+		if ("0".equals(o)) {
+			customers(n).takeAlcohol()
+		} else {
			val m = query(2).toInt
			if ("food".equals(o)) customers(n).takeFood(m)
			else if ("softdrink".equals(o)) customers(n).takeSoftdrink(m)
			else if ("alcohol".equals(o)) customers(n).takeAlcohol(m)
+		}
	}
	for (customer <- customers) println(customer.amount)
}
静的メンバ
import scala.io.StdIn._
import scala.collection.mutable.ArrayBuffer

+object Customer {
+	var numOfLeft: Int = 0
+}

class Customer {
	var amount: Int = 0

	def takeFood(price: Int): Unit = {
		amount += price
	}

	def takeSoftdrink(price: Int): Unit = {
		amount += price
	}

	def takeAlcohol(price: Int): Unit = { }

	def takeAlcohol(): Unit = {
		takeAlcohol(500)
	}

+	def accounting(): Unit = {
+		println(amount)
+		Customer.numOfLeft += 1
+	}
}

class Adult extends Customer {
	var alcohol: Boolean = false

	override def takeFood(price: Int): Unit = {
		if (alcohol) super.takeFood(price - 200)
		else super.takeFood(price)
	}

	override def takeAlcohol(price: Int): Unit = {
		alcohol = true
		amount += price
	}
}

object Main extends App {
	val nk = readLine().split(' ').map { _.toInt }
	val N = nk(0)
	val K = nk(1)
	val customers: ArrayBuffer[Customer] = ArrayBuffer()
	for (_ <- 0 until N) customers += (if (readInt() < 20) new Customer else new Adult)
	for (_ <- 0 until K) {
		val query = readLine().split(' ')
		val n = query(0).toInt - 1
		val o = query(1)
		if ("0".equals(o)) {
			customers(n).takeAlcohol()
+		} else if ("A".equals(o)) {
+			customers(n).accounting()
		} else {
			val m = query(2).toInt
			if ("food".equals(o)) customers(n).takeFood(m)
			else if ("softdrink".equals(o)) customers(n).takeSoftdrink(m)
			else if ("alcohol".equals(o)) customers(n).takeAlcohol(m)
		}
	}
-	for (customer <- customers) println(customer.amount)
+	println(Customer.numOfLeft)
}

Swift
クラスの継承
class Customer {
	var amount: Int

	init() {
		self.amount = 0
	}

	func takeFood(_ price: Int) {
		amount += price
	}

	func takeSoftdrink(_ price: Int) {
		amount += price
	}

	func takeAlcohol(_ price: Int) { }
}

class Adult: Customer {
	var alcohol: Bool

	override init() {
		self.alcohol = false
		super.init()
	}

	override func takeFood(_ price: Int) {
		if alcohol {
			super.takeFood(price - 200)
		} else {
			super.takeFood(price)
		}
	}

	override func takeAlcohol(_ price: Int) {
		alcohol = true
		amount += price
	}
}

let nk = readLine()!.split(separator: " ").compactMap { Int($0) }
let (N, K) = (nk[0], nk[1])
var customers: [Customer] = []
for _ in 0..<N {
	customers.append(Int(readLine()!)! < 20 ? Customer() : Adult())
}
for _ in 0..<K {
	let query = readLine()!.split(separator: " ")
	let n = Int(query[0])! - 1
	let o = query[1]
	let m = Int(query[2])!
	if o == "food" {
		customers[n].takeFood(m)
	} else if o == "softdrink" {
		customers[n].takeSoftdrink(m)
	} else if o == "alcohol" {
		customers[n].takeAlcohol(m)
	}
}
for customer in customers {
	print(customer.amount)
}
デフォルト引数
class Customer {
	var amount: Int

	init() {
		self.amount = 0
	}

	func takeFood(_ price: Int) {
		amount += price
	}

	func takeSoftdrink(_ price: Int) {
		amount += price
	}

	func takeAlcohol(_ price: Int) { }

+	func takeAlcohol() {
+		takeAlcohol(500)
+	}
}

class Adult: Customer {
	var alcohol: Bool

	override init() {
		self.alcohol = false
		super.init()
	}

	override func takeFood(_ price: Int) {
		if alcohol {
			super.takeFood(price - 200)
		} else {
			super.takeFood(price)
		}
	}

	override func takeAlcohol(_ price: Int) {
		alcohol = true
		amount += price
	}
}

let nk = readLine()!.split(separator: " ").compactMap { Int($0) }
let (N, K) = (nk[0], nk[1])
var customers: [Customer] = []
for _ in 0..<N {
	customers.append(Int(readLine()!)! < 20 ? Customer() : Adult())
}
for _ in 0..<K {
	let query = readLine()!.split(separator: " ")
	let n = Int(query[0])! - 1
	let o = query[1]
+	if o == "0" {
+		customers[n].takeAlcohol()
+	} else {
		let m = Int(query[2])!
		if o == "food" {
			customers[n].takeFood(m)
		} else if o == "softdrink" {
			customers[n].takeSoftdrink(m)
		} else if o == "alcohol" {
			customers[n].takeAlcohol(m)
		}
+	}
}
for customer in customers {
	print(customer.amount)
}
静的メンバ
class Customer {
+	static var numOfLeft = 0
	var amount: Int

	init() {
		self.amount = 0
	}

	func takeFood(_ price: Int) {
		amount += price
	}

	func takeSoftdrink(_ price: Int) {
		amount += price
	}

	func takeAlcohol(_ price: Int) { }

	func takeAlcohol() {
		takeAlcohol(500)
	}

+	func accounting() {
+		print(amount)
+		Customer.numOfLeft += 1
+	}
}

class Adult: Customer {
	var alcohol: Bool

	override init() {
		self.alcohol = false
		super.init()
	}

	override func takeFood(_ price: Int) {
		if alcohol {
			super.takeFood(price - 200)
		} else {
			super.takeFood(price)
		}
	}

	override func takeAlcohol(_ price: Int) {
		alcohol = true
		amount += price
	}
}

let nk = readLine()!.split(separator: " ").compactMap { Int($0) }
let (N, K) = (nk[0], nk[1])
var customers: [Customer] = []
for _ in 0..<N {
	customers.append(Int(readLine()!)! < 20 ? Customer() : Adult())
}
for _ in 0..<K {
	let query = readLine()!.split(separator: " ")
	let n = Int(query[0])! - 1
	let o = query[1]
	if o == "0" {
		customers[n].takeAlcohol()
+	} else if o == "A" {
+		customers[n].accounting()
	} else {
		let m = Int(query[2])!
		if o == "food" {
			customers[n].takeFood(m)
		} else if o == "softdrink" {
			customers[n].takeSoftdrink(m)
		} else if o == "alcohol" {
			customers[n].takeAlcohol(m)
		}
	}
}
-for customer in customers {
-	print(customer.amount)
-}
+print(Customer.numOfLeft)
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?