zoukankan      html  css  js  c++  java
  • 机试题201702x--不定长数组的输入

    题目描述:

    输入n组数据,对每一组数据进行输入,排序后输出

    Input:

    2

    1 4 2 7 5

    8 9 4 3

    Output:

    1 2 4 5 7

    3 4 8 9

    如题,需要输入数据长度不定的数组,并对其进行排序

    解决方法:

    每次输入时,数据与数据之间用空格分隔,而cin并不读入空格,因此使用cin.get()方法,遇到非空格,即换行,就会停止输入数组

            while(cin>>a[i++])//cin不读空格制表符
            {
                c=cin.get();
                if(c!=32) break;
            }

    完整代码:

    #include<iostream>
    #include<algorithm>
    #define maxn 20
    using namespace std;
    
    int main(){
        int n,a[maxn],i,temp,c;
        cin>>n;
        while(n--)
        {
            i=0;
            while(cin>>a[i++])//cin不读空格制表符等
            {
                c=cin.get();
                if(c!=32) break;
            }
            //直接插入排序
            for(int m=1;m<i;m++)
            {
                for(int n=0;n<m;n++)
                {
                  if(a[n]>a[m])
                  {
                      temp=a[n];
                      a[n]=a[m];
                      a[m]=temp;
    
                  }
                }
            }
            for(int k=0;k<i;k++)
                cout<<a[k]<<" ";
            cout<<endl;
        }
        return 0;
    }
    View Code
  • 相关阅读:
    COM组件
    【游戏引擎架构】入门(一)
    UNICODE字符串
    Python随笔10
    Python随笔9-函数
    Python随笔7
    Python随笔6
    Python随笔5
    Python随笔4
    Python随笔3
  • 原文地址:https://www.cnblogs.com/helloworldToDu/p/12741424.html
Copyright © 2011-2022 走看看