小编Hoa*_*Nam的帖子

指针有问题吗?

我在理解 *binsearch 函数中某些代码行的机制时遇到了问题。特别是,low 和 high 是两个指针,分别初始化为 &tab[0] 和 &tab[n]。在下一行我看到low<high我认为它无效,因为不可能比较两个指针的两个地址。下一行也有同样的问题。我不知道我是否正确,我需要大家的一些想法。

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define MAXWORD 100
int getword(char *, int);
struct key *binsearch(char *, struct key *, int);
/* count C keywords; pointer version */
main()
{
    char word[MAXWORD];
    struct key *p;
    while (getword(word, MAXWORD) != EOF)
        if (isalpha(word[0]))
            if ((p=binsearch(word, keytab, NKEYS)) != NULL)
                p->count++;
    for (p = keytab; p < keytab + NKEYS; p++)
        if (p->count > 0)
            printf("%4d %s\n", p->count, p->word);
    return 0; …
Run Code Online (Sandbox Code Playgroud)

c struct pointers

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

在 SPOJ 上提交代码会出现运行时错误 (SIGABRT)

我在 SPOJ 上做了一个练习来练习高级算法。


问题陈述如下:

Harish 去超市为他的 'n' 个朋友买了正好 'k' 公斤的苹果。超市真的很奇怪。物品的定价是非常不同的。他去了苹果区询问价格。推销员给了他一张卡片,他在卡片上发现苹果的价格不是每公斤。苹果被装在盖子里,每个包含“x”公斤苹果,x > 0,“x”是一个整数。“x”公斤包裹的价值为“y”卢比。因此,标语牌包含一个表,其中条目“y”表示“x”公斤包的价格。如果 'y' 是 -1,则表示相应的数据包不可用。现在由于苹果只能以包形式提供,他决定最多为他的“n”个朋友购买“n”包,即他不会购买超过 n 包的苹果。Harish 非常喜欢他的朋友,所以他不想让他的朋友失望。


这是我用来解决问题的代码:

#include <algorithm>
#include <iostream>
#include <vector>

using std::cout;
using std::cin;
using std::vector;
using std::endl;

int MinValueOf(int a, int b)
{
    return (a < b) ? a : b;
}
int BuyingApple(vector<int> PriceaTag, int Friends, int KilogramsToBuy)
{
    vector<vector<int>> Table(Friends + 1, vector<int>(KilogramsToBuy + 1, 0));
    for(int i = 1; i <= Friends; i++)
    {
        for(int j = 0; j …
Run Code Online (Sandbox Code Playgroud)

c++ runtime-error sigabrt

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

无界背包与经典背包对比

我在互联网上读过两个不同的问题0-1背包问题无界背包问题。我发现这两个问题都可以通过动态规划来解决,但是以两种不同的方式。如果0-1背包问题使用二维数组来解决,无界背包问题只使用一维数组。

您可以阅读更多0-1背包问题无界背包问题

据我所知,这两个问题的区别在于,0-1 背包问题只包含有限数量的东西,而无界背包问题可以获取任何资源的 1 个或多个实例。但是,我不知道为什么要改变解决这个问题的方式?你能告诉我原因吗?

c++ algorithm dynamic-programming

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

无法读取 C 中的文本文件

我有一个任务来创建一个菜单程序,该程序使用链接列表来存储电话目录列表。这是我的源代码:

int isempty(FILE *in)
{
    return in == NULL;
}
node *makenewnode(item newitem)
{
    node *newnode = (node *) malloc(sizeof(node));
    newnode->info = newitem;
    return newnode;
}
int countlines(FILE *datain)
{
    int lines = 0;
    char string[MAX];
    while (fgets(string, MAX, datain))
        lines++;
    return lines;
}
node *push(node *listin, item newitem)
{
    node *newnode = makenewnode(newitem);
    if(listin == NULL)
    {
        newnode->next = NULL;
        listin = newnode;
    }
    else
    {
        newnode->next = listin;
        listin = newnode;
        head = listin;
    }
    cur = head; …
Run Code Online (Sandbox Code Playgroud)

c file linked-list file-handling

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