作者shadowjohn (转角遇到爱)
看板Programming
标题Re: [问题] 1-9位数不重复印出来 (D)
时间Wed Dec 7 20:53:05 2016
※ 引述《mikemagic88 (Mikemagic88)》之铭言:
: 使用者输入1 印1-9
: 使用者输入2 印1-98 (11, 22, 33等重复的不印)
: 使用者输入3 印1-987 (121, 988, 667等有重复的不印)
import std.stdio;
import std.string;
import std.conv;
import std.math;
import std.regex;
void main()
{
write("\nInput something punk> ");
auto input = strip(stdin.readln());
int level = to!int(input);
level = (level>=10)?10: (level<=0)?0:level;
if(level==0) return;
else write(1);
for(int i=2,max_i=pow(10,level);i<max_i;i++)
{
string si = to!string(i);
int[string] aa;
for(int j=0;j<=9;j++)
{
aa[to!string(j)]=0;
}
int is_found=0;
for(int j=0,max_j=to!int(countchars(si,"0123456789"));j<max_j;j++)
{
if( aa[ to!string(si[j]) ] !=0)
{
is_found=1;
break;
}
else
{
aa[ to!string(si[j]) ]=1;
}
}
if(is_found == 0)
{
write(", ");
write(i);
}
}
writeln("");
}
(dmd-2.072.1)[root@3wa d]# rdmd mycode.d
Input something punk> 2
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 23,
24, 25, 26, 27, 28, 29, 30, 31, 32, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43,
45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 56, 57, 58, 59, 60, 61, 62, 63, 64,
65, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 78, 79, 80, 81, 82, 83, 84, 85,
86, 87, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98
(dmd-2.072.1)[root@3wa d]#
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 36.233.81.220
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Programming/M.1481115189.A.151.html
※ 编辑: shadowjohn (36.233.81.220), 12/07/2016 20:54:26
1F:→ shadowjohn: :( D 的re match试半天(.).*\1 140.134.48.253 12/08 09:08
2F:→ shadowjohn: 但都只有生效一次,希望有好心人帮忙 140.134.48.253 12/08 09:09
3F:→ shadowjohn: matchAll跟 ,"g" 也都试了~ :~ 140.134.48.253 12/08 09:09
4F:→ Neisseria: 有些冷门语言的 regex engine 怪怪的 125.227.36.83 12/08 10:49
5F:→ Neisseria: 像我也发现 Rust 内建的 regex 不好用 125.227.36.83 12/08 10:49
6F:→ Neisseria: 後来就改用 pcre,才比较顺手 125.227.36.83 12/08 10:50
7F:→ Neisseria: 如果有问题,换个 regex engine 比较快 125.227.36.83 12/08 10:54
8F:→ shadowjohn: 原来如此,晚点再试一下~谢啦^_^~~ 140.134.48.253 12/08 11:45