Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.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 如何根据每个字符串的长度获得其幂?_Java_Algorithm - Fatal编程技术网

Java 如何根据每个字符串的长度获得其幂?

Java 如何根据每个字符串的长度获得其幂?,java,algorithm,Java,Algorithm,我有一个长度为15的字符串:“10101000000010”。我想买独家的,也就是^ int s=a[3]^a[5]^a[7]^a[9]^a[11]^a[13]^a[15] //here ^ skips 1 index all the way up until the end of string which is 7. int t = a[3] ^ a[ 6] ^ a[ 7] ^ a[10] ^ a[11] ^ a[14] ^ a[15]; //here ^ skips 2 index and

我有一个长度为15的字符串:
“10101000000010”
。我想买独家的,也就是^

int s=a[3]^a[5]^a[7]^a[9]^a[11]^a[13]^a[15]

//here ^ skips 1 index all the way up until the end of string which is 7.

int t = a[3] ^ a[ 6] ^ a[ 7] ^ a[10] ^ a[11] ^ a[14] ^ a[15];
//here ^ skips 2 index and lets 2 go, and so on...
我如何设计一个算法来实现这一点?在取每个索引的exclusive or之后,我只需要
int

那么问题是,我想知道,如果字符串的长度是20,我如何使它符合上面的过程


对于任何给定大小的字符串,我都可以复制上面的代码,下面的代码满足了您的要求

public static int power(String a) {
    int total = 0;
    for(int i = 3; i < a.length(); i += 3) {
        total ^= (int) a.charAt(i);
    }
    return total;
}
公共静态整数功率(字符串a){
int-total=0;
对于(int i=3;i
你是在用^?假设您的字符串示例故意只使用1和0,那么您的答案几乎肯定是1。除非您的第一个数字是0,否则它将是0。请澄清您的预期输出是什么?我不明白你认为
one
two
最终应该是什么。是的,你希望^2^3…@user3550935为什么它从3开始,而不是0、1或2?这似乎是一个需要解决的随机问题好吧,让我重新编辑整个问题,它会给你一个清晰的想法