zoukankan      html  css  js  c++  java
  • C语言数字交换

    注意特判max可能在a[0]位置,此时调换最小值后最大值下标会不准确,需要将最大值下标更新为上一步交换后的下标。

    int a[10];
    
    int main()
    {
        string line;
        while(getline(cin,line))
        {
            stringstream ss(line);
            int minIndex=0,maxIndex=0;
            for(int i=0;i<10;i++)
            {
                ss>>a[i];
                if(a[i] < a[minIndex]) minIndex=i;
                if(a[i] > a[maxIndex]) maxIndex=i;
            }
    
            swap(a[0],a[minIndex]);
    
            if(maxIndex == 0) maxIndex=minIndex;
            swap(a[9],a[maxIndex]);
    
            for(int i=0;i<10;i++)
                cout<<a[i]<<' ';
            cout<<endl;
        }
        //system("pause");
        return 0;
    }
    
  • 相关阅读:
    类加载机制与jdk智能调优命令
    初认Redis
    Spring-Cloud组件eureka
    SpringBoot入门知识
    SpringCloud
    java内存模型
    Redis
    Vue
    Nginx
    Linux系统
  • 原文地址:https://www.cnblogs.com/fxh0707/p/14412251.html
Copyright © 2011-2022 走看看