R4fun之文字竖排
2015-06-04 23:29
411 查看
在Crossin的编程教室中看到一则文字竖排的题目(http://chuansong.me/n/265078)
于是思量着应该如何实现,就顺手写了个R实现的demo以供借鉴
<span style="font-size:18px;">verticalP<-function(str,row){
len=nchar(str)
if(len%%row!=0){
column=len%/%row+1
}else{
column=len%/%row
}
sum=column*row
chars=c(strsplit(str,"")[[1]],rep(' ',sum-len))
mat=matrix(chars,ncol=column)
pivot=column%/%2
for(i in seq(pivot)){
temp=mat[,i]
mat[,i]=mat[,column-i+1]
mat[,column-i+1]=temp
}
result=apply(mat,1,paste,collapse='|')
cat(result,sep='\n')
}</span>经测试尚可运行良好
于是思量着应该如何实现,就顺手写了个R实现的demo以供借鉴
<span style="font-size:18px;">verticalP<-function(str,row){
len=nchar(str)
if(len%%row!=0){
column=len%/%row+1
}else{
column=len%/%row
}
sum=column*row
chars=c(strsplit(str,"")[[1]],rep(' ',sum-len))
mat=matrix(chars,ncol=column)
pivot=column%/%2
for(i in seq(pivot)){
temp=mat[,i]
mat[,i]=mat[,column-i+1]
mat[,column-i+1]=temp
}
result=apply(mat,1,paste,collapse='|')
cat(result,sep='\n')
}</span>经测试尚可运行良好
相关文章推荐
- CMake 使用方法
- 第三章 43,44题
- java第三次实验
- ThinkPHP 3.2 PHPExcel 导入导出文件 第三方类库不能用问题解决
- poj 1847 Tram 【最短路 dijkstra + floyd + spfa】
- Java for LeetCode 149 Max Points on a Line
- 变量(续)
- ActiveMQ入门实例
- Java NIO系列教程(二) Channel
- 第二章第6题
- uva 1593 代码对齐
- LeetCode Unique Paths II(dp)
- 路由表解释
- python 与 c/c++混合编程
- 通过redis的monitor命令排除故障
- 4.22
- ia32问题
- “找一”
- Arch下 OpenJDK shows intermittent performance and UI issues. We recommend using the Oracle JRE/JDK.
- 为什么外网和内网不能直接通信