“男友让我打十万个「对不起」,汉字标上多少遍。”这个问题用 R 如何实现?

WBOY
发布: 2016-06-06 16:22:13
原创
1698 人浏览过

关键是“汉字标上多少遍”。还有“对不起”必须是中文的。
男友让我打十万个「对不起」,汉字标上多少遍。如何快速实现? - 生活

回复内容:

Excel VBA一行解决,一百万以内无压力,感谢度娘:-)
="对不起,第"&IF(A1
“男友让我打十万个「对不起」,汉字标上多少遍。”这个问题用 R 如何实现? 题主问的是R,不能在Python面前丢脸

<code class="language-text">library(foreach)

map = function(Target, X,Y)
{
  for (i in 1:length(X))
  {
    Target[Target == X[i]] = Y[i] 
  }
  return(Target)
}

Digital = c((1:9)*1000, (1:9)*100, (1:9)*10, 1:9, 0)
Chs = c(paste(c("壹","贰","叁","肆","伍","陆","柒","捌","玖"), "仟", sep = ""),
        paste(c("壹","贰","叁","肆","伍","陆","柒","捌","玖"), "佰", sep = ""),
        paste(c("壹","贰","叁","肆","伍","陆","柒","捌","玖"), "拾", sep = ""),
        c("壹","贰","叁","肆","伍","陆","柒","捌","玖", "零"))

change.simple = function(x)
{
  xs = as.character(x)
  xa = foreach(i = 1:nchar(xs), .combine = "c") %do% {
    as.numeric(substr(xs,i,i))*(10^(nchar(xs) - i))}
  dup = which((xa[-1] == 0) & (xa[-length(xa)] == 0))+1
  if (length(dup)>0) xa = xa[-dup]  
  if (xa[length(xa)] == 0) xa = xa[-length(xa)]
  xa = map(xa, Digital, Chs)
  return(paste(xa, collapse=""))
}

change = function(x)
{
  if (x>=10000)
  {
    if((x %/% 10000) %% 10 == 0)
    {
      if (x %% 10000 == 0) 
        return(paste(change.simple(x %/% 10000), "万", sep = ""))
      else 
        return(paste(change.simple(x %/% 10000), "万零", change.simple(x %% 10000), sep = ""))
    }else
    {
      if (x %% 10000</code>
登录后复制
你故意少写一个数字,比如第1741条不写。如果他检查不出来,就跟他分手。
<code class="language-rconsole"><span class="go">fuck 
<span class="go">	if (x == 100000) return("十万")</span>
<span class="go">	digits = c("一", "二", "三", "四", "五", "六", "七", "八", "九")</span>
<span class="go">	units = c("", "十", "百", "千", "万")</span>
<span class="go">	x_vec = rev(as.numeric(unlist(strsplit(as.character(x), ""))))</span>
<span class="go">	ans = ""</span>
<span class="go">	reserve_0 = FALSE</span>
<span class="go">	for(it in rev(seq(length(x_vec)))) {</span>
<span class="go">		if (x_vec[it] != 0) {</span>
<span class="go">			if (reserve_0) {</span>
<span class="go">				ans = paste(ans, "零", sep = "")</span>
<span class="go">				reserve_0 = FALSE</span>
<span class="go">			}</span>
<span class="go">			ans = paste(ans, digits[x_vec[it]], units[it], sep = "")</span>
<span class="go">		} else {</span>
<span class="go">			reserve_0 = TRUE</span>
<span class="go">		}</span>
<span class="go">	}</span>
<span class="go">	if (x = 10)</span>
<span class="go">		ans = substring(ans, 2, 10)</span>
<span class="go">	return(ans)</span>
<span class="go">}</span>

<span class="go">#####################################################################</span>
<span class="go">print(paste("对不起, 第", sapply(seq(1e5), fuck), "遍", sep = ""))</span>
</span></code>
登录后复制
你男友为了让你学好编程也是蛮拼的 既然难点是在数字部分的汉字化,加个函数好了
<code class="language-splus">charfunc<span class="o"><span class="kr">function</span><span class="p">(</span>x<span class="p">)</span> <span class="p">{</span>
  numb<span class="o"><span class="kt">c</span><span class="p">(</span><span class="s">'0'</span><span class="o">=</span><span class="s">'零'</span><span class="p">,</span><span class="s">'1'</span><span class="o">=</span><span class="s">'一'</span><span class="p">,</span><span class="s">'2'</span><span class="o">=</span><span class="s">'二'</span><span class="p">,</span><span class="s">'3'</span><span class="o">=</span><span class="s">'三'</span><span class="p">,</span><span class="s">'4'</span><span class="o">=</span><span class="s">'四'</span><span class="p">,</span><span class="s">'5'</span><span class="o">=</span><span class="s">'五'</span><span class="p">,</span><span class="s">'6'</span><span class="o">=</span><span class="s">'六'</span><span class="p">,</span>
          <span class="s">'7'</span><span class="o">=</span><span class="s">'七'</span><span class="p">,</span><span class="s">'8'</span><span class="o">=</span><span class="s">'八'</span><span class="p">,</span><span class="s">'9'</span><span class="o">=</span><span class="s">'九'</span><span class="p">)</span>
  <span class="kp">units</span><span class="o"><span class="kt">c</span><span class="p">(</span><span class="s">''</span><span class="p">,</span><span class="s">'十'</span><span class="p">,</span><span class="s">'百'</span><span class="p">,</span><span class="s">'千'</span><span class="p">,</span><span class="s">'万'</span><span class="p">,</span><span class="s">'十万'</span><span class="p">)</span>
  
  res1<span class="o"><span class="kp">as.character</span><span class="p">(</span>x<span class="p">)</span>
  res2<span class="o">numb<span class="p">[</span><span class="kp">unlist</span><span class="p">(</span><span class="kp">strsplit</span><span class="p">(</span>res1<span class="p">,</span><span class="s">''</span><span class="p">))]</span>
  res3<span class="o"><span class="kp">paste</span><span class="p">(</span>res2<span class="p">,</span><span class="kp">units</span><span class="p">[</span><span class="kp">length</span><span class="p">(</span>res2<span class="p">)</span><span class="o">:</span><span class="m">1</span><span class="p">],</span>sep<span class="o">=</span><span class="s">''</span><span class="p">,</span>collapse<span class="o">=</span><span class="s">''</span><span class="p">)</span>
  res4<span class="o"><span class="kp">gsub</span><span class="p">(</span><span class="s">'零\\w'</span><span class="p">,</span><span class="s">'零'</span><span class="p">,</span>res3<span class="p">)</span>
  res5<span class="o"><span class="kp">gsub</span><span class="p">(</span><span class="s">'零+'</span><span class="p">,</span><span class="s">'零'</span><span class="p">,</span>res4<span class="p">)</span>
  <span class="kp">gsub</span><span class="p">(</span><span class="s">'零$'</span><span class="p">,</span><span class="s">''</span><span class="p">,</span>res5<span class="p">)</span>
  
<span class="p">}</span>


sorry<span class="o"><span class="kt">data.frame</span><span class="p">(</span><span class="kp">paste</span><span class="p">(</span><span class="s">'对不起,第'</span><span class="p">,</span><span class="kp">apply</span><span class="p">(</span><span class="kt">matrix</span><span class="p">(</span><span class="m">1</span><span class="o">:</span><span class="m">100000</span><span class="p">),</span><span class="m">1</span><span class="p">,</span>charfunc<span class="p">),</span><span class="s">'遍'</span><span class="p">,</span>sep<span class="o">=</span><span class="s">''</span><span class="p">),</span>
                stringsAsFactors<span class="o">=</span><span class="bp">F</span><span class="p">)</span>
<span class="kp">names</span><span class="p">(</span>sorry<span class="p">)</span><span class="o"><span class="s">'sorry'</span>

<span class="kp">head</span><span class="p">(</span>sorry<span class="p">)</span>
<span class="kp">tail</span><span class="p">(</span>sorry<span class="p">)</span>
</span></span></span></span></span></span></span></span></span></span></code>
登录后复制
Ruby的。。应该还有bug。。扛不住先睡了。。
<code class="language-rb"><span class="k">class</span> <span class="nc">Fixnum</span>
  <span class="k">def</span> <span class="nf">to_chinese</span>
    <span class="n">length</span> <span class="o">=</span> <span class="nb">self</span><span class="o">.</span><span class="n">to_s</span><span class="o">.</span><span class="n">length</span>
    <span class="n">array</span> <span class="o">=</span> <span class="o">[]</span>
    <span class="n">time</span> <span class="o">=</span> <span class="n">length</span> <span class="o">%</span> <span class="mi">4</span> <span class="o">==</span> <span class="mi">0</span> <span class="o">?</span> <span class="n">length</span> <span class="o">/</span> <span class="mi">4</span> <span class="p">:</span> <span class="n">length</span> <span class="o">/</span> <span class="mi">4</span>  <span class="o">+</span> <span class="mi">1</span>
    <span class="n">chars</span> <span class="o">=</span> <span class="s1">'亿万 '</span><span class="o">[</span><span class="mi">3</span> <span class="o">-</span> <span class="n">time</span> <span class="p">,</span> <span class="mi">3</span><span class="o">]</span>
    <span class="n">time</span><span class="o">.</span><span class="n">downto</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span> <span class="k">do</span> <span class="o">|</span><span class="n">t</span><span class="o">|</span>
      <span class="n">start</span> <span class="o">=</span>  <span class="p">(</span><span class="n">t</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span> <span class="o">*</span> <span class="mi">4</span> <span class="o">+</span> <span class="n">length</span> <span class="o">%</span> <span class="mi">4</span>  
      <span class="n">cut_start</span>   <span class="o">=</span> <span class="n">start</span> <span class="o"> <span class="mi">0</span> <span class="o">?</span> <span class="mi">0</span> <span class="p">:</span> <span class="n">start</span>
      <span class="n">cut_length</span>  <span class="o">=</span> <span class="n">start</span> <span class="o"> <span class="mi">0</span> <span class="o">?</span> <span class="mi">4</span> <span class="o">+</span> <span class="n">start</span> <span class="p">:</span> <span class="mi">4</span>
      <span class="n">cut</span> <span class="o">=</span> <span class="nb">self</span><span class="o">.</span><span class="n">to_s</span><span class="o">[</span><span class="n">cut_start</span> <span class="p">,</span> <span class="n">cut_length</span><span class="o">]</span>
      <span class="k">unless</span> <span class="n">cut</span> <span class="o">==</span> <span class="s1">''</span>
        <span class="n">ch</span> <span class="o">=</span> <span class="n">cut</span><span class="o">.</span><span class="n">to_i</span><span class="o">.</span><span class="n">to_ch</span>
        <span class="n">array</span><span class="o">.</span><span class="n">push</span> <span class="s2">"</span><span class="si">#{</span><span class="n">ch</span><span class="si">}#{</span><span class="n">chars</span><span class="o">[</span><span class="n">t</span><span class="o">]</span><span class="si">}</span><span class="s2">"</span>  <span class="k">unless</span> <span class="n">ch</span> <span class="o">==</span> <span class="s1">''</span>
      <span class="k">end</span>
    <span class="k">end</span>
    <span class="n">array</span><span class="o">.</span><span class="n">reverse</span><span class="o">.</span><span class="n">join</span><span class="o">.</span><span class="n">delete</span><span class="p">(</span><span class="s1">' '</span><span class="p">)</span>
  <span class="k">end</span>

  <span class="k">def</span> <span class="nf">to_ch</span>
    <span class="n">chars</span> <span class="o">=</span> <span class="s1">'零一二三四五六七八九'</span>
    <span class="n">bits</span>  <span class="o">=</span> <span class="s1">' 十百千'</span>
    <span class="n">array</span> <span class="o">=</span> <span class="o">[]</span>
    <span class="nb">self</span><span class="o">.</span><span class="n">to_s</span><span class="o">.</span><span class="n">length</span><span class="o">.</span><span class="n">times</span> <span class="k">do</span> <span class="o">|</span><span class="n">t</span><span class="o">|</span>
      <span class="n">cnumber</span> <span class="o">=</span> <span class="n">chars</span><span class="o">[</span><span class="nb">self</span><span class="o">.</span><span class="n">to_s</span><span class="o">.</span><span class="n">reverse</span><span class="o">[</span><span class="n">t</span><span class="o">].</span><span class="n">to_i</span><span class="o">]</span>
      <span class="n">i_array</span> <span class="o">=</span> <span class="o">[</span><span class="kp">nil</span> <span class="p">,</span> <span class="s1">''</span> <span class="p">,</span> <span class="s1">'零'</span><span class="o">]</span>
      <span class="k">if</span> <span class="n">cnumber</span> <span class="o">!=</span> <span class="s1">'零'</span> 
        <span class="n">char</span> <span class="o">=</span> <span class="s2">"</span><span class="si">#{</span><span class="n">cnumber</span><span class="si">}#{</span><span class="n">bits</span><span class="o">[</span><span class="n">t</span><span class="o">]</span><span class="si">}</span><span class="s2">"</span>
      <span class="k">elsif</span> <span class="n">t</span><span class="o">-</span><span class="mi">1</span> <span class="o">>=</span> <span class="mi">0</span> <span class="o">&&</span> <span class="o">!</span><span class="n">i_array</span><span class="o">.</span><span class="n">include?</span><span class="p">(</span><span class="n">array</span><span class="o">[</span><span class="n">t</span><span class="o">-</span><span class="mi">1</span><span class="o">]</span><span class="p">)</span> <span class="o">&&</span>  <span class="o">!</span><span class="n">i_array</span><span class="o">.</span><span class="n">include?</span><span class="p">(</span><span class="o">!</span><span class="n">array</span><span class="o">[</span><span class="mi">0</span><span class="o">]</span><span class="p">)</span>
        <span class="n">char</span> <span class="o">=</span> <span class="s1">'零'</span>
      <span class="k">else</span>
        <span class="n">char</span> <span class="o">=</span> <span class="s1">''</span>
      <span class="k">end</span>
      <span class="n">array</span><span class="o">.</span><span class="n">push</span> <span class="n">char</span>
    <span class="k">end</span>
    <span class="n">array</span><span class="o">.</span><span class="n">reverse</span><span class="o">.</span><span class="n">join</span>
  <span class="k">end</span>
<span class="k">end</span>

<span class="mi">1000000</span><span class="o">.</span><span class="n">times</span> <span class="p">{</span><span class="o">|</span><span class="n">time</span><span class="o">|</span> <span class="nb">p</span> <span class="s2">"对不起,第</span><span class="si">#{</span><span class="n">time</span><span class="o">.</span><span class="n">to_chinese</span><span class="si">}</span><span class="s2">遍"</span> <span class="p">}</span>
</span></span></code>
登录后复制

你不是擅长R话题么。。。。。。

“男友让我打十万个「对不起」,汉字标上多少遍。”这个问题用 R 如何实现?
<code class="language-text">unit </code>
登录后复制
@石临源 ,你的程序有bug,我重写了个。

<code class="language-python"><span class="n">CHINESE_DIGITS</span> <span class="o">=</span> <span class="s">'零一二三四五六七八九'</span>
<span class="n">CHINESE_UNITS</span> <span class="o">=</span> <span class="p">(</span><span class="s">''</span><span class="p">,</span><span class="s">'十'</span><span class="p">,</span><span class="s">'百'</span><span class="p">,</span><span class="s">'千'</span><span class="p">,</span><span class="s">'万'</span><span class="p">)</span>

<span class="k">def</span> <span class="nf">tslt_le4</span><span class="p">(</span><span class="n">intnum</span><span class="p">):</span>
    <span class="n">lststr</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">intnum</span><span class="p">)</span><span class="o">.</span><span class="n">zfill</span><span class="p">(</span><span class="mi">4</span><span class="p">)[::</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span>
    <span class="n">units</span> <span class="o">=</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">CHINESE_UNITS</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="k">if</span> <span class="n">lststr</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">!=</span> <span class="s">'0'</span> <span class="k">else</span> <span class="s">''</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">4</span><span class="p">))</span>

    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">3</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">lststr</span><span class="p">[</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">]</span> <span class="o">==</span> <span class="s">'0'</span> <span class="ow">and</span> <span class="n">lststr</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">==</span> <span class="s">'0'</span><span class="p">:</span>
            <span class="n">lststr</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="s">''</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">lststr</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span> <span class="o">==</span> <span class="s">'0'</span><span class="p">:</span>
            <span class="n">lststr</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span> <span class="o">=</span> <span class="s">''</span>

    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">4</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">lststr</span><span class="p">[</span><span class="n">i</span><span class="p">]:</span>
            <span class="n">lststr</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">CHINESE_DIGITS</span><span class="p">[</span><span class="nb">int</span><span class="p">(</span><span class="n">lststr</span><span class="p">[</span><span class="n">i</span><span class="p">])]</span>

    <span class="n">result</span> <span class="o">=</span> <span class="s">''</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">lststr</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">+</span> <span class="n">units</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">))</span>    

    <span class="n">result</span> <span class="o">=</span> <span class="n">result</span><span class="p">[:</span><span class="o">-</span><span class="mi">3</span><span class="p">]</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s">'二'</span><span class="p">,</span> <span class="s">'两'</span><span class="p">)</span> <span class="o">+</span> <span class="n">result</span><span class="p">[</span><span class="o">-</span><span class="mi">3</span><span class="p">:]</span>

    <span class="n">result</span> <span class="o">=</span> <span class="n">result</span><span class="o">.</span><span class="n">rstrip</span><span class="p">(</span><span class="s">'零'</span><span class="p">)</span>

    <span class="k">return</span> <span class="n">result</span>

<span class="k">def</span> <span class="nf">tslt_le8</span><span class="p">(</span><span class="n">intnum</span><span class="p">):</span>
    <span class="n">leftint</span> <span class="o">=</span> <span class="n">intnum</span><span class="o">//</span><span class="mi">10</span><span class="o">**</span><span class="mi">4</span>
    <span class="n">rightint</span> <span class="o">=</span> <span class="n">intnum</span><span class="o">%</span><span class="mi">10</span><span class="o">**</span><span class="mi">4</span>

    <span class="n">left</span> <span class="o">=</span> <span class="n">tslt_le4</span><span class="p">(</span><span class="n">leftint</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">left</span><span class="p">:</span>
        <span class="n">left</span> <span class="o">+=</span> <span class="s">'万'</span>

    <span class="n">rightint</span> <span class="o">=</span> <span class="n">intnum</span><span class="o">%</span><span class="mi">10</span><span class="o">**</span><span class="mi">4</span>
    <span class="n">right</span> <span class="o">=</span> <span class="n">tslt_le4</span><span class="p">(</span><span class="n">rightint</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">leftint</span> <span class="ow">and</span> <span class="mi">0</span> <span class="o"> <span class="n">rightint</span> <span class="o"> <span class="mi">1000</span><span class="p">:</span>
        <span class="n">right</span> <span class="o">=</span> <span class="s">'零'</span> <span class="o">+</span> <span class="n">right</span>

    <span class="n">result</span> <span class="o">=</span> <span class="n">left</span> <span class="o">+</span> <span class="n">right</span>

    <span class="k">if</span> <span class="n">result</span> <span class="o">==</span> <span class="s">''</span><span class="p">:</span>
        <span class="n">result</span> <span class="o">=</span> <span class="s">'零'</span>

    <span class="k">if</span> <span class="n">result</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s">'一十'</span><span class="p">):</span>
        <span class="n">result</span> <span class="o">=</span> <span class="n">result</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span>

    <span class="k">return</span> <span class="n">result</span>


<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">"__main__"</span><span class="p">:</span>
    <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s">'sorry.txt'</span><span class="p">,</span><span class="s">'w'</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">100001</span><span class="p">):</span>
            <span class="n">sorry_str</span> <span class="o">=</span> <span class="s">'对不起 第{}遍</span><span class="se">\n</span><span class="s">'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">tslt_le8</span><span class="p">(</span><span class="n">i</span><span class="p">))</span>
            <span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">sorry_str</span><span class="p">)</span>
</span></span></code>
登录后复制
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板