鱼C论坛

 找回密码
 立即注册
查看: 2290|回复: 2

[已解决]怎么能改一下弄成数组输出,小白试过好几次,都不行

[复制链接]
发表于 2019-5-4 01:42:02 | 显示全部楼层 |阅读模式

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

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

x
#include<stdio.h>
void main()
{
    int i,b,s,g;
    printf("The qualified three digits are:\n");
    for( i = 100; i < 1000; i++)
    {
        b = (int)(i/100);
        s = (int)((i-b*100)/10);
        g = (int)(i-b*100-s*10);
        if (((b+s)==g)&&s>=1&&g>=1)
        {
            printf("%d\t\n",i);
        }
        
    }
   
}

PS E:\Administrator\Documents\Visual Studio 2019> & 'c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.22.1\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-un1cjrtq.l25' '--stdout=Microsoft-MIEngine-Out-yrs3ajoq.p0w' '--stderr=Microsoft-MIEngine-Error-5niky0z4.qqe' '--pid=Microsoft-MIEngine-Pid-2vfwd0g1.1le' '--dbgExe=E:\MingGW\bin\gdb.exe' '--interpreter=mi'
The qualified three digits are:
112
123
134
145
156
167
178
189
213
224
235
246
257
268
279

学习进度还没到数组那边,想看一下怎么变一下能变成数组输出
最佳答案
2019-5-4 09:51:32
本帖最后由 jackz007 于 2019-5-4 10:11 编辑

        新加 200 个元素的整型数组解决楼主的问题。要注意的是,第一个元素被用来指示后续元素个数。
  1. #include<stdio.h>
  2. int main(void)
  3. {
  4.         int i , b , s , g , array[200]                                  ;
  5.         for(array[0] = 0 , i = 100 ; i < 1000 ; i ++) {
  6.                 b = i / 100                                             ;
  7.                 s = (i % 100) / 10                                      ;
  8.                 g = i % 10                                              ;
  9.                 if(b + s == g && s >= 1 && g >= 1) {
  10.                     array[array[0] + 1] = i                             ;
  11.                     array[0] ++                                         ;
  12.                 }
  13.         }
  14.         printf("The qualified three digits are:\n")                     ;
  15.         for (i = 0 ; i < array[0] ; i ++) printf("%d\n" , array[i + 1]) ;
  16. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-5-4 09:51:32 | 显示全部楼层    本楼为最佳答案   
本帖最后由 jackz007 于 2019-5-4 10:11 编辑

        新加 200 个元素的整型数组解决楼主的问题。要注意的是,第一个元素被用来指示后续元素个数。
  1. #include<stdio.h>
  2. int main(void)
  3. {
  4.         int i , b , s , g , array[200]                                  ;
  5.         for(array[0] = 0 , i = 100 ; i < 1000 ; i ++) {
  6.                 b = i / 100                                             ;
  7.                 s = (i % 100) / 10                                      ;
  8.                 g = i % 10                                              ;
  9.                 if(b + s == g && s >= 1 && g >= 1) {
  10.                     array[array[0] + 1] = i                             ;
  11.                     array[0] ++                                         ;
  12.                 }
  13.         }
  14.         printf("The qualified three digits are:\n")                     ;
  15.         for (i = 0 ; i < array[0] ; i ++) printf("%d\n" , array[i + 1]) ;
  16. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-5-4 12:00:04 From FishC Mobile | 显示全部楼层
多谢了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-7-12 23:08

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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