用R来试试小学三年级数学题

2019-06-10  本文已影响0人  木䬕

1.题目如下: mmexport1560142169243.jpg

2.R解决办法:

# 定义函数,筛选最终值 ----------------------------------------------------------------------
library(tidyverse)
find_jiou <- function(t){
  set.seed(t)
  i<-c(1,3,5,7,9)
  I <- as.character(i)
  j<-c(0,2,4,6,8)
  J<- as.character(j)
  a<- sample(i,1,replace = T) %>% as.character()
  b<- sample(j,1,replace = T)%>% as.character()
  c<- sample(j[-1],1,replace = T)%>% as.character()
  d<- sample(i,1,replace=T)%>% as.character()
  ab <- paste0(a,b,collapse = "") %>% as.numeric()
  cd <- paste0(c,d,collapse = "") %>% as.numeric()
  r1<- as.character(ab*as.numeric(d)) %>% substring(1:3,1:3)
  r2 <-as.character(ab*as.numeric(c)) %>% substring(1:2,1:2)
  rf<-as.character(ab*cd) %>% substring(1:3,1:3)
  re<- paste0(a,b,c,d,collapse = "") %>% substring(1:4,1:4)
  tr1<-(r1 %in% J) %>% all()
  tr2<-(r2 %in% J)%>% all()
  trf<-(rf %in% J)%>% all()
  tlength1 <-r1 %>% paste0(collapse = "") %>% nchar()
  tlength1 <-tlength1==3
  tlength2 <-r2 %>% paste0(collapse = "") %>% nchar()
  tlength2<-tlength2==2
  tlengthrf <-rf %>% paste0(collapse = "") %>% nchar()
  tlengthrf<-tlengthrf==3
  r<-c(tr1,tr2,trf,tlength1,tlength2,tlengthrf)
  if(all(r)) print(paste0(re,collapse = "") %>% as.numeric())
}
for (h in 1:625) {
  find_jiou(h)
}
#结果
> for (h in 1:625) {
+   find_jiou(h)
+ }
[1] 3227
[1] 3227
[1] 3227

最终找到的奇数和偶数分别为:32*27=864

上一篇下一篇

猜你喜欢

热点阅读