//C# 7.3, .NET Framework 4.7.2
namespace
{
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
private static Random r = new Random();
public static void Main()
{
var Xi = RandomArray(10, () => r.Next(-10, 10));
Xi.Println();
var notZeroCount = Xi.Count(item => item != 0);
var replaced = Xi.Replace(item => item != 0, 1);
notZeroCount.Println();
replaced.Println();
}
private static T[] RandomArray<T>(int count, Func<T> next)
{
var temp = new T[count];
for(var i = 0; i < count; i++)
temp[i] = next();
return temp;
}
}
public static class Extensions
{
public static IEnumerable<T> Replace<T>(this IEnumerable<T> source, Func<T, bool> selector, T newValue)
{
foreach (var item in source)
{
if (selector(item))
yield return newValue;
else
yield return item;
}
}
public static int Count<T>(this IEnumerable<T> source, Func<T, bool> selector)
{
var count = 0;
foreach (var item in source)
if (selector(item)) count++;
return count;
}
public static void Println<T>(this T[] source)
{
foreach (var item in source)
Console.Write(item.ToString() + " ");
}
public static void Println<T>(this IEnumerable<T> source)
{
source.ToArray().Println();
}
public static void Println<T>(this T source)
{
Console.WriteLine(source);
}
}
}
216₈ = 142₁₀ = 8Е₁₆
D3₁₆ = 211₁₀ = 323₈
Объяснение:
Переведите число 216₈ по схеме А8 → А10 → А16
216₈ = 2 * 8² + 1 * 8¹ + 6 * 8⁰ = 2 * 64 + 1 * 8 + 6 * 1 = 128 + 8 + 6 = 142₁₀
216₈ = 142₁₀
142 / 16 = 8 + остаток 14 (Е)
8 / 16 = 0 + остаток 8
записываем остатки снизу вверх
В качестве цифр 16-ой системы счисления используются цифры от 0 до 9 и латинские буквы от A до F.
А = 10₁₀ B = 11₁₀ C = 12₁₀ D = 13₁₀ E = 14₁₀ F = 15₁₀
142₁₀ = 8Е₁₆
Переведите число D3₁₆ по схеме А16 → А10 → А8
D3₁₆ = D * 16¹ + 3 * 16⁰ = 13 * 16 + 3 * 1 = 211₁₀
В качестве цифр 16-ой системы счисления используются цифры от 0 до 9 и латинские буквы от A до F.
А = 10₁₀ B = 11₁₀ C = 12₁₀ D = 13₁₀ E = 14₁₀ F = 15₁₀
D3₁₆ = 211₁₀
211 / 8 = 26 + остаток 3
26 / 8 = 3 + остаток 2
3 / 8 = 0 + остаток 3
записываем остатки снизу вверх
211₁₀ = 323₈