A. Phoenix and Balancetime

本文探讨了一个算法问题,即如何将一组按2的幂次规律排列的硬币分为两堆,使得每堆硬币的数量相等且两堆之间的重量差最小。通过示例说明了最优解的寻找过程,并提供了一段C++代码实现,但该代码存在问题,有待高手指导优化。

Phoenix has n coins with weights 21,22,…,2n. He knows that n is even.He wants to split the coins into two piles such that each pile has exactly n/2 coins and the difference of weights between the two piles is minimized. Formally, let a denote the sum of weights in the first pile, and b denote the sum of weights in the second pile. Help Phoenix minimize |a−b|, the absolute value of a−b.

Input

The input consists of multiple test cases.
The first line contains an integer t (1≤t≤100) — the number of test cases.
The first line of each test case contains an integer n (2≤n≤30; n is even) — the number of coins that Phoenix has.

Output

For each test case, output one integer — the minimum possible difference of weights between the two piles.

Example
input

2
2
4

output

2
6

Note

In the first test case, Phoenix has two coins with weights 2 and 4. No matter how he divides the coins, the difference will be 4−2=2.
In the second test case, Phoenix has four coins of weight 2, 4, 8, and 16. It is optimal for Phoenix to place coins with weights 2 and 16 in one pile, and coins with weights 4 and 8 in another pile. The difference is (2+16)−(4+8)=6.
一开始没使用函数,不知道为什么会出错,求高人指点。

