为此 challenge在 PCG.SE 上,需要编写一个函数/程序来返回/输出前 N 个素数。
但是,程序中不允许使用带有质数代码点的字符。
其中两个字符是 5
和 e
.任一字母的代码点都包含 5
.正因为如此,\u{codepoint}
对这两个字母不起作用。
使用转义,我可以从代码中删除所有主要字符,除了 e
在 return
.
我可以用 System.out
或 FileDescriptor.out
但都包含字母 e
.
有没有办法不使用 e
返回或输出或 5
?
作为引用,这里是我转义字符的代码:
int[]b(int b\u0029{
int c,d,f[]\u003d{}\u003b
for(f\u003dj\u0061v\u0061.util.Arr\u0061ys.copy\u004ff(f,b\u0029\u003bb-->0\u003b\u0029
for(d\u003d0,c\u003d2\u003bf[b]<1\u003bf[b]\u003dd<1?c:f[b],d\u003d0,c\u002b\u002b\u0029
for(int h:f\u0029
d\u003dh>0&&c\u002fh*h\u003d\u003dc?1:d\u003b
return f;
}
没有转义字符:
int[]b(int b){
int c,d,f[]={};
for(f=java.util.Arrays.copyOf(f,b);b-->0;)
for(d=0,c=2;f[b]<1;f[b]=d<1?c:f[b],d=0,c++)
for(int h:f)
d=h>0&&c/h*h==c?1:d;
return f;
}
最佳答案
我找到了一种无需使用 System.out
即可输出质数的方法, FileDescriptor.out
, 或 return
.我的方法不使用“主要”字符。
这非常棘手,因为 e
在 Java 包名、类名和方法名中无处不在。另外,S
必须禁止,因为它的编号 83
Unicode 和 83
是素数。它的 Unicode 转义 \u0053
包含 5
其 Unicode 字符 53
也是素数。以下是在这些规则下我们不能使用 Unicode 转义的所有字符:
%
(37 是质数,\u0025
有一个 5
)5
(53 是质数,\u0035
有一个 5
)S
(83 是质数,\u0053
有一个 5
)Y
(89 是质数,\u0059
有一个 5
)e
(101 是质数,\u0065
有一个 5
)这消除了:
toString
, fromString
, parseInt
, valueOf
, 和 values
. java.beans
, java.net
, java.lang.reflect
包裹Class.forName
new
的使用运算符(operator)。 Logger
的日志框架包含 e
的类. 以下是我们可以使用 Unicode 转义的字符:
)
(41 是质数,\u0029
是允许的)+
(43 是质数,\u002b
是允许的)/
(47 是质数,\u002f
是允许的);
(59 是质数,\u003b
是允许的)=
(61 是质数,\u003d
是允许的)C
(67 是质数,\u0043
是允许的)G
(71 是质数,\u0047
是允许的)I
(73 是质数,\u0049
是允许的)O
(79 是质数,\u004f
是允许的)a
(97 是质数,\u0061
是允许的)k
(107 是质数,\u006b
是允许的)q
(113 是质数,\u0071
是允许的)尝试生成消息包含所需输出的异常是满足 Java 要求的唯一方法。我们不能使用受检异常,因为几乎所有我能找到的受检异常都使用
e
以他们的名义,以及直到 Throwable
的每个父类(super class)中.我需要使用 throws
包含异常名称的子句包含 e
.我不能extend
任何类,因为 extends
包含 e
s。这留下了未经检查的异常。最有可能的用途是
IllegalArgumentException
它不是由这段代码直接创建的,而是通过调用内置的东西来创建的,它会抛出一个 IllegalArgumentException
.Enum
s 会有所帮助,除了 valueOf
包含 e
. javax.crypto.Mac.getInstance
有一个 e
;无论如何它都会抛出一个已检查的异常。 javax.naming.ldap.Rdn.unescapeValue("\\b" + stringFormOfOutput)
通过异常输出列表,但该方法具有 e
. java.util.UUID.fromString
通过异常输出列表,但该方法有 S
. This method抛出
DataBindingException
which is unchecked .javax.xml.bind.JAXB.unmarshal(stringFormOfOutput, Long.class)
通过 DataBindingException
输出列表,没有 e
. 现在我们要做的就是转换
int[]
到 String
.通过将数组连接到 ""
进行字符串转换不起作用,因为 arrays, as objects in Java, don't override toString()
. Arrays.toString
因 S
出局.我们可以使用
Arrays.asList
获得 List
出数据。但是 Arrays.asList(f)
gives a List<int[]>
, not a List<Integer>
,使问题复杂化。更改 f
的类型至 Integer[]
不起作用,它有 e
.更改 f
的类型至 Long[]
作品。将数组转换为
String
如下。Long c,d,f[]...
""+Arrays.asList(f)
代替
return
声明,使方法void
,并致电 unmarshal
. Long
的使用意味着需要进行一些其他调整,例如使用 long
文字和 fill
用零代替默认数组 null
s。void b(int b){
Long c,d,f[]={};
for(f=java.util.Arrays.copyOf(f,b),Arrays.fill(f,0L);b-->0;)
for(d=0L,c=2L;f[b]<1;f[b]=d<1?c:f[b],d=0L,c++)
for(long h:f)
d=h>0&&c/h*h==c?1:d;
javax.xml.bind.JAXB.unmarshal(""+Arrays.asList(f),Long.class);
}
转义“主要”字符:
void b(int b\u0029{
Lon\u0067 c,d,f[]\u003d{}\u003b
for(f\u003dj\u0061v\u0061.util.Arr\u0061ys.copy\u004ff(f,b\u0029,Arr\u0061ys.fill(f,0L\u0029\u003bb-->0\u003b\u0029
for(d\u003d0L,c\u003d2L\u003bf[b]<1\u003bf[b]\u003dd<1?c:f[b],d\u003d0L,c\u002b\u002b\u0029
for(lon\u0067 h:f\u0029
d\u003dh>0&&c\u002fh*h\u003d\u003dc?1:d\u003b
j\u0061v\u0061x.x\u006dl.bind.JAXB.un\u006d\u0061rsh\u0061l(""\u002bArr\u0061ys.\u0061sList(f\u0029, Lon\u0067.cl\u0061ss\u0029\u003b
}
这是一段丑陋的代码,它可能不会赢得任何代码高尔夫比赛,但这是我能想到的在 Java 中满足要求的唯一方法。
调用这个
b
方法与 10
作为参数产生以下输出,[29, 23, 19, 17, 13, 11, 7, 5, 3, 2]
是前 10 个素数:Exception in thread "main" javax.xml.bind.DataBindingException: javax.xml.bind.UnmarshalException
- with linked exception:
[java.io.FileNotFoundException: C:\dev\src\misc\[29, 23, 19, 17, 13, 11, 7, 5, 3, 2] (The system cannot find the file specified)]
at javax.xml.bind.JAXB.unmarshal(JAXB.java:208)
at Main.b(Main.java:34)
at Main.main(Main.java:12)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:90)
Caused by: javax.xml.bind.UnmarshalException
- with linked exception:
[java.io.FileNotFoundException: C:\dev\src\misc\[29, 23, 19, 17, 13, 11, 7, 5, 3, 2] (The system cannot find the file specified)]
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:206)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:181)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:235)
at javax.xml.bind.JAXB.unmarshal(JAXB.java:205)
... 7 more
Caused by: java.io.FileNotFoundException: C:\dev\src\misc\[29, 23, 19, 17, 13, 11, 7, 5, 3, 2] (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:97)
at sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:90)
at sun.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:188)
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(XMLEntityManager.java:609)
at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(XMLVersionDetector.java:189)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:799)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:764)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:123)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1210)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:568)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:203)
... 10 more
但是
public static void main(String[] args)
呢? ?有一个看似不可避免的S
.制作 b
static
并致电 b
使用静态初始化器,消除 main
和它的 String[]
要求。st\u0061tic
{
b(10\u0029\u003b
}
st\u0061tic void b(int b\u0029{
// ...
输出现在包含
ExceptionInInitializerError
,但输出的其余部分完好无损; DataBindingException
被锁住了。Exception in thread "main" java.lang.ExceptionInInitializerError
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:190)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:87)
Caused by: javax.xml.bind.DataBindingException: javax.xml.bind.UnmarshalException
- with linked exception:
[java.io.FileNotFoundException: C:\dev\src\misc\[29, 23, 19, 17, 13, 11, 7, 5, 3, 2] (The system cannot find the file specified)]
at javax.xml.bind.JAXB.unmarshal(JAXB.java:208)
...
关于java - 不使用字母 e 或数字 5 在 Java 中输出或返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28635275/