CLRS 2.1代码c++实现

例题:
/*这是降序,升序只需将while循环改成:
while(key<a[i])
*/
#include "stdafx.h"
#include<iostream>
using namespace std;
void insertSort(int a[], int length)
{
    int i = 0;
    int j, key;
    for (j = 1; j < length; j++)
    {
        key = a[j];
        i = j - 1;
        while (key > a[i])
        {
            int tmp;
            tmp = a[i + 1];
            a[i + 1] = a[i];
            a[i] = tmp;
            i = i - 1;
            if (i < 0){ break; }
        }
    }
}
int _tmain(int argc, _TCHAR* argv[])
{
    int a[7] = { 1, 4, 5, 22, 33, 44, 55 };
    insertSort(a, 7);
    int i;
    for (i = 0; i < 7; i++)
    {
        cout << a[i] << endl;
    }
    return 0;
}

#include "stdafx.h"
#include<iostream>
using namespace std;
int searchQuestion(int a[],int length,int v)
{
    int i, key=0;
    for (i = 0; i < length; i++)
    {
        if (v == a[i]){
            cout << v << endl;
            key++;
        }
    }
    return key;
}
int _tmain(int argc, _TCHAR* argv[])
{
    int a[10] = { 3, 3, 4, 5, 6, 7, 2, 33, 55 };
    int v;

    while (cin >> v){
        int value = searchQuestion(a, 10, v);
        if (value == 0){
            cout << "NIT" << endl;
        }
    }
    return 0;
}

/*
CLRS 2.1.4
*/
#include "stdafx.h"
#include<iostream>
using namespace std;
void convert(int a[], int n)
{
    int i;
    int temp;
    for (i = 0; i<n / 2; i++)
    {
        temp = a[i];
        a[i] = a[n - i - 1];
        a[n - i - 1] = temp;
    }
}
int *sum(int a[], int lengtha, int b[], int lengthb)
{
    convert(a, lengtha);
    convert(b, lengthb);
    int lengthc = lengtha>lengthb ? lengtha : lengthb;
    lengthc += 1;
    int *c = new int[lengthc];
    memset(c, 0, lengthc);
    int i, key = 0;
    for (i = 0; i<lengthc; i++)
    {
        if (lengtha <= i)
        {
            if (lengthb>i)
            {
                c[i] = b[i] + key;
                if (c[i] >= 2)
                {
                    c[i] %= 2;
                    key = 1;
                }
                else
                {
                    key = 0;
                }
            }
            else
            {
                c[i] = key;
            }
        }
        else if (lengtha>i)
        {
            if (lengthb>i)
            {
                c[i] = a[i] + b[i] + key;
            }
            else
            {
                c[i] = a[i] + key;
            }
            if (c[i] >= 2)
            {
                c[i] %= 2;
                key = 1;
            }
            else
            {
                key = 0;
            }
        }
    }
    return c;
}
int main()
{
    int a[10] = { 1, 0, 1, 0, 1, 1, 0, 0, 1, 1 };
    int    b[9] = { 1, 0, 0, 0, 1, 0, 0, 0, 0 };
    int *c;
    int i, key;
    c = sum(a, 10, b, 9);
    for (i = 10; i >= 0; i--)
    {
        if (c[i] != 0)
        {
            key = i;
            break;
        }
    }
    for (i = key; i >= 0; i--)
    {
        cout << c[i] << " ";
    }
    return 0;
}  

注:2.1.4摘自CSDN某博客,但不记得哪个了

时间: 2024-08-03 11:04:27

CLRS 2.1代码c++实现的相关文章

错误和问题解决的成本

问题描写叙述 错误 数据收集 根本原因 版本号   组件:数据修复           在一个实际成本组织中,(平均,先进先出,后进先出) 一个或更 多的下面情况可能发生: 1.导航到物料成本历史表单上的数量信息,与现有量表单的数量不匹配的记录 2. 一些物料前期已计成本的数量与前面的事务处理历史表单的数量不匹配 3. 全部的库存值报表与事务处理值报表不匹配 4. 存货层次成本更新表单的总数量与现有量数量表单不匹配(只在先进先出/后进先出) 5.这些症状的不论什么一个意味着 MMT-CQL不匹配

