鱼C论坛

 找回密码
 立即注册
查看: 1248|回复: 5

[已解决]C语言 代码不报错 就是没有输出 源代码已附上

[复制链接]
发表于 2021-7-25 22:30:11 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 本空逐弥 于 2021-7-25 22:35 编辑

题目
给出两个非空的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位数字。如果我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。您可以假设除了数字0之外,这两个数都不会以0 开头。

示例:
输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
输出:7 -> 0 -> 8
原因:342 + 465 = 807

/**
* Definition for singly-linked list.
* struct ListNode {
*     int val;
*     struct ListNode *next;
* };
*/

struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2){

}

狗代码如下
  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #include <math.h>
  4. #define LEN sizeof(struct ListNode)

  5. struct ListNode
  6. {
  7.       int val;
  8.     struct ListNode *next;
  9. };
  10. int n;

  11. struct ListNode*creat()
  12. {
  13.     struct ListNode*head;
  14.     struct ListNode*p1,*p2;
  15.     n=0;
  16.     p1=p2=(struct ListNode*)malloc(LEN);
  17.     scanf("%d",&p1->val);
  18.     head=NULL;
  19.     while(p1->val!=-1)
  20.     {
  21.         n=n+1;
  22.         if(n==1)head=p1;
  23.         else p2->next=p1;
  24.         p2=p1;
  25.         p1=(struct ListNode*)malloc(LEN);
  26.         scanf("%d",&p1->val);
  27.     }
  28.     p2->next=NULL;
  29.     return(head);
  30. }

  31. struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2)
  32. {
  33.     int num1=0,num2=0,num=0;
  34.     int count=0,s=0,t;
  35.     struct ListNode*p,*q;
  36.     p=NULL;
  37.     q=p;
  38.     while(l1->val!=-1)
  39.     {
  40.         num1=num1+(l1->val)*(10^count);
  41.         l1=l1->next;
  42.         count++;
  43.     }
  44.    count=0;
  45.    while(l2->val!=-1)
  46.    {
  47.        num2=num2+(l2->val)*(10^count);
  48.        l2=l2->next;
  49.        count++;
  50.    }
  51.    count=0;
  52.    num=num1+num2;
  53.    t=num;
  54.    if(num==0)
  55.    {
  56.        p->val=0;
  57.        p->next=NULL;
  58.    }
  59.    else
  60.    {
  61.        while(t)
  62.        {
  63.            t=t/10;
  64.            s++;
  65.        }
  66.     p->val=num/(10^count)%10;
  67.     count++;
  68.    for(int i=2;i<=t;i++)
  69.    {
  70.        p=p->next;
  71.        p->val=num/(10^count)%10;
  72.        count++;
  73.    }
  74.    p->next=NULL;
  75.    }
  76.    return q;
  77. }


  78. void print(struct ListNode*head)
  79. {
  80.     struct ListNode*p;
  81.     p=head;
  82.     if(head!=NULL)
  83.         do
  84.     {
  85.         printf("%d ",p->val);
  86.         p=p->next;
  87.     }while(p!=NULL);
  88. }

  89. int main()
  90. {
  91.     struct ListNode *l1,*l2,*head;
  92.     l1=creat();
  93.     l2=creat();
  94.     head=addTwoNumbers(l1,l2);
  95.     print(head);
  96.         return 0;
  97. }
复制代码


