计算空格以外的唯一符号

时间:2018-10-25 13:09:23

标签: c#

如何在程序逻辑中添加功能以输出除空格以外的所有符号?

@test

...

def circleRadius(b, c, d):
  temp = c[0]**2 + c[1]**2
  bc = (b[0]**2 + b[1]**2 - temp) / 2
  cd = (temp - d[0]**2 - d[1]**2) / 2
  det = (b[0] - c[0]) * (c[1] - d[1]) - (c[0] - d[0]) * (b[1] - c[1])

  if abs(det) < 1.0e-10:
    return None

  # Center of circle
  cx = (bc*(c[1] - d[1]) - cd*(b[1] - c[1])) / det
  cy = ((b[0] - c[0]) * cd - (c[0] - d[0]) * bc) / det

  radius = ((cx - b[0])**2 + (cy - b[1])**2)**.5

  return radius

3 个答案:

答案 0 :(得分:1)

在查询 时尝试使用 Linq

如果要“输出除[空格]以外的所有符号”:

  using System.Linq;

  ...

  string result = string.Concat(text.Where(c => !char.IsWhiteSpace(c)));

如果要“计算除空格以外的唯一符号”并将结果具体化为字典:

  using System.Linq;

  ...

  var dic = text
    .Where(c => !char.IsWhiteSpace(c)))
    .GroupBy(c => c)
    .ToDictionary(chunk => chunk.Key, chunk => chunk.Count());

答案 1 :(得分:0)

如果您要删除空格,则可以尝试以下操作:

string text = Console.ReadLine().ToLower().Replace(" ", "");

或者,如果您要处理制表符而不是空格:

string text = Console.ReadLine().ToLower().Replace("\t", "");

答案 2 :(得分:0)

使用此代码。

    using System;
    using System.Linq;
    using System.Collections.Generic;
    public class Program
    {
        public static void Main()
        {
             string input = Console.ReadLine();

             var chars = input.ToCharArray();

             var uniqueChars = chars.Where(i=>i!= ' ').Distinct();

             var dictionary = new Dictionary<char,int>();

             foreach(var ch in uniqueChars)
                      dictionary.Add(ch ,chars.Where(i => i == ch).Count());

             foreach(var keyValue in dictionary)
                      Console.WriteLine($"{keyValue.Key} : {keyValue.Value}");


         }
    }

它对我有用: 赛义德·波哈萨尼 s:2 :3 e:2 d:1 b:1 o:1 :1 小时:1 n:1 我:1