# 算法-等值首尾和

### 评论（4）

2017-10-04 4 楼

<?php
{
\$sumArr = array();
if(empty(\$arr))
{
return \$sumArr;
}
\$n = count(\$arr);
\$tail = \$arr[\$n-1];
\$i = 1;
\$j = \$n-2;
while(\$i <= \$n-1 && \$j >= 0)
{
\$tail += \$arr[\$j--];
else
{
\$tail += \$arr[\$j--];
}
}
return \$sumArr;
}
\$arr = array('3','6', '2', '1', '4', '5', '2');
print_r(\$a);
?>

2017-09-17 3 楼

void headTail(unsigned int *a, int len)
{
unsigned int prefixSum = a[0];
unsigned int suffixSum = a[len - 1];
int preInc = 0;
int sufInc = 0;
int count = 0;

for(preInc = 0, sufInc = len - 1; (preInc < len) && (sufInc >= 0); )
{
if(prefixSum > suffixSum)
{
--sufInc;
suffixSum += a[sufInc];
}
else if(prefixSum < suffixSum)
{
++preInc;
prefixSum += a[preInc];
}
else
{
++count;
printf("prefixSum == suffixSum %dn", prefixSum);
if((preInc == len - 1) || (sufInc == 0))
{
break;
}
else
{
prefixSum += a[++preInc];
suffixSum += a[--sufInc];
}
}
}

printf("count = %dn", count);
}

2017-07-21 2 楼

import java.util.*;
/**
*/
public class Test2 {
public static void main(String args[]) {
int[] x = { 3, 6, 2, 1, 4, 5, 2 };
int s = 0;// 用来表示2进制数化为10进制的变量
System.out.print("前置和: ");
for (int i = x.length - 1; i >= (-x.length); i--)// 前置
{
if (i >= 0)
s = s + (int) Math.pow(2, i);
else if (i == -1) {
System.out.print("n后置和: ");
s = 1;
} else {
s = s + (int) Math.pow(2, (int) Math.abs(i + 1));
}
int h = s, j = x.length - 1, sum = 0;// 计算二进制数的临时变量，求出的和
for(;j>=0;j--) {
if (h % 2 == 1)
sum += x[j];
h /= 2;
}
System.out.print(sum + " ");
}
}
}

2017-06-13 1 楼

/**
* clone a java code
*/
static void returnCount(){
int sourceArray[] = new int[]{3, 6, 2, 1, 4, 5, 2};
int cloneArray[] = new int[sourceArray.length];
System.arraycopy(sourceArray, 0, cloneArray, 0, sourceArray.length);

int count = 0;
int tail = cloneArray.length -1;

for (;(head < sourceArray.length) && (tail >= 0);) {
tail--;
cloneArray[tail] += cloneArray[tail+1];
} else if(sourceArray[head] <cloneArray [tail]) {
} else {
count++;
tail--;
if (tail >=0) {
cloneArray[tail] += cloneArray[tail+1];
}
}
}
}
System.out.println("total:" + count);
}

1890 主题
5513 回复
23876 人气