C#
 Computer >> コンピューター >  >> プログラミング >> C#

2つの辞書をマージするC#プログラム


2つの辞書を設定する-

Dictionary < string, int > dict1 = new Dictionary < string, int > ();
dict1.Add("laptop", 1);
dict1.Add("desktop", 2);
Dictionary < string, int > dict2 = new Dictionary < string, int > ();
dict2.Add("desktop", 3);
dict2.Add("tablet", 4);
dict2.Add("mobile", 5);

次に、HashSetメソッドとUnionWith()メソッドを使用して、2つの辞書をマージします-

HashSet < string > hSet = new HashSet < string > (dict1.Keys);
hSet.UnionWith(dict2.Keys);

これが完全なコードです-

using System;
using System.Collections.Generic;
class Program {
   static void Main() {
      Dictionary < string, int > dict1 = new Dictionary < string, int > ();
      dict1.Add("laptop", 1);
      dict1.Add("desktop", 2);

      Dictionary < string, int > dict2 = new Dictionary < string, int > ();
      dict2.Add("desktop", 3);
      dict2.Add("tablet", 4);
      dict2.Add("mobile", 5);

      HashSet < string > hSet = new HashSet < string > (dict1.Keys);
      hSet.UnionWith(dict2.Keys);
      Console.WriteLine("Merged Dictionary...");

      foreach(string val in hSet) {
         Console.WriteLine(val);
      }
   }
}

  1. C++で2つのバイナリ文字列を追加するプログラム

    2進数の文字列が2つある場合、それら2つの2進数文字列を加算して得られた結果を見つけ、その結果を2進数文字列として返す必要があります。 2進数は、0または1のいずれかで表される数値です。2つの2進数を加算する際には、2進数の加算規則があります。 0+0 → 0 0+1 → 1 1+0 → 1 1+1 → 0, carry 1 入力 str1 = {“11”}, str2 = {“1”} 出力 “100” 入力 str1 = {“110”},

  2. 2つの数値を追加するPythonプログラム

    この記事では、特定の問題ステートメントを解決するための解決策とアプローチについて学習します。 問題の説明 2つの大きな数が与えられ、それらを追加して出力を表示する必要があります。 ブルートフォースアプローチでは、オペランド間に「+」演算子を使用するか、2つの数値を反復可能に格納して、Python標準ライブラリで使用可能な組み込みのsum関数を使用できます。 このアプローチでは、計算が10進数で直接行われるため、時間計算量が増加します。 次に、10進数のビットを処理する別のアプローチについて説明します。 ここでは、合計とキャリーを計算する加算器の概念を使用します。 それでは、実装を見