小编use*_*857的帖子

如何编写一个删除字符串一部分的C函数?

我想编写一个C函数来删除给定索引范围的字符串的一部分.

例如,如果输入字符串为"ABCDEFGHIJK"且起始索引为2且结束索引为5,则输出应为:"ABGHIJK".

我试图使用两个函数来做到这一点,一个函数获取我们想要删除的子字符串:

void get_substring(char string[], char substring[], int start, int end) {
    strncpy(substring, string + start, end - start + 1);
}
Run Code Online (Sandbox Code Playgroud)

然后是删除此子字符串的第二个函数:

void remove_portion(char string[], char substring[]) {
    // memmove?
}
Run Code Online (Sandbox Code Playgroud)

我想到的另一种可能性是直接修改原始字符串而不使用子字符串:

void remove_portion(char string[], int start, int end) {
    // if end is less then the length of the string, then
    // copy everything after string[end] into a temp string
    // Then replace string[start] with '\0' and then concatenate
    // string and temp.
    // If end is …
Run Code Online (Sandbox Code Playgroud)

c string

5
推荐指数
1
解决办法
246
查看次数

在不使用sortBy的情况下,按第二个元素对元组列表进行排序

假设函数的输入是:

[(6,-275),(3,123),(7,1000),(9,-572),(3,333),(1,-23)]
Run Code Online (Sandbox Code Playgroud)

输出应该是:

[(9,-572),(6,-275),(1,-23),(3,123),(3,333),(7,1000)]
Run Code Online (Sandbox Code Playgroud)

这是我想要的功能:

sortBySnd :: Ord a => [(a,a)] -> [(a,a)]
sortBySnd [] = []
sortBySnd [(_,a),(_,b)]
    | a < b = [(_,a),(_,b)]
    | a > b = [(_,b),(_,a)]
    | a == b = [(_,a),(_,b)]
Run Code Online (Sandbox Code Playgroud)

这当然是非常错误的,但我只想展示我想要实现的目标.

对于排序函数,可以使用mergesort或除sortBy之外的一些内置排序函数.

haskell tuples list

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

如何转换Int至Double?

我有一个函数,需要一个双输入,但我必须传递一个Int到它,它给了我这个错误:

没有使用'/'的(Fractional Int)实例的实例

这是我的功能:

foo1 :: Int  -> Int
foo1 x1 = foo2 (x1 + 1)

foo2 :: Double -> Int
foo2 x = round (((3244325 * x^343)/2352352656) - ((3432 * x^234)/45324645500) + ((23453 * x^21)/2534743435)
Run Code Online (Sandbox Code Playgroud)

我不知道如何解决这个问题.

是否存在以下内容:

foo1 :: Int  -> Int
foo1 x1 = foo2 $ convertToDouble $ (x1 + 1)
Run Code Online (Sandbox Code Playgroud)

haskell

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

除了特定范围内的最大元素外,如何擦除矢量中的所有元素?

假设我有以下向量:

向量是对的向量,我们基于第一个元素进行比较.

[(1,0),(0,1),(3,2),(6,3),(2,4),(4,5),(7,6),(5,7)]
Run Code Online (Sandbox Code Playgroud)

我想擦除除最大值之外的特定范围内的所有元素.

例如,如果范围是$ l = 2 $和$ r = 5 $,那么输出:

[(1,0),(0,1),(6,3),(7,6),(5,7)]
Run Code Online (Sandbox Code Playgroud)

现在,如果我们再次对$ l = 1 $,$ r = 4 $的输出数组执行此操作,那么输出:

[(1,0),(7,6)]
Run Code Online (Sandbox Code Playgroud)

我发现这个我认为在这里很有用,但我不知道如何使它成对使用.

这是我的尝试:

int main(int argc, char const *argv[]) {
    int N;
    cin >> N;
    vector< pair<int,int> > vector_of_pairs(N);

    for (int i = 0; i < N; i++) {
        int input;
        cin >> input;
        vector_of_pairs[i] = make_pair(input, i);
    }

    int l, r;
    cin >> l >> r;

    int max_in_range = vector_of_pairs[l].first;

    for (int i = …
Run Code Online (Sandbox Code Playgroud)

c++ vector

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

在C中查找int数组中的数字频率

我正在尝试编写一个函数,它接受一个数组的大小和一个由数字组成的int数组作为输入,并打印每个数字的频率.

样本输入和输出:

Input: [1,2,2,3,3,3]

Output:
1 occurs 1 times.
2 occurs 2 times
3 occurs 3 times.
Run Code Online (Sandbox Code Playgroud)

这是我的尝试(不是最优雅的):

void freq(int size, int numArray[]) {
    int one=0, two=0, thr=0, fou=0, fiv=0, six=0, sev=0, eit=0, nin=0;
    int i, j;

    for (i = 0; i < size; i++) {
        for (j = 1; j < size; j++) {
            if (numArray[i] == numArray[j] && numArray[i] == 1) {
                one+=1;
            }
            else if (numArray[i] == numArray[j] && numArray[i] == 2) {
                two+=1;
            }
            else if …
Run Code Online (Sandbox Code Playgroud)

c arrays io function frequency

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

标签 统计

c ×2

haskell ×2

arrays ×1

c++ ×1

frequency ×1

function ×1

io ×1

list ×1

string ×1

tuples ×1

vector ×1