鱼C论坛

 找回密码
 立即注册
查看: 630|回复: 3

[已解决]不知道哪里错了

[复制链接]
发表于 2020-12-16 09:34:41 | 显示全部楼层 |阅读模式

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

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

x
输入一个正整数repeat (0<repeat<10),做repeat次下列运算:
输入一个正整数n (1<n≤10),再输入n个整数,输出最大值极其下标(设最大值惟一,下标从0开始)。
例:括号内是说明
输入
3   (repeat=3)
3 1 6 4
3 10 8 1
5 1 2 5 4 0
输出
max=6,index=1    (最大值6的下标是1)
max=10,index=0   (最大值10的下标是0)
max=5,index=2    (最大值5的下标是2)

#include <stdio.h>
int main()
{
        int a[10];
        int g;
        int w,k,l[10],q[10],o;
        scanf("%d",&g);
        for (int i=0;i<g;i++)
        {
                scanf("%d",k);
                for(w=0;w<k;w++)
                {
                scanf("%d",&a[w]);
            if(w=1)
            {
                    if(a[w]>a[w-1])
                    l[i]=a[w];
                    else
                    l[i]=a[w-1];
                }
            if(w>1)
            {
                    if(a[w]>l[i])
                    {l[i]=a[i];
                        q[i]=w;
                        }
                }
                        }
                        }
                for(o=0;o<g;o++)
                {
                        printf("max=%d,index=%d\n",l[o],q[0]);
                }
       
}
最佳答案
2020-12-16 10:40:38
  1. #include <stdio.h>
  2. int main()
  3. {
  4.         int a[10];
  5.         int i,j,k;
  6.         int repeat, n, max, index;
  7.         scanf("%d", &repeat);         // 重复次数
  8.         for (i = 0;i < repeat;i++)
  9.         {
  10.                 printf("请输入数据的个数:");
  11.                 scanf("%d", &n);          // 输入数据个数
  12.                 for (j = 0;j < n;j++)
  13.                 {
  14.                         scanf("%d", &a[j]);   // 输入数据
  15.                         max = a[0];           // 假设第1个就是最大值
  16.                         index = 0;            // 记录最大值的下标
  17.                         for (k = 1;k < n;k++)
  18.                         {
  19.                                 if (a[k] > max)
  20.                                 {
  21.                                         max = a[k];   // 将大值记为新的最大值
  22.                                         index = k;    // 记录新的最大值的下标
  23.                                 }
  24.                         }
  25.                 }
  26.                 printf("max = %d,index = %d\n", max, index + 1);
  27.         }
  28. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-12-16 10:21:52 | 显示全部楼层
你的变量设置的,看得我头晕,我重新帮你写程序吧
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-12-16 10:29:25 | 显示全部楼层
本帖最后由 jackz007 于 2020-12-16 10:31 编辑
  1. #include <stdio.h>
  2. int main()
  3. {
  4.         int a[10][10] , b[10] , g , i , j , k                                ;
  5.         scanf("%d", & g)                                                     ;
  6.         for (i = 0 ; i < g ; i ++) {
  7.                 scanf("%d" , & b[i])                                         ;
  8.                 for(j = 0 ; j < b[i] ; j ++) scanf("%d" , & a[i][j])         ;
  9.         }
  10.         for(i = 0 ; i < g ; i ++) {
  11.                 for(k = j = 0 ; j < b[i] ; j ++) if(a[i][j] > a[i][k]) k = j ;
  12.                 printf("max = %d , index = %d\n" , a[i][k] , k)              ;
  13.         }
  14. }
复制代码

        编译、运行实况
  1. D:\0002.Exercise\C>cl x.c
  2. 用于 x86 的 Microsoft (R) C/C++ 优化编译器 19.28.29334 版
  3. 版权所有(C) Microsoft Corporation。保留所有权利。

  4. x.c
  5. Microsoft (R) Incremental Linker Version 14.28.29334.0
  6. Copyright (C) Microsoft Corporation.  All rights reserved.

  7. /out:x.exe
  8. x.obj

  9. D:\0002.Exercise\C>x
  10. 3
  11. 3 1 6 4
  12. 3 10 8 1
  13. 5 1 2 5 4 0
  14. max = 6 , index = 1
  15. max = 10 , index = 0
  16. max = 5 , index = 2

  17. D:\0002.Exercise\C>
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-12-16 10:40:38 | 显示全部楼层    本楼为最佳答案   
  1. #include <stdio.h>
  2. int main()
  3. {
  4.         int a[10];
  5.         int i,j,k;
  6.         int repeat, n, max, index;
  7.         scanf("%d", &repeat);         // 重复次数
  8.         for (i = 0;i < repeat;i++)
  9.         {
  10.                 printf("请输入数据的个数:");
  11.                 scanf("%d", &n);          // 输入数据个数
  12.                 for (j = 0;j < n;j++)
  13.                 {
  14.                         scanf("%d", &a[j]);   // 输入数据
  15.                         max = a[0];           // 假设第1个就是最大值
  16.                         index = 0;            // 记录最大值的下标
  17.                         for (k = 1;k < n;k++)
  18.                         {
  19.                                 if (a[k] > max)
  20.                                 {
  21.                                         max = a[k];   // 将大值记为新的最大值
  22.                                         index = k;    // 记录新的最大值的下标
  23.                                 }
  24.                         }
  25.                 }
  26.                 printf("max = %d,index = %d\n", max, index + 1);
  27.         }
  28. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-28 14:46

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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