C语言中Collatz猜想的起始数

C语言中Collatz猜想的起始数,c,C,我一直在做这项学校作业,我们正在用C语言创建。我已经完成了所有的代码,剩下的唯一一件事就是把传入的数字和我从逻辑中得到的数字打印出来。每次更改输入之前和之后,我都尝试使用printf(),但每次迭代只打印两次数字。下面是它应该如何工作的示例。我也会在下面写下我的代码 例如: 如果我输入数字4。我应该回去4,2,1 输入已通过:/hw1 4 预期输出:4,2,1 子ID为:0 儿童ID 1为:17488 父ID是17487 父PID 1为:17488 #include <stdio.h>

我一直在做这项学校作业,我们正在用C语言创建。我已经完成了所有的代码,剩下的唯一一件事就是把传入的数字和我从逻辑中得到的数字打印出来。每次更改输入之前和之后,我都尝试使用
printf()
,但每次迭代只打印两次数字。下面是它应该如何工作的示例。我也会在下面写下我的代码

例如

如果我输入数字
4
。我应该回去
4,2,1

输入已通过:
/hw1 4

预期输出:4,2,1
子ID为:0
儿童ID 1为:17488
父ID是17487
父PID 1为:17488

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>

int main(int argc, char *argv[]) {

  // this is the number the user passes in 
  int n;

  int pid;
  int my_status;
  pid_t my_pid, my_secondpid;
  my_pid = fork();

  if (argc == 1) {
    fprintf(stderr, "Usage: ./hw1 <starting value>\n");

    return -1;
  }

  n = atoi(argv[1]); //  n is the input starting value

  // Error checking

  if (n < 0 || n == 0) {
    printf("Number cannot be less than 0");
    return 1;

  }

  //pid_t my_pid = fork();

  if (my_pid < 0) {
    printf("Unsuccesfull in creating the child process");
    return 1;

  } else if (my_pid == 0) {

    my_secondpid = getpid();

    //pid_t my_child = getpid();

    while (n != 1) {
      //pid_t my_child = getpid();

      // if the number is even
      if (n % 2 == 0) {
        //printf("%d " , n);
        n = n / 2;
        printf(" , %d", n);
        // printf("\n child id is : %d", my_child);
        // printf(" \n parent : %d" , my_pid);
        //printf("\n parent id is : %d" , my_pid);

      }
      // if the number is odd
      else if (n % 2 != 0) {
        //printf("%d" , n);
        n = 3 * n + 1;
        printf(" ,  %d", n);
        //printf("\n child id is : %d", my_child); 
        // printf(" \n parent : %d" , my_pid);
        //printf("\n parent id is : %d" , my_pid);

      }
      //printf("%d \n", n);

    } // end of while
    printf("\n");
    printf("\nChild ID is : %d", my_pid);
    printf("\nChild ID 1 is : %d", my_secondpid);
    //printf("\nProcess ID : %d", pid); 
  } // end of else if

  else {
    wait(NULL);
    my_secondpid = getpid();
    //printf("\n The starting  pid is : %d \n" , my_pid);
    printf("\nParent ID is %d", my_secondpid);
    printf("\nParent PID 1 is : %d", my_pid);
    printf("\n");

  }
  return 0;
}
实际输出:,2,1 子ID为:0 儿童ID 1为:17488 父ID是17487 父PID 1为:17488

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>

