华南理工软件学院考研复试2016机试代码

1:程序设计题
输入个数不限的数字,以逗号为分隔,以回车键为结束,数字大小在-32768-32767之间。从小到达排序后以4个数每行输出
如:
输入:12,34,-12,23,123,56,-9,789
输出:-12 -9 12 23
34 56 128 789

#include<iostream>
#include<string>
using namespace std;
int main()
{
    char c;
    int num[100];
    int count=0;
    scanf("%d",&num[count]);
    scanf("%c",&c);
    while(c==',')
    {
        scanf("%d",&num[++count]);
        scanf("%c",&c);
    }
    count=count+1;
    for(int i=0;i<count-1;i++)
    {
        for(int j=0;j<count-i-1;j++)
        {
            if(num[j]>num[j+1])
            {
                int temp=num[j];
                num[j]=num[j+1];
                num[j+1]=temp;
            }
        }
    }
    int n=1;
    for(int j=0;j<count;j++)
    {
        if(n%4==0)
        {
            cout<<num[j];
            cout<<endl;
        }
        else
        {
            cout<<num[j]<<" ";
        }
        n++;    
    }
    system("pause");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qiqi123i/article/details/79642730
今日推荐