你好,我是普林斯特。

计算机的所有东西都是人做出来的,别人能想得出来的,我也一定能想得出来,在计算机里头,没有任何黑魔法,所有的东西,只不过是我现在不知道而已,总有一天,我会把所有的细节,所有的内部的东西全都搞明白的。       ----翁恺老师

愿你成为部落最强大的战士…                                                  

问题 1025: [编程入门]数组插入处理

问题 1025: [编程入门]数组插入处理

时间限制: 1Sec 内存限制: 128MB 提交: 6529 解决: 4091

题目描述
已有一个已排好的9个元素的数组,今输入一个数要求按原来排序的规律将它插入数组中。
输入
第一行,原始数列。 第二行,需要插入的数字。
输出
排序后的数列
样例输入
1 7 8 17 23 24 59 62 101
50
样例输出
1
7
8
17
23
24
50
59
62
101
思路:
//如果顺序按从小到大,那么如果我比这个元素小,那么我应该放到它前面 
//且如果顺序按从小到大,如果我比这些元素都大,那么我应该放在最后

//如果顺序按从大到小,那么如果我比这个元素大,那么我也应该放到它前面 
//且如果顺序按从大到小,如果我比这些元素都小,那么我应该放在最后

 

 
#include<iostream>
using namespace std;
int main()
{
//    做题步骤: get the array,then compare the relationship of the first factor and the second, 
//    and they are processing differently.
//take care, this messure only suits to no equal situation. 
//  Ok,Here we go
    int array[10];
    for(int i=0;i<9;i++)
    {
        cin>>array[i];
    }
    int temporary;
    cin>>temporary; 
    if(array[0]<array[8])
    {
        for(int i=0;i<9;i++)
        {
            if(temporary<array[i])
            {
                for(int j=9;j>i;j--)
                {
                    array[j]=array[j-1];
                }
                array[i]=temporary;
                break;
            }
            else if(i==8)
            {
                array[9]=temporary;
            }
        }
    } 
    else
    {
        for(int i=0;i<9;i++)
        {
            if(temporary>array[i])
            {
                for(int j=9;j>i;j--)
                {
                    array[j]=array[j-1];
                }
                array[i]=temporary;
                break;
            }
            else if(i==8)
            {
                array[9]=temporary;
            }
        }
    } 
    for(int i=0;i<10;i++)
    {
        cout<<array[i]<<endl;
    }
    return 0;
 } 

 

#include<iostream>
using namespace std;
int main()
{
    //空间换时间
    int array[21];
    for(int i=1;i<19;i+=2)
    {
        cin>>array[i];
    } 
    int temporary;
    cin>>temporary; 
    int singal;
    if(array[0]<array[17])
    {
        for(int i=1;i<19;i+=2)
        {
            if(temporary<array[i])
            {
            /*    for(int j=9;j>i;j--)
                {
                    array[j]=array[j-1];
                }
            */
                array[i-1]=temporary;//如果顺序按从小到大,那么如果我比这个元素小,那么我应该放到它前面 
                singal=i-1;
                break;
            }
            else if(i==17)// if temp is the bigest
            {
                array[18]=temporary;//而如果顺序按从小到大,如果我比这些元素都大,那么我应该放在最后
                singal=18;
                break;
            }
        }
    } 
    else
    {
        for(int i=1;i<19;i+=2)
        {
            if(temporary>array[i])
            {
            /*    for(int j=9;j>i;j--)
                {
                    array[j]=array[j-1];
                }
            */
                array[i-1]=temporary;//如果顺序按从大到小,那么如果我比这个元素大,那么我也应该放到它前面 
                singal=i-1;
                break;
            }
            else if(i==17)// if temp is the least
            {
                array[18]=temporary;//而如果顺序按从大到小,如果我比这些元素都小,那么我应该放在最后
                singal=18;
                break;
            }
        }
    } 
    for(int i=1;i<19;i+=2)
    {
        if(singal==i-1||(singal==18&&i==17))
            cout<<array[i-1]<<endl;
        cout<<array[i]<<endl;
    }
    return 0;
 } 

 

#include<iostream>
using namespace std;
int main()
{
//    做题步骤: get the array,then compare the relationship of the first factor and the second, 
//    and they are processing differently.
//take care, this messure only suits to no equal situation. 
//  Ok,Here we go
//继续优化,其实比较的时候直接输出就可以,可以少走一步循环输出,而且不用再用循环来调换数组位置
//                                                                    当然这是不用数据存储的情况 
//这样一种优化,也不用从跟上一次优化那样用空间换时间 
    int array[10];
    for(int i=0;i<9;i++)
    {
        cin>>array[i];
    }
    int temporary;
    cin>>temporary; 
    int signal=1;
    if(array[0]<array[8])
    {
        for(int i=0;i<9;i++)
        {
            if(temporary<array[i]&&signal==1)//加个signal就是保证temporary只输出一次 
            {
                cout<<temporary<<endl;
                signal=0;
            }
            else if(i==8&&signal==1)//这里是要确保,到了最后temporary还未输出过才会进入到条件输出中 
            {
                cout<<array[i]<<endl;
                cout<<temporary<<endl;
                break;
            }
            cout<<array[i]<<endl;
        }
    } 
    else
    {
        for(int i=0;i<9;i++)
        {
            if(temporary>array[i]&&signal==1)
            {
                cout<<temporary<<endl;
                signal=0;
            }
            else if(i==8&&signal==1)
            {
                cout<<array[i]<<endl;
                cout<<temporary<<endl;
                break;
            }
            cout<<array[i]<<endl;
        }
    }
    return 0;
 } 

 

posted @ 2019-10-02 01:10  珞珈山  阅读(360)  评论(0)    收藏  举报