#include<iostream>
#include<cmath>
using namespace std;
int bal(int n)
{
 int s1,s2=0;
  for(int i=1;i<n/2;i++)
  {
   s1=s1+pow(2,i);
  }
  s1=s1+pow(2,n);
  for(int j=n/2;j<n;j++)
  {
   s2=s2+pow(2,j);
  }
  cout<<fabs(s1-s2)-n<<endl;
}
int main()
{
 int t=0;
 cin>>t;
 while(t--)
 {
  int n;
  cin>>n;
  bal(n);
 }
}
java.sql.SQLException: ERROR 726 (43M10): Inconsistent namespace mapping properties. Ensure that config phoenix.schema.isNamespaceMappingEnabled is consistent on client and server. at org.apache.phoenix.exception.SQLExceptionCode$Factory$1.newException(SQLExceptionCode.java:607) at org.apache.phoenix.exception.SQLExceptionInfo.buildException(SQLExceptionInfo.java:217) at org.apache.phoenix.query.ConnectionQueryServicesImpl.checkClientServerCompatibility(ConnectionQueryServicesImpl.java:1731) at org.apache.phoenix.query.ConnectionQueryServicesImpl.ensureTableCreated(ConnectionQueryServicesImpl.java:1460) at org.apache.phoenix.query.ConnectionQueryServicesImpl.createTable(ConnectionQueryServicesImpl.java:1939) at org.apache.phoenix.schema.MetaDataClient.createTableInternal(MetaDataClient.java:3090) at org.apache.phoenix.schema.MetaDataClient.createTable(MetaDataClient.java:1117) at org.apache.phoenix.compile.CreateTableCompiler$CreateTableMutationPlan.execute(CreateTableCompiler.java:420) at org.apache.phoenix.jdbc.PhoenixStatement$2.call(PhoenixStatement.java:443) at org.apache.phoenix.jdbc.PhoenixStatement$2.call(PhoenixStatement.java:425) at org.apache.phoenix.call.CallRunner.run(CallRunner.java:53) at org.apache.phoenix.jdbc.PhoenixStatement.executeMutation(PhoenixStatement.java:424) at org.apache.phoenix.jdbc.PhoenixStatement.executeMutation(PhoenixStatement.java:412) at org.apache.phoenix.jdbc.PhoenixStatement.executeUpdate(PhoenixStatement.java:1990) at org.apache.phoenix.query.ConnectionQueryServicesImpl$12.call(ConnectionQueryServicesImpl.java:3312) at org.apache.phoenix.query.ConnectionQueryServicesImpl$12.call(ConnectionQueryServicesImpl.java:3266) at org.apache.phoenix.util.PhoenixContextExecutor.call(PhoenixContextExecutor.java:76) at org.apache.phoenix.query.ConnectionQueryServicesImpl.init(ConnectionQueryServicesImpl.java:3266) at org.apache.phoenix.jdbc.PhoenixDriver.getConnectionQueryServices(PhoenixDriver.java:255) at org.apache.phoenix.jdbc.PhoenixEmbeddedDriver.createConnection(PhoenixEmbeddedDriver.java:144) at org.apache.phoenix.jdbc.PhoenixDriver.connect(PhoenixDriver.java:221) at sqlline.DatabaseConnection.connect(DatabaseConnection.java:135) at sqlline.DatabaseConnection.getConnection(DatabaseConnection.java:192) at sqlline.Commands.connect(Commands.java:1364) at sqlline.Commands.connect(Commands.java:1244) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at sqlline.ReflectiveCommandHandler.execute(ReflectiveCommandHandler.java:38) at sqlline.SqlLine.dispatch(SqlLine.java:730) at sqlline.SqlLine.initArgs(SqlLine.java:410) at sqlline.SqlLine.begin(SqlLine.java:515) at sqlline.SqlLine.start(SqlLine.java:267) at sqlline.SqlLine.main(SqlLine.java:206) sqlline version 1.9.0
03-18
<think>好的,我需要解决用户提出的关于java.sql.SQLException: ERROR 726 (43M10)的问题。用户遇到的错误信息是"Inconsistent namespace mapping properties. Ensure that config phoenix.schema.isNamespaceMappingEnabled is consistent on client and server.",首先我要理解这个错误的背景和原因。 首先,用户可能在使用Apache Phoenix时遇到了这个问题。Phoenix是一个构建在HBase上的SQL层,用于进行OLTP和查询大数据。这个错误涉及到命名空间映射属性的不一致,具体是phoenix.schema.isNamespaceMappingEnabled这个配置在客户端和服务器端不一致导致的。 接下来,我需要拆解错误信息。错误726 (43M10)是Phoenix特有的错误代码,通常与命名空间映射有关。phoenix.schema.isNamespaceMappingEnabled这个配置的作用是控制是否启用HBase命名空间与Phoenix schema的映射。当客户端和服务器端的这个配置不一致时,就会触发这个错误。 接下来,我需要思考用户可能的情境。例如,用户可能在创建表或连接数据库时遇到这个问题,因为错误堆栈中提到了createTable和connect相关的方法。这时候,客户端(比如应用程序或Sqlline工具)的配置与Phoenix服务端的配置不一致,导致兼容性检查失败。 接下来,解决方案的步骤应该包括: 1. 确认客户端和服务端的Phoenix版本是否一致。版本不同可能导致默认配置不同,进而引发问题。 2. 检查phoenix.schema.isNamespaceMappingEnabled的配置。用户需要确保客户端和服务端的这个参数值相同。这个配置可能在phoenix-site.xml中设置,或者在连接时通过JDBC URL参数指定。 3. 如果使用的是HBase命名空间,需要确保服务端启用了该配置(设置为true),同时客户端也启用。反之,如果未使用,则两端都应设置为false。 4. 重启服务以使配置生效,特别是在修改了服务端配置后,可能需要重启HBase或Phoenix服务。 5. 验证配置是否生效,可以通过服务端的配置文件和客户端的连接参数进行检查。 6. 处理版本兼容性问题,比如升级客户端或服务端到相同版本,避免因版本差异导致配置默认值不同。 此外,可能需要指导用户如何查找配置文件的位置,例如在Phoenix的安装目录下的conf文件夹中的phoenix-site.xml,或者在HBase的配置目录中。同时,提醒用户在集群环境中,所有节点上的配置都需要保持一致,否则可能出现部分节点配置不一致的问题。 还需要考虑用户可能遇到的常见误区,比如误以为只需要修改客户端或服务端其中一方的配置,或者没有正确重启服务导致配置未生效。此外,用户可能不清楚如何在JDBC URL中指定该参数,例如添加连接属性如phoenix.schema.isNamespaceMappingEnabled=true。 最后,应该给出一个总结性的步骤列表,帮助用户一步步排查和解决问题,确保他们能够系统地检查和调整配置,从而消除不一致的情况,解决错误。</think>### 问题解析:`java.sql.SQLException: ERROR 726 (43M10)` 此错误表示 **Apache Phoenix** 的客户端与服务器端的 **命名空间映射配置** 不一致,导致操作(如表创建、连接等)无法执行。核心问题在于配置项 `phoenix.schema.isNamespaceMappingEnabled` 的值在客户端和服务端不匹配。 --- ### 原因说明 Phoenix 使用 `phoenix.schema.isNamespaceMappingEnabled` 控制 **HBase 命名空间与 Phoenix Schema 的映射关系**: - **`true`**: 启用映射,要求 HBase 启用命名空间功能,且 Phoenix Schema 对应 HBase 的命名空间。 - **`false`**: 禁用映射,所有 Phoenix Schema 直接映射到 HBase 的默认命名空间。 **客户端和服务端的该配置必须一致**,否则会触发此错误。 --- ### 解决步骤 #### 1. **检查版本一致性** - 确保客户端(如 JDBC 驱动、Sqlline)与 Phoenix 服务端版本**完全一致**。 - 版本差异可能导致默认配置不同(例如旧版本默认 `false`,新版本默认 `true`)。 --- #### 2. **配置服务端** - **修改 Phoenix 服务端配置**(`phoenix-site.xml`): ```xml <property> <name>phoenix.schema.isNamespaceMappingEnabled</name> <value>true</value> <!-- 或 false,需与客户端一致 --> </property> ``` - **同步到所有 HBase 节点**,并重启 HBase/Phoenix 服务。 --- #### 3. **配置客户端** - **在客户端连接时显式指定配置**(JDBC URL): ```bash jdbc:phoenix:<zk_host>:2181:/hbase?phoenix.schema.isNamespaceMappingEnabled=true ``` - 或在客户端的 `phoenix-site.xml` 中设置相同值。 --- #### 4. **验证配置生效** - **服务端**:通过 HBase Master Web UI 或检查日志确认配置加载。 - **客户端**:通过 JDBC 连接参数或调试日志确认配置值。 --- #### 5. **处理已存在的表** - 若配置从 `false` 改为 `true`,**需手动迁移旧表**到新命名空间,或保持配置为 `false` 以兼容旧表。 --- ### 常见误区 - **仅修改单端配置**:必须同时更新服务端和客户端。 - **忽略版本差异**:不同版本的默认值可能不同(如 Phoenix 4.14+ 默认 `true`)。 - **未重启服务**:修改服务端配置后需重启 HBase RegionServer 和 Phoenix QueryServer。 --- ### 总结 通过**统一客户端与服务端的 `phoenix.schema.isNamespaceMappingEnabled` 值**,并确保版本一致,即可解决此错误。若需长期使用命名空间,建议启用此配置并迁移旧表。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

【执珪】瑕瑜·夕环玦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值