leetcode第一问题称为twi_sum im遇到错误,请帮助我解决我所做的事情

发布于 2025-02-13 18:05:37 字数 770 浏览 1 评论 0原文

这是我的leetcode第一天第一日问题,称为twe_sum am遇到错误,请帮助我解决我所做的错误, 在VS代码中,下面的代码没有出现错误,也没有输出,但是在Leetcode I中遇到了一个错误:

第25行:CHAR 5:错误:'MAIN'[SOLELE.C]的相互冲突类型 int main(int argc,char *argv []){ ^~~~ 如果我的代码只有22行,但是我在第25行

    #include <stdio.h>
    void main()
    {
    int n, a[10], i, j, t;
    printf("enter the array size");
    scanf("%d", &n);
    printf("enter th array values");
    for (i = 0; i < n; i++)
     scanf("%d", &a[I]);
    printf("enter the targest sum");
scanf("%d", &t);
for (i = 0; i < n; n++)
{
    for (j = i+1; j < n; n++)
    {
        if (a[i] + a[j] == t)
        {
            printf("[%d,%d]", i, j);
        }
      }
    }
  }

第二次尝试中遇到了错误:我在最后一个IF语句中替换了一个新代码,以其他方式检查。所以,现在我得到了输出,但不是正确的

this is my leetcode 1st day 1st problem called two_sum am getting an error, please help me with the mistake what I have done,
in vs code this, below code was getting no error and also no output, but in leetcode I getting an error :

Line 25: Char 5: error: conflicting types for ‘main’ [solution.c]
int main(int argc, char *argv[]) {
^~~~
where my code is only 22 lines but I was getting error in the 25th line

    #include <stdio.h>
    void main()
    {
    int n, a[10], i, j, t;
    printf("enter the array size");
    scanf("%d", &n);
    printf("enter th array values");
    for (i = 0; i < n; i++)
     scanf("%d", &a[I]);
    printf("enter the targest sum");
scanf("%d", &t);
for (i = 0; i < n; n++)
{
    for (j = i+1; j < n; n++)
    {
        if (a[i] + a[j] == t)
        {
            printf("[%d,%d]", i, j);
        }
      }
    }
  }

2nd try: I replaced with a new code in the last if statement to check in another way. so, now I got output but, not the correct one

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鱼窥荷 2025-02-20 18:05:38

根据C标准,应像无参数的函数主

int main( void )

函数一样,而不是固定数量的元素,

int n, a[10], i, j, t;

您可以使用可变长度数组,例如

int n, i, j, t;
printf("enter the array size");
scanf("%d", &n);
int a[n];

另一种方法是动态分配数组,就像

int *a = malloc( n * sizeof( int ) );

您可以编写每对一对一对 类的新线路中的

printf("[%d,%d]\n", i, j);

所有成对之

for (i = 0; i < n; n++)
{
    for (j = i+1; j < n; n++)
    {
        if (a[i] + a[j] == t)
        {
            printf("[%d,%d] ", i, j);
        }
    }
}

putchar( '\n' );

scanf("%d", &a[I]);
              ^^^

scanf("%d", &a[i]);
              ^^^

According to the C Standard the function main without parameters shall be declared like

int main( void )

Instead of the array with the fixed number of elements

int n, a[10], i, j, t;

you could use a variable length array like for example

int n, i, j, t;
printf("enter the array size");
scanf("%d", &n);
int a[n];

Another approach is to allocated the array dynamically like

int *a = malloc( n * sizeof( int ) );

You can write either each pair of values in a new line like

printf("[%d,%d]\n", i, j);

Or after all pairs you could print the new line character like

for (i = 0; i < n; n++)
{
    for (j = i+1; j < n; n++)
    {
        if (a[i] + a[j] == t)
        {
            printf("[%d,%d] ", i, j);
        }
    }
}

putchar( '\n' );

{ay attention to that you have a typo

scanf("%d", &a[I]);
              ^^^

you need to write

scanf("%d", &a[i]);
              ^^^
溺孤伤于心 2025-02-20 18:05:38

尝试声明mainint main(int argc,char * argv [])int main(void)void main通常不是有效的类型声明,因此我认为vs code and Leetcode不会将您的主声明视为程序的条目。

Try declaring main as int main(int argc, char * argv[]) or int main(void). void main is usually not a valid type declaration so I presume both VS Code and Leetcode do not treat your main declaration as the entry to your program.

凉墨 2025-02-20 18:05:38

在LeetCode中,您无需(也不应该)编写自己的主要功能。 LeetCode具有自己的主要功能。另外,您不需要包含库。您需要做的就是仅实现二Xosum函数。

这是两个总和问题的LeetCode的默认代码定义:



/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* twoSum(int* nums, int numsSize, int target, int* returnSize){

}

您需要做的是在twosum()函数中写代码。


// don't need to #include<stdio.h>

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* twoSum(int* nums, int numsSize, int target, int* returnSize){
    // write your code here.
}


// don't need to write main function

In leetcode, you don't need to (and should not) write your own main function. Leetcode has its own main function. Also you don't need to include libraries. What you need to do is just to implement the twoSum function.

Here's Leetcode's default code definition for the two sum problem:



/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* twoSum(int* nums, int numsSize, int target, int* returnSize){

}

What you need to do is write code within the twoSum() function.


// don't need to #include<stdio.h>

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* twoSum(int* nums, int numsSize, int target, int* returnSize){
    // write your code here.
}


// don't need to write main function

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文