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?

cscの作法 その533

Last updated at Posted at 2024-11-13

概要

cscの作法、調べてみた。
練習問題やってみた。

練習問題

2から9までの2つのリストの上位集合を判定せよ。

写真

image.png

方針

  • checkbox使う。
  • hashset使う。

サンプルコード

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.Collections.Generic;

namespace App
{
	public partial class Form1: Form {
		CheckBox aka0;
		CheckBox aka1;
		CheckBox aka2;
		CheckBox aka3;
		CheckBox aka4;
		CheckBox aka5;
		CheckBox aka6;
		CheckBox aka7;
		CheckBox siro0;
		CheckBox siro1;
		CheckBox siro2;
		CheckBox siro3;
		CheckBox siro4;
		CheckBox siro5;
		CheckBox siro6;
		CheckBox siro7;
		public Form1() {
			Text = "siroがakaの上位集合かどうか";
			ClientSize = new Size(800, 500);
			Label label0 = new Label();
			label0.Location = new Point(50, 50);
			label0.Text = "aka:";
			aka0 = new CheckBox();
			aka0.Location = new Point(110, 80);
			aka0.Text = "2";
			aka1 = new CheckBox();
			aka1.Location = new Point(190, 50);
			aka1.Text = "3";
			aka2 = new CheckBox();
			aka2.Location = new Point(270, 80);
			aka2.Text = "4";
			aka3 = new CheckBox();
			aka3.Location = new Point(350, 50);
			aka3.Text = "5";
			aka4 = new CheckBox();
			aka4.Location = new Point(430, 80);
			aka4.Text = "6";
			aka5 = new CheckBox();
			aka5.Location = new Point(510, 50);
			aka5.Text = "7";
			aka6 = new CheckBox();
			aka6.Location = new Point(590, 80);
			aka6.Text = "8";
			aka7 = new CheckBox();
			aka7.Location = new Point(670, 50);
			aka7.Text = "9";

			Label label2 = new Label();
			label2.Location = new Point(50, 120);
			label2.Text = "";
			label2.Size = new System.Drawing.Size(700, 1);
			label2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;

			Label label1 = new Label();
			label1.Location = new Point(50, 150);
			label1.Text = "siro:";
			siro0 = new CheckBox();
			siro0.Location = new Point(110, 180);
			siro0.Text = "2";
			siro1 = new CheckBox();
			siro1.Location = new Point(190, 150);
			siro1.Text = "3";
			siro2 = new CheckBox();
			siro2.Location = new Point(270, 180);
			siro2.Text = "4";
			siro3 = new CheckBox();
			siro3.Location = new Point(350, 150);
			siro3.Text = "5";
			siro4 = new CheckBox();
			siro4.Location = new Point(430, 180);
			siro4.Text = "6";
			siro5 = new CheckBox();
			siro5.Location = new Point(510, 150);
			siro5.Text = "7";
			siro6 = new CheckBox();
			siro6.Location = new Point(590, 180);
			siro6.Text = "8";
			siro7 = new CheckBox();
			siro7.Location = new Point(670, 150);
			siro7.Text = "9";

			Button btn1 = new Button();
			btn1.Location = new Point(300, 300);
			btn1.Text = "test";
			btn1.Click += btn1_Click;
			Controls.AddRange(new Control[] {
				label0,
				aka0,
				aka1,
				aka2,
				aka3,
				aka4,
				aka5,
				aka6,
				aka7,
				label2,
				label1,
				siro0,
				siro1,
				siro2,
				siro3,
				siro4,
				siro5,
				siro6,
				siro7,
				btn1
			});
		}
		void btn1_Click(object sender, System.EventArgs e) {
			var hst1 = new HashSet<int>() { };
			var hst2 = new HashSet<int>() { };
			if (aka0.Checked) hst1.Add(2);
			if (aka1.Checked) hst1.Add(3);
			if (aka2.Checked) hst1.Add(4);
			if (aka3.Checked) hst1.Add(5);
			if (aka4.Checked) hst1.Add(6);
			if (aka5.Checked) hst1.Add(7);
			if (aka6.Checked) hst1.Add(8);
			if (aka7.Checked) hst1.Add(9);
			if (siro0.Checked) hst2.Add(2);
			if (siro1.Checked) hst2.Add(3);
			if (siro2.Checked) hst2.Add(4);
			if (siro3.Checked) hst2.Add(5);
			if (siro4.Checked) hst2.Add(6);
			if (siro5.Checked) hst2.Add(7);
			if (siro6.Checked) hst2.Add(8);
			if (siro7.Checked) hst2.Add(9);
			MessageBox.Show("siroがakaの上位集合かどうかの判定結果:" + hst2.IsSupersetOf(hst1).ToString());
		}
		[STAThread]
		public static void Main() {
			Application.EnableVisualStyles();
			Application.SetCompatibleTextRenderingDefault(false);
			Application.Run(new Form1());
		}
	}
}







以上

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?