数据插入到有序数组(难度:半颗星)

问题描述:

输入一个有序的整数序列(从小到大排列),然后再输入一个需要插入到数组中的整数,输出插入数据之前和之后的数列。(要保证插入数据之后数列仍然是从小到大排列)。

例如:
输入数列: 1 2 4 5
待插入数据: 3

插入数据后的数据: 1 2 3 4 5

参考代码:

#include <stdio.h>

int arr[100];

int main()
{
    char ch = ' ';
    int i, num, nCount = 0;

    printf("输入一个数列: ");
    while (ch != '\n')
    {
        scanf_s("%d", &arr[nCount++]);
        ch = getchar();
    }

    printf("输入一个要插入的数: ");
    scanf_s("%d", &num);

    printf("插入数据前:\n");
    for (i = 0; i < nCount; i++)
        printf("%d ", arr[i]);
    printf("\n");

    for (i = nCount - 1; i >= 0 && num < arr[i]; i--)
        arr[i + 1] = arr[i];
    arr[i + 1] = num;//把数放到正确的位置
    nCount++;//数组长度+1

    printf("\n插入数据前:\n");
    for (i = 0; i < nCount; i++)
        printf("%d ", arr[i]);
    printf("\n");

    return 0;
}

运行结果:

这里写图片描述

难度级别5星

11-01

我安装完sniffer抓包工具后,WIN2K系统每次在接近出现用户登录界面的时候(通常服务和驱动也在这个时候加载起来)就自动重启,用安全模式也时快进去的时候就自动重启rnrn特点1:rn1。我前2天写了一个服务,机器启动的时候会core,我没来得及删去rn2。下面是我安装的very famous的sniffer软件相应的.inf文件(哪位大龙虾是否能看出问题?)rn[Identification]rn OptionType = NetServicern[Options]rn SNIFFERrn[FileConstants]rnUtilityInf = "UTILITY.INF"rnsubroutineinf = "SUBROUTN.INF"rnSoftwareType = "service"rnExit_Code = 0rnNetEventDLL = "%SystemRoot%\System32\netevent.dll"rnManufacturer = "Network Associates, Inc."rnProductMajorVersion = "2"rnProductMinorVersion = "04"rnProductVersion = $(ProductMajorVersion)"."$(ProductMinorVersion)rnProductSoftwareName = "Sniffer"rnProductSoftwareImagePath = "\SystemRoot\System32\drivers\sniffer.sys"rnrnNetRuleSoftwareType = "lmNetService ndisTransport rasCapableTransport ndisWanTransport"rnNetRuleSoftwareClass = "rasCapableTransport netBiosTransport"rnrnNetRuleSoftwareUse = $(SoftwareType)" yes yes"rnNetRuleSoftwareBindForm = """Sniffer"" yes yes simple"rnrnProductKeyName = $(!NTN_SoftwareBase)"\"$(Manufacturer)"\"$(ProductSoftwareName)"\CurrentVersion"rnParamKeyName = $(!NTN_ServiceBase)"\"$(ProductSoftwareName)"\Parameters"rn[GeneralConstants]rnfrom = ""rnto = ""rnExitCodeOk = 0rnExitCodeCancel = 1rnExitCodeFatal = 2rnKeyNull = ""rnMAXIMUM_ALLOWED = 33554432rnRegistryErrorIndex = NO_ERRORrnKeyProduct = ""rnKeyParameters = ""rnTRUE = 1rnFALSE = 0rnNoTitle = 0rnExitState = "Active"rnOldVersionExisted = $(FALSE)rnDriverPath = $(!STF_NTPATH)\driversrn[date]rn Now = ? $(!LIBHANDLE) GetSystemDatern[Identify]rn read-syms Identificationrn set Status = STATUS_SUCCESSFULrn set Identifier = $(OptionType)rn set Media = #("Source Media Descriptions", 1, 1)rn Return $(Status) $(Identifier) $(Media)rn[ReturnOptions]rn set Status = STATUS_FAILEDrn set OptionList = rn set OptionTextList = rn set LanguageList = ^(LanguagesSupported, 1)rn Ifcontains(i) $($0) in $(LanguageList)rn goto returnoptionsrn elsern set Status = STATUS_NOLANGUAGErn goto finish_ReturnOptionsrn endifrnreturnoptions = +rn set OptionList = ^(Options, 1)rn set OptionTextList = ^(OptionsText$($0), 1)rn set Status = STATUS_SUCCESSFULrnfinish_ReturnOptions = +rn Return $(Status) $(OptionList) $(OptionTextList)rn[InstallOption]rn set Option = $($1)rn set SrcDir = $($2)rn set AddCopy = $($3)rn set DoCopy = $($4)rn set DoConfig = $($5)rn set LanguageList = ^(LanguagesSupported, 1)rn Ifcontains(i) $($0) NOT-IN $(LanguageList)rn Return STATUS_NOLANGUAGErn endifrn Debug-Output "OEMNXPNB.INF: STF_CWDDIR is: "$(!STF_CWDDIR)rn Debug-Output "OEMNXPNB.INF: STF_LANGUAGE is: "$(!STF_LANGUAGE)rn set-subst LF = "\n"rn read-syms GeneralConstantsrn read-syms FileConstantsrn read-syms DialogConstants$(!STF_LANGUAGE)rn ifstr(i) $(!NTN_Origination) == "NCPA"rn set Continue = $(OK)rn endifrn read-syms FileConstants$(!STF_LANGUAGE)rn detect datern set-title $(FunctionTitle)rn set to = Beginrn set from = Beginrn set CommonStatus = STATUS_SUCCESSFULrn EndWaitrnBegin = +rn Ifstr(i) $(!NTN_InstallMode) == deinstallrn set StartLabel = removeadapterrn else-Ifstr(i) $(!NTN_InstallMode) == Updatern set StartLabel = UpgradeSoftwarern else-Ifstr(i) $(!NTN_InstallMode) == bindrn set StartLabel = bindingadapterrn else-Ifstr(i) $(!NTN_InstallMode) == configurern Shell $(UtilityInf),RegistryErrorString,CANNOT_CONFIGURE_SOFTWARErn ifint $($ShellCode) != $(!SHELL_CODE_OK)rn Debug-Output "OEMNXPNB.INF: ShellCode error: cannot get an error string."rn goto ShellCodeErrorrn endifrn set Error = $($R0)rn set from = endrn set to = endrn goto nonfatalinforn elsern set StartLabel = installadapter

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试