小编Dhr*_*hok的帖子

在 C++ 中的排序向量中搜索范围 [x,y] [使用 lower_bound() 和 upper_bound() ]

我有一个排序向量数组,

向量<整数> b[1000009];

现在我必须在行 b[factor] 中搜索 x 和 y 之间的范围。
'factor'、'x' 和 'y' 都是整数。
我使用了以下方法:

        int lb,ub;
        if(b[factor][0]>=x){lb=0;}
        else
            {
                lb=upper_bound(b[factor].begin(),b[factor].end(),x)-b[factor].begin();
                while(b[factor][lb-1]>=x)lb--;
            }
        if(b[factor][sz2-1]<=y)
            {
                ub=sz2-1;
            }
        else {
                ub=lower_bound(b[factor].begin(),b[factor].end(),y)-b[factor].begin();
                while(b[factor][ub]>y)ub--;
            }
Run Code Online (Sandbox Code Playgroud)

但是这种方法并不能一直给出正确的答案。此外,我想使用一些比较器功能来实现相同的功能。这是我第一次使用lower_bound()upper_bound()。所以请告诉我如何在这里实现比较器功能。

c++ stl lower-bound

4
推荐指数
1
解决办法
1787
查看次数

有趣的2 - 算法/数学(来自Hackerrank ACM APC)

我在最近的比赛中遇到了一个问题.我无法找到解决方案,也没有关于这个问题的社论.

问题链接

我在这里也引用了问题陈述,以防链接不起作用.

求出整数n大于或等于A且小于或等于B(A <= n <= B),并且2 ^ n十进制表示以n结尾.

例如: 2 ^ 36 = 68719476736,以"36"结尾.

INPUT

第一行包含整数T,即测试用例的数量.随后是T行,每行包含两个整数A和B.

约束

1 <= T <= 10^5

A<=B

A,B <= 10^150
Run Code Online (Sandbox Code Playgroud)

OUTPUT

打印T行,每行包含相应测试用例的答案.


样本输入

2
36 36
100 500
Run Code Online (Sandbox Code Playgroud)

样本输出

1
0
Run Code Online (Sandbox Code Playgroud)

c algorithm math

4
推荐指数
1
解决办法
1774
查看次数

为什么我不能在Java中的char数组中存储日语UTF-8字符?

我有一个字符串"1234567(Asics(アシックスワーキング))".它有unicode字符,有些是ASCII的一部分,有些则不是.java的作用是ASCII字符需要一个字节,其他unicode字符需要两个字节.

我的程序的某些部分无法以此格式处理字符串.所以我想将值编码为转义序列.

所以字符串

"1234567(Asics(アシックスワーキング))"

会映射到

"\ u0031\u0032\u0033\u0034\u0035\u0036\u0037\u0028\u0041\u0073\u0069\u0063\u0073\u0020\u0028\u30a2\u30b7\u30c3\u30af\u30b9\u30ef\u30fc\u30ad\u30f3\u30b0\u0029\u0020\u0029"

.

我写了这个函数来做到这一点: -

public static String convertToEscaped(String utf8) throws java.lang.Exception
    {
    char[] str = utf8.toCharArray();
    StringBuilder unicodeStringBuilder = new StringBuilder();
    for(int i = 0; i < str.length; i++){
    char charValue = str[i];
    int intValue = (int) charValue;
    String hexValue = Integer.toHexString(intValue);
    unicodeStringBuilder.append("\\u");
    for (int length = hexValue.length(); length < 4; length++) {
        unicodeStringBuilder.append("0");
    }
    unicodeStringBuilder.append(hexValue);
    }
    return unicodeStringBuilder.toString();
    }
Run Code Online (Sandbox Code Playgroud)

这在我的程序之外工作正常但在我的程序中引起了问题.这发生在线路上char[] str = utf8.toCharArray(); 某种程度上我失去了我的日本unicode字符,这发生了,因为t将这些字符分成char数组中的2.

所以我决定byte []改用它.

    public static String convertToEscaped(String …
Run Code Online (Sandbox Code Playgroud)

java string unicode utf-8

2
推荐指数
1
解决办法
759
查看次数

标签 统计

algorithm ×1

c ×1

c++ ×1

java ×1

lower-bound ×1

math ×1

stl ×1

string ×1

unicode ×1

utf-8 ×1