七大盗&钻石之谜C程序

Seven thieves & diamonds riddle C program

我最近为以下'Seven thieves and diamonds'谜题写了一个C程序:

"There are seven thieves, They steal diamonds from a diamond merchant and run away in jungle. While running, night sets in and they decide to rest in the jungle When everybody’s sleeping, two of the best friends get up and decide to distribute the diamonds among themselves and run away. So they start distributing but find that one diamond was extra. So they decide to wake up 3rd one and divide the diamonds again …..only to their surprise they still find one diamond extra. So they decide to wake up fourth one. Again one diamond is spare. 5th woken up……still one extra. 6th still one extra. Now they wake up 7th and diamonds are distributed equally."

虽然逻辑很容易理解,但我的程序似乎有很多错误。似乎只有数字 3、5 和 7 运行。

总体上是编程新手,感觉自己的程序不是很精巧:

#include<stdio.h> 

int main() 
{ 
    int n,i,j,k; 
    int a[30]; 
    printf("Enter the number of thieves\n"); 
    scanf("%d",&n); 
    i=n+1; 
    while(1) 
    { 
        j=2; 
        k=0; 
        while(j<n) 
        { 
            if(i%j == 1 && i%n==0) 
            { 
                a[k]=1; 
            } 
            else 
            { 
                a[k]=0; 
            } 
            if(k==n-2) 
            { 
                k=0; 
            } 
            j++; 
            k++; 
        } 
        for(j=0;j<n-1;j++) 
        { 
            if(a[j]==0) 
            { 
                break; 
            } 
            else if(j==n-3 && a[j] == 1) 
            { 
                printf("The number of diamonds = %d\n",i); 
                return; 
            } 
        } 
        i++; 
    } 
} 

如果有人能帮助我将这段代码开发成更非特定的东西,那就太好了,这样它就可以 return 输出 'n.' 的所有值此外,任何反馈一般都是高度赞赏。

你的代码很难理解,所以我写了自己的代码来调试这个,你的程序虽然晦涩难懂,但对于有效输入来说是完全正确的,你只是没有很好地处理所有情况,因此你永远处于 while 循环中。并非每个输入都适用于此问题,只有 素数 会为您提供此问题的答案,因此像 2、4 和 6 这样的输入将不起作用,因此需要对其进行处理。

这是一个测试,将您的输出与我为有效输入编写的测试进行比较。

#Of Theives   Your Code    Test Code
    3            3            3
    5            25           25
    7            301          301
    11           25201        25201
    13           83161        83161

您可以像这样编写一个快速函数来测试这种护理:

int isPrime(int tmp)
{
    int i;
    for(i = 2; i <= tmp/2; i++)
    {
        if(tmp % i == 0)
            return 0;
    }
    return 1;
}

然后你可以检查大于 1 的有效输入(因为这样就没有足够的小偷让故事发生)和质数,如下所示:

#include<stdio.h> 

int isPrime(int tmp)
{
    int i;
    for(i = 2; i <= tmp/2; i++)
    {
        if(tmp % i == 0)
            return 0;
    }
    return 1;
}



int main() 
{ 
    int n,i,j,k; 
    int a[30]; 
    printf("Enter the number of thieves that is prime and greater than 1\n"); 
    scanf("%d",&n); 
    i=n+1;
    if(isPrime(n) && n > 1)
    {
        while(1) 
        { 
            j=2; 
            k=0; 
            while(j<n) 
            { 
                if(i%j == 1 && i%n==0) 
                { 
                    a[k]=1; 
                } 
                else 
                { 
                    a[k]=0; 
                } 
                if(k==n-2) 
                { 
                    k=0; 
                } 
                j++; 
                k++; 
            } 
            for(j=0;j<n-1;j++) 
            { 
                if(a[j]==0) 
                { 
                    break; 
                } 
                else if(j==n-3 && a[j] == 1) 
                { 
                    printf("The number of diamonds = %d\n",i); 
                    return; 
                } 
            } 
            i++; 
        } 
    }
    else
    {
    printf("Input Invalid.\n"); 
    }
} 

我写的测试谜语的代码:

#include<stdio.h>


int isPrime(int tmp)
{
    int i;
    for(i = 2; i <= tmp/2; i++)
    {
        if(tmp % i == 0)
            return 0;
    }
    return 1;
}

long gcd(long a, long b) {
  if (b == 0) {
    return a;
  }
  else {
    return gcd(b, a % b);
  }
}

int main()
{
    int thieves, i;
    long diamonds, lcm = 1;
    printf("Enter the number of thieves that is prime and greater than 1:\n");
    scanf("%d",&thieves);

    if(isPrime(thieves) && thieves > 1)
    {
        for(i = 2;i < thieves;i++)
        {
                lcm = (lcm*i)/gcd(i,lcm);
        }

        i = 1;
        dimonds = lcm*i + 1;
        while(dimonds % thieves != 0)
        {
            dimonds = lcm*++i + 1;
        }
        printf("There are a minimum of diamonds is: %d\n",diamonds);
    }
    else 
    {
        printf("Input inv\n");

    }
}