int main(int argc, char *argv[]) {

  // this is the number the user passes in 
  int n;

  int pid;
  int my_status;
  pid_t my_pid, my_secondpid;
  my_pid = fork();

  if (argc == 1) {
    fprintf(stderr, "Usage: ./hw1 <starting value>\n");

    return -1;
  }

  n = atoi(argv[1]); //  n is the input starting value

  // Error checking

  if (n < 0 || n == 0) {
    printf("Number cannot be less than 0");
    return 1;

  }

  //pid_t my_pid = fork();

  if (my_pid < 0) {
    printf("Unsuccesfull in creating the child process");
    return 1;

  } else if (my_pid == 0) {

    my_secondpid = getpid();

    //pid_t my_child = getpid();

    while (n != 1) {
      //pid_t my_child = getpid();

      // if the number is even
      if (n % 2 == 0) {
        //printf("%d " , n);
        n = n / 2;
        printf(" , %d", n);
        // printf("\n child id is : %d", my_child);
        // printf(" \n parent : %d" , my_pid);
        //printf("\n parent id is : %d" , my_pid);

      }
      // if the number is odd
      else if (n % 2 != 0) {
        //printf("%d" , n);
        n = 3 * n + 1;
        printf(" ,  %d", n);
        //printf("\n child id is : %d", my_child); 
        // printf(" \n parent : %d" , my_pid);
        //printf("\n parent id is : %d" , my_pid);

      }
      //printf("%d \n", n);

    } // end of while
    printf("\n");
    printf("\nChild ID is : %d", my_pid);
    printf("\nChild ID 1 is : %d", my_secondpid);
    //printf("\nProcess ID : %d", pid); 
  } // end of else if

  else {
    wait(NULL);
    my_secondpid = getpid();
    //printf("\n The starting  pid is : %d \n" , my_pid);
    printf("\nParent ID is %d", my_secondpid);
    printf("\nParent PID 1 is : %d", my_pid);
    printf("\n");

  }
  return 0;
}
#包括
#包括
#包括
int main(int argc,char*argv[]){
//这是用户传入的数字
int n;
int-pid;
int我的_状态;
pid不是我的pid,我的pid;
my_pid=fork();
如果(argc==1){
fprintf(标准,“用法:./hw1\n”);
返回-1;
}
n=atoi(argv[1]);//n是输入的起始值
//错误检查
如果(n<0 | | n==0){
printf(“数字不能小于0”);
返回1;
}
//pid_t my_pid=fork();
如果(我的pid<0){
printf(“创建子进程失败”);
返回1;
}else if(my_pid==0){
my_secondpid=getpid();
//pid_t my_child=getpid();
而(n!=1){
//pid_t my_child=getpid();
//如果数字是偶数
如果(n%2==0){
//printf(“%d”,n);
n=n/2;
printf(“,%d”,n);
//printf(“\n子id为:%d”,我的孩子);
//printf(“\n父项:%d”,我的pid);
//printf(“\n父id为:%d”,我的pid);
}
//如果数字是奇数
否则如果(n%2!=0){
//printf(“%d”,n);
n=3*n+1;
printf(“,%d”,n);
//printf(“\n子id为:%d”,我的孩子);
//printf(“\n父项:%d”,我的pid);
//printf(“\n父id为:%d”,我的pid);
}
//printf(“%d\n”,n);
}//时间结束
printf(“\n”);
printf(“\n文件ID为:%d”,我的pid);
printf(“\n文件ID 1为:%d”,我的第二个PID);
//printf(“\n进程ID:%d”,pid);
}//else if结束
否则{
等待(空);
my_secondpid=getpid();
//printf(“\n起始pid为:%d\n”,我的pid);
printf(“\n租金ID为%d”,我的第二个PID);
printf(“\n租金PID 1为:%d”,我的PID);
printf(“\n”);
}
返回0;
}

请注意,
pid\u t
不一定与
“%d”
匹配。建议在printf中使用强制转换(“%ld”,(long)my_secondpid)
除非我遗漏了什么,否则在
循环之前打印它有什么问题吗?我知道了。我添加了
printf(“%d”,n)在while循环之前。@DavidCRankin无需中断
&
n%2
更具可读性,任何进行过任何优化的编译器都会注意到
2
是一个编译时间常数,幂为2,并为您这样做。我同意第二个
if
应该被删除。我对代码使用
fork()
感到困惑;实际上没有必要打印Collatz序列。