Advertisement
vovanhoangtuan

Tính Tổng

Jun 7th, 2020
861
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.73 KB | None | 0 0
  1. using System;
  2.  
  3. namespace TinhTong
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             int[] a = nhapDuLieu();
  10.             task(a);
  11.         }
  12.  
  13.         static void task(int[] a)
  14.         {
  15.             int s = 0;
  16.             for (int i = 0; i < a.Length; i++)
  17.             {
  18.                 s += a[i];
  19.             }
  20.  
  21.             bool[,] res = new bool[2, s + 1];
  22.          
  23.             res[0, s] = true;
  24.             res[0, a[0]] = true;
  25.  
  26.             for (int i = 1; i < a.Length; i++)
  27.             {
  28.                 for (int j = 0; j < res.GetLength(1) - 1; j++)
  29.                 {
  30.                     if (res[0, j] == true) res[1,j + a[i]] = true;
  31.                 }
  32.                 res[0, a[i]] = true;
  33.                 for (int j = 0; j < res.GetLength(1); j++)
  34.                 {
  35.                     if (res[1, j] == true) res[0, j] = true;
  36.                 }
  37.             }
  38.  
  39.             int total = 0;
  40.             for (int i = 0; i < res.GetLength(1); i++)
  41.             {
  42.                 if (res[0, i] == true) total++;
  43.             }
  44.  
  45.             Console.WriteLine(total);
  46.             for (int i = 0; i < res.GetLength(1); i++)
  47.             {
  48.                 if (res[0, i] == true) Console.Write(i + " ");
  49.             }
  50.             Console.WriteLine();
  51.         }
  52.  
  53.         static int[] nhapDuLieu()
  54.         {
  55.             int n = int.Parse(Console.ReadLine());
  56.             int[] input = new int[n];
  57.  
  58.             string temp = Console.ReadLine();
  59.             string[] splitData = temp.Split(' ');
  60.  
  61.             for (int i = 0; i < splitData.Length; i++)
  62.             {
  63.                 input[i] = int.Parse(splitData[i]);
  64.             }
  65.             return input;
  66.         }
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement