Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何轻松地将x作为字符串输出为n的幂_Java - Fatal编程技术网

Java 如何轻松地将x作为字符串输出为n的幂

Java 如何轻松地将x作为字符串输出为n的幂,java,Java,我需要输出大的数字,例如847288609443as3⁴⁵。我所有的号码的格式都是3^n或3^n-1。我使用的提示来自。目前,我的代码如下所示: public static void main(String[] args) throws IOException { Map<String,Character> map = new HashMap<>(); map.put("-", '\u207b'); map.put("5", '\u

我需要输出大的数字,例如
847288609443
as
3⁴⁵。我所有的号码的格式都是
3^n
3^n-1
。我使用的提示来自。目前,我的代码如下所示:

public static void main(String[] args) throws IOException {        
   Map<String,Character> map = new HashMap<>();
   map.put("-", '\u207b');       map.put("5", '\u2075');
   map.put("0", '\u2070');       map.put("6", '\u2076');
   map.put("1", '\u00b9');       map.put("7", '\u2077');
   map.put("2", '\u00b2');       map.put("8", '\u2078');
   map.put("3", '\u00b3');       map.put("9", '\u2079');
   map.put("4", '\u2074');       

   for(int i = 1; i< 50; i++){          
       String [] s = String.valueOf(i).split("");
       StringBuilder sb = new StringBuilder();
       sb.append("3");
       Stream.of(s).forEach(e->sb.append(map.get(e)));
       System.out.println(sb.toString());
   }
}
publicstaticvoidmain(字符串[]args)抛出IOException{
Map Map=newhashmap();
map.put(“-”,“\u207b”);map.put(“5”,“\u2075”);
map.put('0','\u2070');map.put('6','\u2076');
map.put('1','\u00b9');map.put('7','\u2077');
map.put('2','\u00b2');map.put('8','\u2078');
map.put('3','\u00b3');map.put('9','\u2079');
地图放置(“4”,“\u2074”);
对于(inti=1;i<50;i++){
String[]s=String.valueOf(i).split(“”);
StringBuilder sb=新的StringBuilder();
某人附加(“3”);
forEach的流(e->sb.append(map.get(e));
System.out.println(sb.toString());
}
}
unicode值来自此wiki页面:()


有没有其他方法来进行这种转换,而不是拆分我的指数的字符串值并添加unicode字符?

我将假设您的代码符合您的要求

将每个数字转换为unicode上标并附加到StringBuilder的想法很好,但您的实现效率不高——特别是在拆分中。如果这让你感到困扰,那么你可以这样做,速度会快得多:

static final String SUPDIGITS = "\u2070\u00b9\u00b2\u00b3\u2074\u2075\u2076\u2077\u2078\u2079";

public static void main(String[] args) throws IOException {        

   StringBuilder sb = new StringBuilder();
   for(int i = 1; i< 50; i++) {
       sb.setLength(0);

       //append digits in reverse order
       int v = i;
       for (;v>0;v/=10) {
           sb.append(SUPDIGITS.charAt(v%10));
       }
       //and then the 3
       sb.append("3");
       //and then reverse it
       sb.reverse();
       System.out.append(sb).println();
   }
}
static final String SUPDIGITS=“\u2070\u00b9\u00b2\u00b3\u2074\u2075\u2076\u2077\u2078\u2079”;
公共静态void main(字符串[]args)引发IOException{
StringBuilder sb=新的StringBuilder();
对于(int i=1;i<50;i++){
sb.设定长度(0);
//按相反顺序追加数字
int v=i;
对于(;v>0;v/=10){
sb.追加(SUPDIGITS.charAt(v%10));
}
//然后是3
某人附加(“3”);
//然后把它倒过来
使某人倒转;
System.out.append(sb.println();
}
}

这里的主要区别在于,这个版本的内存分配要少得多。

我将假设您的代码实现了您想要的功能

将每个数字转换为unicode上标并附加到StringBuilder的想法很好,但您的实现效率不高——特别是在拆分中。如果这让你感到困扰,那么你可以这样做,速度会快得多:

static final String SUPDIGITS = "\u2070\u00b9\u00b2\u00b3\u2074\u2075\u2076\u2077\u2078\u2079";

public static void main(String[] args) throws IOException {        

   StringBuilder sb = new StringBuilder();
   for(int i = 1; i< 50; i++) {
       sb.setLength(0);

       //append digits in reverse order
       int v = i;
       for (;v>0;v/=10) {
           sb.append(SUPDIGITS.charAt(v%10));
       }
       //and then the 3
       sb.append("3");
       //and then reverse it
       sb.reverse();
       System.out.append(sb).println();
   }
}
static final String SUPDIGITS=“\u2070\u00b9\u00b2\u00b3\u2074\u2075\u2076\u2077\u2078\u2079”;
公共静态void main(字符串[]args)引发IOException{
StringBuilder sb=新的StringBuilder();
对于(int i=1;i<50;i++){
sb.设定长度(0);
//按相反顺序追加数字
int v=i;
对于(;v>0;v/=10){
sb.追加(SUPDIGITS.charAt(v%10));
}
//然后是3
某人附加(“3”);
//然后把它倒过来
使某人倒转;
System.out.append(sb.println();
}
}

这里的主要区别在于,此版本的内存分配要少得多。

您可以这样做:

private static char[] DIGITS = {
        '\u2070', '\u00b9', '\u00b2', '\u00b3', '\u2074',
        '\u2075', '\u2076', '\u2077', '\u2078', '\u2079'};

public static String toSuperscript(int i) {
    boolean neg = i < 0;
    if (neg) {
        i = -i;
    }
    char[] chars = new char[15];
    int k = chars.length;
    do {
        chars[--k] = DIGITS[i%10];
        i /= 10;
    } while (i > 0);
    if (neg) {
        chars[--k] = '\u207b'; // minus sign
    }
    return new String(chars, k, chars.length-k);
}

public static void main(String[] args) throws IOException {        
   for(int i = 1; i< 50; i++){          
       System.out.println("3" + toSuperscript(i));
   }
}
private static char[]位={
'\u2070'、'\u00b9'、'\u00b2'、'\u00b3'、'\u2074',
'\u2075'、'\u2076'、'\u2077'、'\u2078'、'\u2079'};
公共静态字符串toSuperscript(int i){
布尔负=i<0;
如果(负){
i=-i;
}
char[]chars=新字符[15];
int k=字符长度;
做{
字符[--k]=数字[i%10];
i/=10;
}而(i>0);
如果(负){
字符[--k]='\u207b';//减号
}
返回新字符串(chars,k,chars.length-k);
}
公共静态void main(字符串[]args)引发IOException{
对于(inti=1;i<50;i++){
System.out.println(“3”+toSuperscript(i));
}
}

您可以这样做:

private static char[] DIGITS = {
        '\u2070', '\u00b9', '\u00b2', '\u00b3', '\u2074',
        '\u2075', '\u2076', '\u2077', '\u2078', '\u2079'};

public static String toSuperscript(int i) {
    boolean neg = i < 0;
    if (neg) {
        i = -i;
    }
    char[] chars = new char[15];
    int k = chars.length;
    do {
        chars[--k] = DIGITS[i%10];
        i /= 10;
    } while (i > 0);
    if (neg) {
        chars[--k] = '\u207b'; // minus sign
    }
    return new String(chars, k, chars.length-k);
}

public static void main(String[] args) throws IOException {        
   for(int i = 1; i< 50; i++){          
       System.out.println("3" + toSuperscript(i));
   }
}
private static char[]位={
'\u2070'、'\u00b9'、'\u00b2'、'\u00b3'、'\u2074',
'\u2075'、'\u2076'、'\u2077'、'\u2078'、'\u2079'};
公共静态字符串toSuperscript(int i){
布尔负=i<0;
如果(负){
i=-i;
}
char[]chars=新字符[15];
int k=字符长度;
做{
字符[--k]=数字[i%10];
i/=10;
}而(i>0);
如果(负){
字符[--k]='\u207b';//减号
}
返回新字符串(chars,k,chars.length-k);
}
公共静态void main(字符串[]args)引发IOException{
对于(inti=1;i<50;i++){
System.out.println(“3”+toSuperscript(i));
}
}

您可以将其实现为代码点的映射。以下是非负数的实现:

private static final int[] superCodepoint = new int[] {
'\u2070', '\u00b9', '\u00b2', '\u00b3', '\u2074', '\u2075', '\u2076', '\u2077', '\u2078', '\u2079'
};
public static String ToSuperscript(String s) {
    return s.codePoints()
        .map(c -> Character.isDigit(c) ? superCodepoint[Character.digit(c, 10)] : c)
        .collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)
        .toString();
}

您可以将其实现为代码点的映射。以下是非负数的实现:

private static final int[] superCodepoint = new int[] {
'\u2070', '\u00b9', '\u00b2', '\u00b3', '\u2074', '\u2075', '\u2076', '\u2077', '\u2078', '\u2079'
};
public static String ToSuperscript(String s) {
    return s.codePoints()
        .map(c -> Character.isDigit(c) ? superCodepoint[Character.digit(c, 10)] : c)
        .collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)
        .toString();
}

我已经使用了该问题的答案。我的问题是,这是否真的是做这件事的唯一方法,或者如果有人有不同的方法。可能的重复我已经使用了这个问题的答案。我的问题是,这是否真的是做这件事的唯一方法,或者是否有人有不同的方法。非常感谢。特别是关于时间和内存分配的提示。非常感谢。特别是关于时间和内存分配的提示。