原创 浙大PAT反轉鏈表:7-2 Reversing Linked List

7-2 Reversing Linked List (25分) Given a constant K and a singly linked list L, you are supposed to reverse the link

原创 浙大PAT:一元多項式的加法與乘法

PAT:一元多項式的加法與乘法 #include <stdio.h> #include <stdlib.h> typedef struct Node{ int xs; int zs; struct Nod

原创 墓地雕塑 LA 3708(有一個雕塑不動的完整證明)

輸入:當前墓碑規模n,添加規模m 解:取一個雕塑不動(在所有樣例都成立),找到其他雕塑移動至距離最近的目標(保證不會有兩個雕塑移動到同一目標點),此爲最優解。 將圓按 10000:n+m 的比例縮小,借用取上下界得方法找到距離最近

原创 浙大PAT最大子列和:Maximum Subsequence Sum

#include <iostream> int MaxSubSeqSum(int A[], int n, int &first, int &last) { int ThisSum(0),MaxSum(A[0]); int

原创 按距離排序

輸入 多組數據,每組一行,是一個整數n和一個字符串s 輸出 定義兩個整數的距離爲兩個整數差的絕對值 定義兩個字符串的距離爲兩個字符串長度差的絕對值 對每組數據: 對數組a按和n的距離從小到大排序後輸出。距離相同的,值小的排在前面。

原创 結構體在c和c++中的不同

分三塊來講述:   1 首先://注意在C和C++裏不同     在C中定義一個結構體類型要用typedef:     typedef struct Student     {     int a;     }Stu;     於

原创 二維數組作爲函數形參該如何傳遞

今天在論壇上有朋友提問:聲明函數如下void function(int** pInt),意圖是想參數傳遞一個二維數組。於是就定義了一個二維數組,比如 int[1][1],然後調用函數。結果如何?當然是失敗了,編譯器提示:canno

原创 sqrt函數的幾種實現方法

Implement int sqrt(int x). Compute and return the square root of x. 1:二分查找 思路:要實現一個sqrt函數,可以使用二分法,首先確定一個範圍[begin,

原创 浙大PAT:堆的操作集

7-5 堆中的路徑 (25分) 將一系列給定數字插入一個初始爲空的小頂堆H[]。隨後對任意給定的下標i,打印從H[i]到根結點的路徑。 輸入格式: 每組測試第1行包含2個正整數N和M(≤1000),分別是插入元素的個數、以及需要打

原创 C語言實現平衡二叉樹全過程

Root of AVL Tree An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child s

原创 構建完全二叉搜索樹

7-7 Complete Binary Search Tree (30分) A Binary Search Tree (BST) is recursively defined as a binary tree which has

原创 浙大PAT:是否是同一顆二叉排序樹

7-4 是否同一棵二叉搜索樹 (25分) 給定一個插入序列就可以唯一確定一棵二叉搜索樹。然而,一棵給定的二叉搜索樹卻可以由多種不同的插入序列得到。例如分別按照序列{2, 1, 3}和{2, 3, 1}插入初始爲空的二叉搜索樹,都得

原创 浙大PAT:7-5 Tree Traversals Again (25分)

7-5 Tree Traversals Again (25分) An inorder binary tree traversal can be implemented in a non-recursive way with a s

原创 浙大PAT出棧序列:7-3 Pop Sequence

Given a stack which can keep M numbers at most. Push N numbers in the order of 1, 2, 3, …, N and pop randomly. You

原创 浙大PAT遍歷葉子結點:7-4 List Leaves

7-4 List Leaves (25分) Given a tree, you are supposed to list all the leaves in the order of top down, and left to r