「男友讓我打十萬個「對不起」,漢字標上多少遍。」這個問題用 R 如何實現?
關鍵是「漢字標上多少遍」。還有「對不起」必須是中文的。
男友讓我打十萬個「對不起」,漢字標上多少遍。如何快速實現? - 生活
Excel VBA一行解決,一百萬以內無壓力,感謝度娘:-)
="對不起,第"IF(A1&<0,"負","")TEXT(ROUNDDOWN(ABS(ROUND(A1,2)),0),"[dbnum2]g/通用格式")"遍"你故意少寫一個數字,比如第1741條不寫。如果他檢查不出來,就跟他分手。
fuck &<- function(x) {
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 &<= 19 x &>= 10)
ans = substring(ans, 2, 10)
return(ans)
}
#####################################################################
print(paste("對不起, 第", sapply(seq(1e5), fuck), "遍", sep = ""))
題主問的是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), .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&<1000 x %% 10000 != 0)
return(paste(change.simple(x %/% 10000), "萬零", change.simple(x %% 10000), sep = ""))
else
return(paste(change.simple(x %/% 10000), "萬", change.simple(x %% 10000), sep = ""))
}
}else
return(change.simple(x))
}
cat(paste(foreach(i = 1:100000, .combine = c) %do% {paste("對不起第", change(i), "遍")}, collapse= "
"))
一些測試
對不起第 叄萬零叄 遍
對不起第 壹拾萬零叄 遍對不起第 壹拾萬 遍對不起第 壹萬 遍對不起第 壹萬貳仟零叄 遍
對不起第 壹萬貳仟叄佰叄拾叄 遍對不起第 壹萬貳仟 遍對不起第 壹拾貳萬零叄佰零貳 遍對不起第 壹仟玖佰玖拾壹萬零伍佰零叄 遍
Jaron Zhou
樓上paste()時候加個 sep=""吧,否則 編程痕迹太明顯了。
你不是擅長R話題么。。。。。。
既然難點是在數字部分的漢字化,加個函數好了
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)
結果是這樣
--------------------------------------------------------------------------------------------------------------------------------------------上面的代碼已經滿足了題主的需要,不過只適用最大十萬,超過十萬條,就需要將charfunc函數修改了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,""))]
core&<-function(y,unit="") {
y1&<-paste(y,units[length(y):1],sep="")
y2&<-gsub("零\w+","零",y1)
y3&<-gsub("零+","零",paste(y2,collapse=""))
paste(gsub("零$","",y3),unit,sep="")
}
if (length(res2)&<=4) {
res&<-core(res2)
}
if (length(res2)&>=5 length(res2)&<9) {
unit1&<-res2[1:(length(res2)-4)]
unit2&<-res2[(length(res2)-3):length(res2)]
res&<-paste(core(unit1,"萬"),core(unit2),sep="",collapse="")
}
res
}
這樣,小於一億都可以了,比如這樣
如果需要超過一億的話,嗯,接著修改函數吧程序員改變世界系列
@石臨源 ,你的程序有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 = "對不起 第{}遍
".format(tslt_le8(i))
f.write(sorry_str)
CHINESE_DIGITS = "零一二三四五六七八九"
CHINESE_UNITS = ("","十","百","千","萬", "億")
TRANS = str.maketrans("一二三四五六七八九十百千", "壹貳叄肆伍陸柒捌玖拾佰仟")
def digit2Chinesexx(num):
"""Return the small Chinese Charactors string of a num."""
num = int(num)
res = tslt_le16(num)
return res
def digit2ChineseDX(num):
"""Return the big Chinese Charactors string of a num."""
return digit2Chinesexx(num).translate(TRANS)
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 += "萬"
right = tslt_le4(rightint)
if leftint and 0 &< rightint &< 10**3:
right = "零" + right
result = left + right
return result
def tslt_le16(intnum):
leftint = intnum // 10**8
rightint = intnum % 10**8
left = tslt_le8(leftint)
if left:
left += "億"
right = tslt_le8(rightint)
if leftint and 0 &< rightint &< 10**7:
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, 10**17):
sorry_str = "對不起 第{}遍
".format(digit2ChineseDX(i))
f.write(sorry_str)
你男友為了讓你學好編程也是蠻拼的
這個問題有Python標籤,我就上一個我寫的Python版本的程序實現吧。
# -*- coding: utf-8 -*-
CHINESE_DIGITS = "零一二三四五六七八九"
CHINESE_UNITS = ["","","十","百","千","萬","十萬"]
def translate_number_to_tuple(number_str):
number_n = len(number_str)
result = []
for i in range(number_n):
result.append((int(number_str[i]),number_n-i))
return result
def translate_number_to_chinese_simple(result_tuple):
i,j = result_tuple
if i!=0:
return CHINESE_DIGITS[i]+CHINESE_UNITS[j]
else:
return CHINESE_DIGITS[i]
def translate_number_to_chinese(num_str):
chinese_num = translate_number_to_tuple(num_str)
result = ""
for i in chinese_num:
result += translate_number_to_chinese_simple(i)
return result
def translate_number(number):
if number&>9999:
number = str(number)
chinese_num1 = number[:-4]
chinese_num2 = number[-4:]
result_1 = translate_number_to_chinese(chinese_num1)
result_1 = result_1.replace("零","")
if result_1[0] == "一" and len(result_1) &> 1:
result_1 = result_1[1:]
result_2 = translate_number_to_chinese(chinese_num2)
result_2 = result_2.replace("零零零","零",2)
result_2 = result_2.replace("零零","零",3)
if result_2[0] == "零":
result_2 = result_2[1:]
result = result_1+"萬"+result_2
else:
number = str(number)
result = translate_number_to_chinese(number)
result = result.replace("零零零","零",2)
result = result.replace("零零","零",3)
if result[-1] == "零":
result = result[:-1]
if len(result) &< 4 and result[0] == "一":
result = result[1:]
return result
if __name__ == "__main__":
with open("sorry.txt","w") as f:
for i in range(100000):
sorry_str = "對不起 第"+translate_number(i+1)+"遍
"
f.write(sorry_str)
題主你暴露了,老師留的作業要自己寫。就算他是你男票也不例外~
爪機黨,懶得貼代碼,詳見附圖。
for (i in 1:10) {
if (i == 1) {
cat("
","I have told you once TO BREAK UP WITH YOUR BOYFRIEND!!!")
} else if (i == 2) {
cat("
","I have told you twice TO BREAK UP WITH YOUR BOYFRIEND!!!")
} else {
cat("
","I have told you for", i, "times TO BREAK UP WITH YOUR BOYFRIEND!!!")
}
}
_(:з」∠)_
現在花式虐狗都有細分領域了…
一隻只code dog還熱火朝天掄著袖子擼R擼得不亦樂乎。
# c←("程序猿找不到女朋友不是沒有道理的")女票 我看到你邀我回答這個問題 首先想到的不是怎麼實現 而是在揣摩你的真正意圖
把男朋友割掉