好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

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

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

回复内容: Excel VBA一行解决,一百万以内无压力,感谢度娘:-)
="对不起,第"&IF(A1
题主问的是R,不能在Python面前丢脸

 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), 测试数据bine = "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  
你故意少写一个数字,比如第1741条不写。如果他检查不出来,就跟他分手。

  fuck  
 	if (x == 100000) return("十万") 
 	digits = c("一", "二", "三", "四", "五", "六", "七", "八", "九") 
 	units = c("", "十", "百", "千", "万") 
 	x_vec = rev(as.numeric(unlist(strsplit(as.character(x), "")))) 
 	ans = "" 
 	reserve_0 = FALSE 
 	for(it in rev(seq(length(x_vec)))) { 
 		if (x_vec[it] != 0) { 
 			if (reserve_0) { 
 				ans = paste(ans, "零", sep = "") 
 				reserve_0 = FALSE 
 			} 
 			ans = paste(ans, digits[x_vec[it]], units[it], sep = "") 
 		} else { 
 			reserve_0 = TRUE 
 		} 
 	} 
 	if (x  = 10) 
 		ans = substring(ans, 2, 10) 
 	return(ans) 
 } 

 ##################################################################### 
 print(paste("对不起, 第", sapply(seq(1e5), fuck), "遍", sep = "")) 
  
你男友为了让你学好编程也是蛮拼的 既然难点是在数字部分的汉字化,加个函数好了

 charfunc   function  ( x )   { 
  numb   c  (  '0'  =  '零'  ,  '1'  =  '一'  ,  '2'  =  '二'  ,  '3'  =  '三'  ,  '4'  =  '四'  ,  '5'  =  '五'  ,  '6'  =  '六'  , 
           '7'  =  '七'  ,  '8'  =  '八'  ,  '9'  =  '九'  ) 
   units    c  (  ''  ,  '十'  ,  '百'  ,  '千'  ,  '万'  ,  '十万'  ) 
  
  res1   as.character  ( x ) 
  res2  numb [  unlist  (  strsplit  ( res1 ,  ''  ))] 
  res3   paste  ( res2 ,  units  [  length  ( res2 )  :  1  ], sep =  ''  , collapse =  ''  ) 
  res4   gsub  (  '零\\w'  ,  '零'  , res3 ) 
  res5   gsub  (  '零+'  ,  '零'  , res4 ) 
   gsub  (  '零$'  ,  ''  , res5 ) 
  
 } 


sorry   data.frame  (  paste  (  '对不起,第'  ,  apply  (  matrix  (  1  :  100000  ),  1  , charfunc ),  '遍'  , sep =  ''  ), 
                stringsAsFactors =  F  ) 
 names  ( sorry )    'sorry' 

 head  ( sorry ) 
 tail  ( sorry ) 
  
Ruby的。。应该还有bug。。扛不住先睡了。。

  class   Fixnum 
   def   to_chinese 
     length   =   self  .  to_s  .  length 
     array   =   [] 
     time   =   length   %   4   ==   0   ?   length   /   4   :   length   /   4    +   1 
     chars   =   '亿万 '  [  3   -   time   ,   3  ] 
     time  .  downto  (  0  )   do   |  t  | 
       start   =    (  t   -   1  )   *   4   +   length   %   4   
       cut_start     =   start      0   ?   0   :   start 
       cut_length    =   start      0   ?   4   +   start   :   4 
       cut   =   self  .  to_s  [  cut_start   ,   cut_length  ] 
       unless   cut   ==   '' 
         ch   =   cut  .  to_i  .  to_ch 
         array  .  push   "  #{  ch  }#{  chars  [  t  ]  }  "    unless   ch   ==   '' 
       end 
     end 
     array  .  reverse  .  join  .  delete  (  ' '  ) 
   end 

   def   to_ch 
     chars   =   '零一二三四五六七八九' 
     bits    =   ' 十百千' 
     array   =   [] 
     self  .  to_s  .  length  .  times   do   |  t  | 
       cnumber   =   chars  [  self  .  to_s  .  reverse  [  t  ].  to_i  ] 
       i_array   =   [  nil   ,   ''   ,   '零'  ] 
       if   cnumber   !=   '零'  
         char   =   "  #{  cnumber  }#{  bits  [  t  ]  }  " 
       elsif   t  -  1   >=   0   &&   !  i_array  .  include?  (  array  [  t  -  1  ]  )   &&    !  i_array  .  include?  (  !  array  [  0  ]  ) 
         char   =   '零' 
       else 
         char   =   '' 
       end 
       array  .  push   char 
     end 
     array  .  reverse  .  join 
   end 
 end 

 1000000  .  times   {  |  time  |   p   "对不起,第  #{  time  .  to_chinese  }  遍"   } 
  

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

 unit   
@石临源 ,你的程序有bug,我重写了个。

  CHINESE_DIGITS   =   '零一二三四五六七八九' 
 CHINESE_UNITS   =   (  ''  ,  '十'  ,  '百'  ,  '千'  ,  '万'  ) 

 def   tslt_le4  (  intnum  ): 
     lststr   =   list  (  str  (  intnum  )  .  zfill  (  4  )[::  -  1  ]) 
     units   =   tuple  (  CHINESE_UNITS  [  i  ]   if   lststr  [  i  ]   !=   '0'   else   ''   for   i   in   range  (  4  )) 

     for   i   in   range  (  3  ): 
         if   lststr  [  i  +  1  ]   ==   '0'   and   lststr  [  i  ]   ==   '0'  : 
             lststr  [  i  ]   =   '' 
     else  : 
         if   lststr  [  3  ]   ==   '0'  : 
             lststr  [  3  ]   =   '' 

     for   i   in   range  (  4  ): 
         if   lststr  [  i  ]: 
             lststr  [  i  ]   =   CHINESE_DIGITS  [  int  (  lststr  [  i  ])] 

     result   =   ''  .  join  (  lststr  [  i  ]   +   units  [  i  ]   for   i   in   range  (  3  ,   -  1  ,   -  1  ))     

     result   =   result  [:  -  3  ]  .  replace  (  '二'  ,   '两'  )   +   result  [  -  3  :] 

     result   =   result  .  rstrip  (  '零'  ) 

     return   result 

 def   tslt_le8  (  intnum  ): 
     leftint   =   intnum  //  10  **  4 
     rightint   =   intnum  %  10  **  4 

     left   =   tslt_le4  (  leftint  ) 
     if   left  : 
         left   +=   '万' 

     rightint   =   intnum  %  10  **  4 
     right   =   tslt_le4  (  rightint  ) 
     if   leftint   and   0      rightint      1000  : 
         right   =   '零'   +   right 

     result   =   left   +   right 

     if   result   ==   ''  : 
         result   =   '零' 

     if   result  .  startswith  (  '一十'  ): 
         result   =   result  [  1  :] 

     return   result 


 if   __name__   ==   "__main__"  : 
     with   open  (  'sorry.txt'  ,  'w'  )   as   f  : 
         for   i   in   range  (  1  ,   100001  ): 
             sorry_str   =   '对不起 第{}遍  \n  '  .  format  (  tslt_le8  (  i  )) 
             f  .  write  (  sorry_str  ) 
  

查看更多关于“男友让我打十万个[对不起],汉字标上多少遍。”这个问题用R如何实现?的详细内容...

  阅读:50次