用golang打印数字

前端之家收集整理的这篇文章主要介绍了用golang打印数字前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

效果:

guodeMacBook-Air:programming_in_goguo$gorun1_1.go1234
122222233333344
112344
1222222333333444444
1234
11111112222223333334


go 源码(节选)

packagemain

import(
"fmt"
"os"
)

funcmain(){
iflen(os.Args)!=2{
fmt.Printf("%snumber\n",os.Args[0])
return
}

bigdigits:=[][]string{
{"000","00","000",},{
"1","11","1","1111111",{
"999999","99","999999","9",}

forrow:=rangebigdigits[0]{
strdigit:=os.Args[1]
line:=""
fori:=rangestrdigit{
d:=strdigit[i]-'0'
ifd<0||d>9{
fmt.Printf("errornumber\n")
return
}
line+=bigdigits[d][row]+""
}
fmt.Printf("%s\n",line)
}
}


完成同同样效果的c语言源码(节选)

#include<stdio.h>
#include<stdlib.h>

intmain(intargc,char**argv){
constchar*(a[][11])={
{"000","1111111"
},"999999"
}
};

if(argc!=2){
printf("Usage:%s<number>\n",argv[0]);
return0;
}

char*p;
intd,r,row;
row=(int)(sizeof(a[0])/sizeof(a[0][0]));

for(r=0;r<row;r++){

for(p=argv[1];*p;p++){
d=*p-'0';
if(d<0||d>9){
printf("errornumber\n");
returnEXIT_FAILURE;
}
if(a[d][r]==NULL)
gotodone;

printf("%s",a[d][r]);
}
printf("\n");
}
done:
return0;
}

猜你在找的Go相关文章