鱼C论坛

 找回密码
 立即注册
查看: 2901|回复: 7

[已解决]数组问题

[复制链接]
发表于 2022-9-24 17:07:21 | 显示全部楼层 |阅读模式

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

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

x
怎么把一系列数存入数组中?(输入时数字的数量是随机的)(如输入:1 -5 9 6 3 7 5 2 6)
求助!!!
最佳答案
2022-9-24 18:39:20
jackz007 发表于 2022-9-24 17:15
编译、运行实况:

        按下  或者  - z 结束输入

用了层主的代码,介意我删
  1. #include <stdio.h>

  2. int main(void)
  3. {
  4.         int d[1000] , i , n,ch;                               ;
  5.         for(n = 0 ; '\n'!=(ch=getchar()); n ++)
  6.         {
  7.             ungetc(ch,stdin);
  8.             scanf("%d" , & d[n]);
  9.         }
  10.         printf("%d" , d[0])                               ;
  11.         for(i = 1 ; i < n ; i ++) printf("\t%d" , d[i])   ;
  12.         printf("\n")                                      ;
  13. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-9-24 17:15:41 | 显示全部楼层
本帖最后由 jackz007 于 2022-9-24 17:16 编辑
  1. #include <stdio.h>

  2. int main(void)
  3. {
  4.         int d[1000] , i , n                               ;
  5.         for(n = 0 ; (scanf("%d" , & d[n])) != EOF ; n ++) ;
  6.         printf("%d" , d[0])                               ;
  7.         for(i = 1 ; i < n ; i ++) printf("\t%d" , d[i])   ;
  8.         printf("\n")                                      ;
  9. }
复制代码

        编译、运行实况:
  1. D:\[00.Exerciese.2022]\C>g++ -o x x.c

  2. D:\[00.Exerciese.2022]\C>x
  3. 1
  4. -5
  5. 9
  6. 6
  7. 3
  8. 7
  9. 5
  10. 2
  11. 6
  12. ^Z
  13. 1       -5      9       6       3       7       5       2       6

  14. D:\[00.Exerciese.2022]\C>
复制代码

        按下 <F6> 或者 <Ctrl> - z 结束输入

评分

参与人数 1荣誉 +1 鱼币 +2 收起 理由
jhq999 + 1 + 2 一人一半

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2022-9-24 18:08:50 | 显示全部楼层
本帖最后由 jhq999 于 2022-9-24 18:12 编辑

想简单了
  1. #include <stdio.h>
  2. #define N 100
  3. int main()
  4. {
  5.     int num[N]={0};
  6.     int  i=0,ch=0,len=0,flag=1;

  7.     while((ch=getchar())!='\n'&&i<N)
  8.     {
  9.         if(ch>='0'&&ch<='9')num[i++]=(ch-'0')*flag,flag=1;
  10.         else if('-'==ch)flag=-1;
  11.     }
  12.     for(len=i,i=0;i<len;i+=1)printf("%d ",num[i]);

  13.     return 0;
  14. }

复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2022-9-24 18:21:03 | 显示全部楼层
  1. #include <stdio.h>
  2. #define N 100
  3. int main()
  4. {
  5.     int num[N]= {0};
  6.     int  i=0,ch=0,len=0,flag=1;

  7.     while((ch=getchar())!='\n'&&i<N)
  8.     {
  9.         if(ch>='0'&&ch<='9')
  10.         {
  11.             num[i]=num[i]*10+(ch-'0');

  12.         }
  13.         else if('-'==ch)flag=-1;
  14.         else if(0x20==ch)
  15.         {
  16.             num[i]*=flag;
  17.             flag=1;
  18.             i+=1;
  19.         }
  20.     }
  21.     num[i++]*=flag;
  22.     for(len=i,i=0; i<len; i+=1)printf("%d ",num[i]);

  23.     return 0;
  24. }
复制代码
  1. 1 -51 09 6 3 117 55 2 699
  2. 1 -51 9 6 3 117 55 2 699
  3. Process returned 0 (0x0)   execution time : 1.366 s
  4. Press any key to continue.
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2022-9-24 18:39:20 | 显示全部楼层    本楼为最佳答案   
jackz007 发表于 2022-9-24 17:15
编译、运行实况:

        按下  或者  - z 结束输入

用了层主的代码,介意我删
  1. #include <stdio.h>

  2. int main(void)
  3. {
  4.         int d[1000] , i , n,ch;                               ;
  5.         for(n = 0 ; '\n'!=(ch=getchar()); n ++)
  6.         {
  7.             ungetc(ch,stdin);
  8.             scanf("%d" , & d[n]);
  9.         }
  10.         printf("%d" , d[0])                               ;
  11.         for(i = 1 ; i < n ; i ++) printf("\t%d" , d[i])   ;
  12.         printf("\n")                                      ;
  13. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-9-24 18:46:01 | 显示全部楼层
jhq999 发表于 2022-9-24 18:39
用了层主的代码,介意我删

          没事,别客气!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-9-24 21:58:23 | 显示全部楼层
  1. #include <stdio.h>

  2. #define MAXS 10000

  3. int main() {
  4.         int a[MAXS], i = -1, x;
  5.         while (scanf("%d", &x))
  6.                 a[++i] = x;
  7.         printf("输入完毕");
  8.         return 0;
  9. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2022-9-26 15:23:51 | 显示全部楼层
谢谢各位!!问题解决了!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-23 23:56

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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