小编Rit*_*esh的帖子

从perodic table元素形成的最大单词的算法

我想为以下问题场景编写算法

获取周期表元素的名称,找到可以形成的最大单词?如符号Na, Ne等应被视为单个元件.

这是在一家知名公司的求职面试中被问到的.任何人都可以帮我解决这个问题.

algorithm cpu-word

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

在C中调用main函数

#define f(x) (x*(x+1)*(2*x+1))/6
void terminate();
main()
{
   int n,op;
   char c;
   printf("Enter n value\n");
   scanf("%d",&n);
   op=f(n);
   printf("%d",op);
   printf("want to enter another value: (y / n)?\n");
   scanf("%c",&c);   // execution stops here itself without taking input.
   getch();
   if(c=='y')
    main();
   else
    terminate();
   getch();

 }
void terminate()
{
exit(1);
}
Run Code Online (Sandbox Code Playgroud)

在上面的程序中,我想从用户那里获取输入,直到他输入一个n值.为此,我试图main()重复调用函数.如果它在C中是合法的,我想知道为什么程序终止scanf("%c",&c)于注释行所示.有人,请帮忙.

c program-entry-point

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

使用指针反转字符串的C代码

#include <stdio.h>

main() 
{
    char* str;
    char* strrev;
    int   i = 0;
    int   j = 0;
    int   c;

    printf("Enter the string\n");
    scanf("%[^\n]%*c", str);

    while (*(str + i) != '\n')
    {
        i++;
    }

    for (c = i; c >= 0; c--)
    {
        *(strrev + j) = *(str + c);
        j++;
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的反转字符串的代码.当我编译代码时,它会出错segmentation fault.有人帮助我理解错误并实现我的错误.谢谢

c arrays pointers character

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