求教大佬~
最佳答案
2021-7-25 23:31:44
有好多问题
  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #include <math.h>

  4. #define LEN sizeof(struct ListNode)

  5. struct ListNode
  6. {
  7.     int val;
  8.     struct ListNode *next;
  9. };

  10. int n;

  11. struct ListNode*creat()
  12. {
  13.     struct ListNode*head;
  14.     struct ListNode*p1,*p2;
  15.     n=0;
  16.     p1=p2=(struct ListNode*)malloc(LEN);
  17.     scanf("%d",&p1->val);
  18.     head=NULL;
  19.     while(p1->val!=-1)
  20.     {
  21.         n=n+1;
  22.         if(n==1)head=p1;
  23.         else p2->next=p1;
  24.         p2=p1;
  25.         p1=(struct ListNode*)malloc(LEN);
  26.         scanf("%d",&p1->val);
  27.     }
  28.     p2->next=NULL;
  29.     return(head);
  30. }

  31. struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2)
  32. {
  33.     int num1=0,num2=0,num=0;
  34.     //int count=0,s=0,t;
  35.     int count=0;
  36.     struct ListNode*p,*q;
  37.     p=NULL;
  38.     q=p;
  39.     //while(l1->val!=-1)
  40.     while(l1)
  41.     {
  42.         //num1=num1+(l1->val)*(10^count);
  43.         num1 = num1 + l1->val * pow(10, count);
  44.         l1=l1->next;
  45.         count++;
  46.     }
  47.     count=0;
  48.     //while(l2->val!=-1)
  49.     while(l2)
  50.     {
  51.         /*num2=num2+(l2->val)*(10^count);*/
  52.         num2 = num2 + l2->val * pow(10, count);
  53.         l2=l2->next;
  54.         count++;
  55.     }
  56.     struct ListNode *head = NULL;
  57.     p = q = NULL;
  58.     num = num1 + num2;
  59.     do {
  60.         p = malloc(sizeof(struct ListNode));
  61.         p->next = NULL;
  62.         p->val = num % 10;
  63.         num /= 10;
  64.         if(!head) head = q = p;
  65.         else q->next = p, q = q->next;
  66.     } while(num);
  67.     return head;
  68.     /*
  69.     count=0;
  70.     num=num1+num2;
  71.     t=num;
  72.     if(num==0)
  73.     {
  74.         // 不申请内存?
  75.         p->val=0;
  76.         p->next=NULL;
  77.     }
  78.     else
  79.     {
  80.         while(t)
  81.         {
  82.             t=t/10;
  83.             s++;
  84.         }
  85.         p->val=num/(10^count)%10;
  86.         count++;
  87.         for(int i=2;i<=t;i++)
  88.         {
  89.             p=p->next;
  90.             p->val=num/(10^count)%10;
  91.             count++;
  92.         }
  93.         p->next=NULL;
  94.     }
  95.     return q;
  96.     */
  97. }


  98. void print(struct ListNode*head)
  99. {
  100.     struct ListNode*p;
  101.     p=head;
  102.     if(head!=NULL)
  103.         do
  104.         {
  105.             printf("%d ",p->val);
  106.             p=p->next;
  107.         }while(p!=NULL);
  108. }

  109. int main()
  110. {
  111.     struct ListNode *l1,*l2,*head;
  112.     l1=creat();
  113.     l2=creat();
  114.     head=addTwoNumbers(l1,l2);
  115.     print(head);
  116.     // 又不释放内存
  117.     return 0;
  118. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-7-25 22:53:41 | 显示全部楼层
num1=num1+(l1->val)*(10^count);
C语言中 ^ 是位异或运算符,可不是乘方
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-7-25 23:31:44 | 显示全部楼层    本楼为最佳答案   
有好多问题
  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #include <math.h>

  4. #define LEN sizeof(struct ListNode)

  5. struct ListNode
  6. {
  7.     int val;
  8.     struct ListNode *next;
  9. };

  10. int n;

  11. struct ListNode*creat()
  12. {
  13.     struct ListNode*head;
  14.     struct ListNode*p1,*p2;
  15.     n=0;
  16.     p1=p2=(struct ListNode*)malloc(LEN);
  17.     scanf("%d",&p1->val);
  18.     head=NULL;
  19.     while(p1->val!=-1)
  20.     {
  21.         n=n+1;
  22.         if(n==1)head=p1;
  23.         else p2->next=p1;
  24.         p2=p1;
  25.         p1=(struct ListNode*)malloc(LEN);
  26.         scanf("%d",&p1->val);
  27.     }
  28.     p2->next=NULL;
  29.     return(head);
  30. }

  31. struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2)
  32. {
  33.     int num1=0,num2=0,num=0;
  34.     //int count=0,s=0,t;
  35.     int count=0;
  36.     struct ListNode*p,*q;
  37.     p=NULL;
  38.     q=p;
  39.     //while(l1->val!=-1)
  40.     while(l1)
  41.     {
  42.         //num1=num1+(l1->val)*(10^count);
  43.         num1 = num1 + l1->val * pow(10, count);
  44.         l1=l1->next;
  45.         count++;
  46.     }
  47.     count=0;
  48.     //while(l2->val!=-1)
  49.     while(l2)
  50.     {
  51.         /*num2=num2+(l2->val)*(10^count);*/
  52.         num2 = num2 + l2->val * pow(10, count);
  53.         l2=l2->next;
  54.         count++;
  55.     }
  56.     struct ListNode *head = NULL;
  57.     p = q = NULL;
  58.     num = num1 + num2;
  59.     do {
  60.         p = malloc(sizeof(struct ListNode));
  61.         p->next = NULL;
  62.         p->val = num % 10;
  63.         num /= 10;
  64.         if(!head) head = q = p;
  65.         else q->next = p, q = q->next;
  66.     } while(num);
  67.     return head;
  68.     /*
  69.     count=0;
  70.     num=num1+num2;
  71.     t=num;
  72.     if(num==0)
  73.     {
  74.         // 不申请内存?
  75.         p->val=0;
  76.         p->next=NULL;
  77.     }
  78.     else
  79.     {
  80.         while(t)
  81.         {
  82.             t=t/10;
  83.             s++;
  84.         }
  85.         p->val=num/(10^count)%10;
  86.         count++;
  87.         for(int i=2;i<=t;i++)
  88.         {
  89.             p=p->next;
  90.             p->val=num/(10^count)%10;
  91.             count++;
  92.         }
  93.         p->next=NULL;
  94.     }
  95.     return q;
  96.     */
  97. }


  98. void print(struct ListNode*head)
  99. {
  100.     struct ListNode*p;
  101.     p=head;
  102.     if(head!=NULL)
  103.         do
  104.         {
  105.             printf("%d ",p->val);
  106.             p=p->next;
  107.         }while(p!=NULL);
  108. }

  109. int main()
  110. {
  111.     struct ListNode *l1,*l2,*head;
  112.     l1=creat();
  113.     l2=creat();
  114.     head=addTwoNumbers(l1,l2);
  115.     print(head);
  116.     // 又不释放内存
  117.     return 0;
  118. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-7-27 23:34:04 | 显示全部楼层

请问为什么是while(l1)啊,这个链表结束的标志不应该是-1吗
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-7-27 23:46:31 | 显示全部楼层
本空逐弥 发表于 2021-7-27 23:34
请问为什么是while(l1)啊,这个链表结束的标志不应该是-1吗


是 -1 吗?那这里为什么不是 while(p != -1); 而是 while(p!=NULL);
你看一下创建函数,-1 是最后一个节点吗?

  1. void print(struct ListNode*head)
  2. {
  3.     struct ListNode*p;
  4.     p=head;
  5.     if(head!=NULL)
  6.         do
  7.         {
  8.             printf("%d ",p->val);
  9.             p=p->next;
  10.         }while(p!=NULL);
  11. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-7-28 16:53:01 | 显示全部楼层
人造人 发表于 2021-7-27 23:46
是 -1 吗?那这里为什么不是 while(p != -1); 而是 while(p!=NULL);
你看一下创建函数,-1 是最后一个 ...

了解了 -1没有作为节点的值 谢谢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-27 05:05

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表