【算法(Algorithms)第4版 练习 1.3.20】教程文章相关的互联网学习教程文章

【算法练习题】力扣数组练习题(1):盛最多水的容器【代码】【图】

原题说明: 给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) 。在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0)。找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。 说明:你不能倾斜容器,且 n 的值至少为 2。 原题链接:https://leetcode-cn.com/problems/container-with-most-water 解法一:暴力求解 拿到这道题的第一个想法就是暴力求解。设定两层循环,第一层用...

算法练习-4-开根号-递归/非递归方式【代码】【图】

1 #include <stdio.h>2 3 float fabs(float x)4 {5 return x>=0 ? x:(-1)*x;6 }7 8 float sqrt1(float a,float p,float e)//求根号a,p是根号a的大致近似(可以令p=a/2),e是结果允许误差(取0.00001)9 { 10 while(fabs(p*p-a)>=e) 11 p=(p+a/p)/2; 12 return p; 13 } 14 15 float sqrt2(float a,float p,float e)//递归方式 16 { 17 if(fabs(p*p-a)<e) 18 return p; 19 else 20 r...

排序算法练习之选择排序【代码】

练习练习练习!!!import java.util.Arrays;/*** @Date: 2019-11-24 09:20* @King: No blood!No bone!No ash!!!*/ public class TestSelectionSort {public static void main(String[] args) {/*** 定义数组*/int [] arr = new int[7];arr[0] = 31;arr[1] = 13;arr[2] = 21;arr[3] = 33;arr[4] = 10;arr[5] = 25;arr[6] = 11;/*** 输出排序前*/System.out.println("排序前:"+ Arrays.toString(arr));/*** 调用两种选择排序方法,并...

算法练习2(n个数挑选k个数的不同情况集合)

n个数挑选k个数的不同情况集合#include<stdio.h> void f(int N,int k,int a[],int b[],int m); int main() {int N=5;int k=3;int a[10];int b[5]={1,2,3,4,5};f(N,k,a,b,0);return 0; } void f(int N,int k,int a[],int b[],int m) { int i;if(k==0){for(i=0;i<m;i++){printf("%d",a[i]);}printf(" ");}else{for(i=N;i>=k;i--){a[m]=b[i-1];f(i-1,k-1,a,b,m+1);}} }

java方法,冒泡排序,选择排序,插入排序,二分查找,打印正三角形及买彩票案例练习

方法: 方法(函数),复用性,可读性 方法格式: 访问权限修饰符[其他的修饰符 如static]返回值类型 方法名 public static void getmenu(){content;} 参数: 实际参数:实际参与运算的 形式参数:接受实际参数的 方法返回值和重载: return:结束方法 返回值:由return带给调用者 注意: 1.若当前没有返回值类型,即返回值类型为void,方法中不写return 2.return表示结束一个方法,也...

C#练习题答案: 滑稽的算法任务【难度:2级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战【代码】

滑稽的算法任务【难度:2级】: 答案1: using System; using System.Linq;public class NumbersFinder {public static int Generator(int size, int position){if (position < 1 || position > Math.Pow(size, size)) return -1;var sum = "";position--;for (var i = 0; i < size; i++){sum = ((position % size) + 1).ToString() + sum;position /= size;}return int.Parse(sum);} }? 答案2: using System; using System.Collectio...

算法笔记第三章练习题_A+B和C,部分A+B,程序运行时间,划拳

部分A+B问题描述: 正整数 A 的“D?A??(为 1 位整数)部分”定义为由 A 中所有 D?A?? 组成的新整数 P?A??。例如:给定 A=3862767,D?A??=6,则 A 的“6 部分”P?A?? 是 66,因为 A 中有 2 个 6。 现给定 A、D?A??、B、D?B??,请编写程序计算 P?A??+P?B??。 输入格式: 输入在一行中依次给出 A、D?A??、B、D?B??,中间以空格分隔,其中 0<A,B<10?10??。 输出格式: 在一行中输出 P?A??+P?B?? 的值。 输入样例 1: 3862767 6 135302...

Leetcode练习:从中序与后序遍历序列构造二叉树,递归与迭代,python实现。

如题,递归方法与迭代方法。 两个关键点:一:后序遍历的最后一项是树的根节点,这个根节点在中序遍历的中间把中序遍历分成左子树和右子树两部分;二:同一个树中序遍历和后序遍历包含元素相同(顺序不同),通过中序遍历的分隔点返回后序遍历,找到左右子树的后序遍历。 两点注意事项:一个是小心笔误;第二个是找到根节点后,到中序遍历中找到分隔点,这个分隔点同样是后序遍历的分隔点。举例来说: 中序遍历: 【左子树部分】(...

【算法练习】字符串处理 poj2925:大整数的因子【代码】

题目链接:http://bailian.openjudge.cn/practice/2925 2925:大整数的因子 总时间限制: 1000ms 内存限制: 65536kB 描述 已知正整数k满足2<=k<=9,现给出长度最大为30位的十进制非负整数c,求所有能整除c的k。 输入 一个非负整数c,c的位数<=30。 输出 若存在满足 c%k == 0 的k,从小到大输出所有这样的k,相邻两个数之间用单个空格隔开;若没有这样的k,则输出"none"。 样例输入30样例输出2 3 5 6 解题思路: 大整数除小整数的简...

算法入门练习记录

沫璃邀请她的朋友参加周末的派对。沫璃买了3种颜色的气球,现在她要有这些气球来装饰餐桌,每个餐桌只用恰好3个气球装饰,要求3个气球的颜色不能完全一样,可以是2种或者3种颜色。沫璃想知道这些气球最多能装饰多少张餐桌。 分析:取出值最大的颜色,只要其他两种颜色加起来达到总颜色的1/3,那么答案是气球总数/3,如果达不到1/3,那么答案是气球总数减去最大值颜色。 #include<iostream> using namespace std; int main(){ ...

js算法练习--栈【代码】

class stack{constructor (){this.list=[];}//入栈Push(item){this.list.push(item);}//出站Pop(){return this.list.pop();}//判断栈是否为空GetIsEmpty(){return this.list.length==0;}//栈的大小GetSize(){return this.list.length;}//清空栈Clear(){this.list=[];}//读出栈数据Read(){console.log(this.list.toString());}}//使用let stackInfo=new stack();stackInfo.GetIsEmpty();//truestackInfo.Push(11);stackInfo.Push(10...

js算法练习--队【代码】

//一般队列class queue {constructor() {this.list = [];}//入队EnQueue(item) {this.list.push(item);}//出队Dequeue() {return this.list.shift();}//第一个元素值GetFront() {return this.list[0];}//长度GetSize() {return this.list.length;}//是否为空GetIsEmpty() {return this.list.length == 0;}// 清空Clear() {this.list = [];}//读取元素Read() {console.log(this.list.toString());}}let queueTest = new queue();que...

js算法练习--链表【代码】

class Node{constructor(element){this.element=element;this.next=null;}}class Linked{constructor(){this.head=null;this.length=0;}append(element){let node=new Node(element);if(this.head==null)this.head=node;else{let current=this.head;while(current && current.next){current=current.next;}current.next=node;}this.length++;}Insert(element,position){if(position<0 || position>this.length)return false;let no...

js算法练习--二叉树【代码】

class Node {constructor(key) {this.key = key;this.left = null;this.right = null;}}class BstSearch {constructor() {this.root = null;}insert(key) {var newNode = new Node(key);const inserNode = (node, newNode) => {if (newNode.key < node.key) {if (node.left == null) {node.left = newNode;}else {inserNode(node.left, newNode);}}else {if (node.right == null) {node.right = newNode;}else {inserNode(node.rig...

js算法练习--双向链表【代码】

class Node{constructor(element){this.element=element;this.pre=null;this.next=null;}}class DoubleLink{constructor(){this.head=null;this.tail=null;this.length=0;}Append(element){var node=new Node(element);if(this.head==null){this.head=node;this.tail=node;}else{this.tail.next=node;node.pre=this.tail;this.tail=node;}this.length++;}Insert(element,position){if(position<0 || position>this.length)return f...