《算法导论》(CLRS)第三版 第1、2章总结(二)

5. 归并排序 (Merge Sort) 1 #include <stdio.h> 2 #include <stdlib.h> 3 4 void print(int arr[], int n) { 5 int i; 6 for (i = 0; i < n; i++) { 7 printf("%d ", arr[i]); 8 } 9 printf("\n"); 10 } 11 12 void merge(int arr[], int s,

[CLRS][CH 15.5]最优二叉查找树

背景铺垫 假设我们正在设计一个翻译程序,讲英语翻译成法语,需要用一棵BST存储文章中出现的单词及等价的法语.因为要频繁地查找这棵树,所以我们希望查找时间越短越好.当然我们可以考虑使用红黑树,或者可能更适用的伸展树,来实现这一操作.但是这仍然不能满足我们的需要:由于单词出现频率不同,如果mycophagist这种奇怪的单词出现在根节点,而the.a.is这些常用单词出现在叶节点,即使O(lgn)的查找速度也极大的浪费了时间. 因此我们需要这样一种BST:频繁出现的单词出现在离根节点较近的地方.假设

Xcode 快速开发 代码块 快捷键

Xcode的代码片段(Code Snippets)创建自定义的代码片段,当你重用这些代码片段时,会给你带来很大的方便. 常用的: 1.strong:@property (nonatomic,strong) <#Class#> *<#object#>;2.weak:@property (nonatomic,weak) <#Class#> *<#object#>;3.copy:@property (nonatomic,copy) NSString *<#s

solr分布式索引【实战一、分片配置读取:工具类configUtil.java,读取配置代码片段,配置实例】

1 private static Properties prop = new Properties(); 2 3 private static String confFilePath = "conf" + File.separator + "config.properties";// 配置文件目录 4 static { 5 // 加载properties 6 InputStream is = null; 7 InputStreamReader isr = null;

微信支付PHP SDK —— 公众号支付代码详解

在微信支付 开发者文档页面 下载最新的 php SDK http://mch.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=11_1 这里假设你已经申请完微信支付 1. 微信后台配置  如图 我们先进行测试,所以先把测试授权目录和 测试白名单添加上.测试授权目录是你要发起微信请求的哪个文件所在的目录. 例如jsapi 发起请求一般是jsapi.php所在目录 为测试目录,测试白名单即开发人员的微信号. 正式的支付授权目录不能和测试的一样否则会报错.不填

如何上传代码到github?

如何上传代码到github? 首先你需要一个github账号,所有还没有的话先去注册吧! https://github.com/ 我们使用git需要先安装git工具,这里给出下载地址,下载后一路直接安装即可: https://git-for-windows.github.io/ 1.进入Github首页,点击New repository新建一个项目  2.填写相应信息后点击create即可 Repository name: 仓库名称 Description(可选): 仓库描述介绍 Public,

自己写的代码会写了,还是太渣,复杂度什么的直接报表

原题目是这样子的,本人按照一贯的作风想得很简单 #include<iostream> using namespace std; void H(int n) { while (n > 1) { if (n % 2 == 0) //这一步把取余和除法弄混淆. { cout << n << " "; n = n / 2; } else { cout << n << " "; n = 3 * n + 1; }

category is in invalid format hint微信第三方平台将第三方提交的代码包提交审核出错

微信第三方平台通过接口https://api.weixin.qq.com/wxa/submit_audit?access_token=TOKEN将第三方提交的代码包提交审核时一直返回错误码85008的错误信息: category is in invalid format hint 查了半天是接口提交数据时json_encode时中文不能编码提交 将向微信的提交代码     $this->https_post($url,json_encode($postData)); 改为     $this->