输入一个无序的数组,按照一定的方式输出结果, 要求两个数的相加和为10

//输入一个无序的数组 {3, 7, 8, 2, 6, 7, 5, 9, 9, 1} ,请按照如下的方式输出结果, 要求两个数的相加和为10 .比如  9,1 ; 9; 8,2 ; 7, 3; 7; 6

#include "stdafx.h"

#include <stdlib.h>

void SumTenFunc(int *number, int length)
{
    if (number == NULL || length <= 0)
    {
        return;
    }
    //顺序排序
    int flog = 0;
    for (int i = 0; i <= length - 1; ++i)
    {
        flog = 0;
        for (int j = length - 1; j >= i; j--)
        {
            if(number[j] < number[j - 1])
            {
                int temp = number[j - 1];
                number[j - 1] = number[j];
                number[j] = temp;
                flog = 1;
            }
        }
        if(1 == flog)
        {
            continue;
        }
    }
    int low = 0;
    int high = length - 1;
    while(low < high)
    {
        //小于等于 10 的 同时相加  当两个数之和小于10的时候 ,如果只是
        if (number[low] + number[high] == 10)
        {
            printf("%d, %d  \n", number[low], number[high]);
            //先中间移动
            ++low;
            --high;
        }
        else if (number[low] + number[high] > 10)
        {
            printf("%d \n", number[high]);
            --high;
        }
        else if (number[low] + number[high] < 10)
        {
            //printf("%d %d \n", number[high], number[low]);
            ++low;
        }
    }
}

void Test()
{
    //int array1[] = {3, 7, 8, 2, 6, 7, 5, 9, 9, 1};
    //int array1[] = {1, 2, 3, 4, 5, 6, 7, 9};
    int array1[] = {1, 2, 3, 3, 4, 5, 6, 7, 9};
    SumTenFunc(array1, sizeof(array1)/ sizeof(int));
}

int _tmain(int argc, _TCHAR* argv[])
{
    Test();
    system("pause");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值