本文介绍了寻找从标准输入第二个最大的元素,而无需使用数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 #包括LT&;&stdio.h中GT;诠释主(){
    INT I,M1,M2,N,NUM;
    看跌期权(\\ n);
    scanf函数(%d个,&安培; N);
    对于(i = 0; I< N;我++){
         scanf函数(%D,试验#);
         如果(我== 0){
              NUM = M1 = 2;
         }
         如果(NUM> M1){
              M2 = M1;
              M1 = NUM​​;
         }否则如果(NUM> 2){
              M2 = NUM​​;
         }
     }
    返回0;
}

我的标准输入: -950 -588 -169 -187 -445 400 -1

我得标准输出: -169 但它的显示标准输出: \\ n

请注意:我想不阵列来解决这个问题。


解决方案

  / *程序发现的第二大数字,而不使用数组* /
        主要()
        {
         INT NUM,大型= 0,slarge = 0,I = 0;
         clrscr();
         的printf(输入号码:);
         而(ⅰ小于10)
         {
          scanf函数(%D,试验#);
          如果(我== 0)
          {
          大= NUM​​;
          }
          否则,如果(NUM>大)
          {
           slarge =大;
           大= NUM​​;
          }
          否则,如果(NUM> slarge)
          {
           slarge = NUM​​;
          }
          我++;
         }
         的printf(大型数:%d,大);
         的printf(\\ n第二大=%d个,slarge);
         残培();
         返回0;
        }
#include<stdio.h>

int main() {
    int i, m1, m2, n, num;
    puts("\n");
    scanf("%d",&n);
    for(i = 0; i < n; i++) {
         scanf("%d", &num);
         if(i == 0) {
              num = m1 = m2;
         }
         if(num > m1) {
              m2 = m1; 
              m1 = num; 
         } else if(num > m2) {
              m2 = num;
         }
     }
    return 0;
}

my stdin: -950 -588 -169 -187 -445 400 -1

I have to get stdout: -169 but its showing stdout: \n

Note: I want to solve this problem without arrays.

解决方案
/* Program to find the second largest number without using array */
        main()
        {
         int num,large=0,slarge=0,i=0;
         clrscr();
         printf("Enter the number:");
         while(i<10)
         {
          scanf("%d",&num);
          if(i==0)
          {
          large=num;
          }
          else if(num>large)
          {
           slarge=large;
           large=num;
          }
          else if(num>slarge)
          {
           slarge=num;
          }
          i++;
         }
         printf("Large number:%d",large);
         printf("\nSecond large=%d",slarge);
         getch();
         return 0;
        }

这篇关于寻找从标准输入第二个最大的元素,而无需使用数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 05:55