
c++
syqhit
这个作者很懒,什么都没留下…
展开
-
杭电acm1008
#include#include#includeusing namespace std;int main(){ int A,n; int a[100]; while(cin>>n) { if(n==0) { } else{ int sum=0;原创 2016-04-13 17:43:59 · 450 阅读 · 0 评论 -
字符串转数字
看了剑指offer上面的第一道题,字符串转数字,就去查了下,有多种方法。比如可以直接用函数atoi();下面是我的代码。#include#include#include#include#includeusing namespace std;int main(){string a ="100";int num=0;if(a.length()!=0){原创 2016-04-19 21:17:29 · 307 阅读 · 0 评论 -
链表操作 创建链表和打印链表
通过以下程序实现创建一个长度为10的链表,其中链表的元素是0-9.并且打印出来。//#include"stdafx.h"#include#include#include#includeusing namespace std;struct ListNode{ int m_key; ListNode* next;};void createLi原创 2016-04-20 00:02:40 · 397 阅读 · 0 评论 -
leetcode 283 Movezeros
实现将数组中的0元素移动到末尾我的思路是扫描一遍数组,元素为0则计数加一,不为0则按顺序复制给新数组。完整c++代码如下:#include #includeusing namespace std;void moveZeroes(vector& nums) { int count,i,j,p; count=0; j=0; int l原创 2016-04-20 16:02:27 · 290 阅读 · 0 评论 -
输出数组的全部组合
采用递归的思想。#include#include#includeusing namespace std;void combine(int arr[],int data[],int start,int end,int index,int r){ if(index==r) { for(int i=0;i {cout原创 2016-04-22 09:15:38 · 3274 阅读 · 0 评论 -
幸运数 47
一道关于幸运数的题目,不过我思路一定是错的。还没想出来怎么做,先mark一下吧。#include#include#include#include#includeusing namespace std;int main() {vectora;vectorb1;int aa,b;cin>>aa>>b;int temp=b;if(aa{原创 2016-04-22 11:01:16 · 1232 阅读 · 0 评论 -
替换字符串中的空格为%20
剑指offer上面的一道题。例如字符串是we are happy.输出 we%20are%20happy.如果按照基本的思路从前到后遍历字符串,每次识别到空格后面的字符就要往后移动两个位。因此用从后向前的方式先计算出替换后的数组的长度。设置两个指针一个指向新数组的尾部,另一个指向原来数组的尾部。直到两个指针的数值相同时表示遍历完成。代码如下。#include#includ原创 2016-04-22 16:30:26 · 1370 阅读 · 0 评论 -
斐波那契数列
输出斐波那契数列的第n项第一个想到的是用通项公式,这个最简单。但是第一次提交没有成功,后来看了看是丢失精度问题。 int fib1(int n) { double a=(1+sqrt(5))/2.0; double b=(1-sqrt(5))/2.0; if(n==1||n==2) return 1; else return (p原创 2016-05-21 12:07:55 · 236 阅读 · 0 评论 -
重建二叉树
输入一棵二叉树的前序遍历序列和中序遍历序列,重建二叉树,返回二叉树的根结点。采用递归的思想,前序遍历的第一个节点是二叉树的根节点,在中序遍历中找到根节点左边的是左子树,右边的是右子树。struct TreeNode* reConstructBinaryTree(vector pre,vector in) { int len=pre.size(); if(len==0)原创 2016-07-06 10:19:09 · 220 阅读 · 0 评论 -
用两个栈实现队列
用两个栈实现一个队列的push和pop操作队列的特点是先进先出,栈是先进后出。因此,push操作只需要按顺序进栈,pop操作需要把一个栈中的元素转移到另一个栈。stack stack1;stack stack2;void push(int node) { stack1.push(node); } int pop() { int a;原创 2016-07-06 10:51:23 · 248 阅读 · 0 评论 -
杭电acm统计字符
这题要注意从键盘输入字符串的时候用cin函数,识别到空格会自动停止,因此要用getline(cin,str),str是要输入的字符串。Problem Description统计一个给定字符串中指定的字符出现的次数 Input测试输入包含若干测试用例,每个测试用例包含2行,第1行为一个长度不超过5的字符串,第2行为一个长度不超过80的字符串。注意这里的字符串包含空原创 2016-04-30 23:37:20 · 1341 阅读 · 0 评论 -
杭电acm1870愚人节的礼物
问题思路是识别到除了(和)以外的字符就停止识别,其实题目说了只有B字符,也可以识别到B字符的时候break.识别过程中(数字加一,)数目减一。Problem Description四月一日快到了,Vayko想了个愚人的好办法——送礼物。嘿嘿,不要想的太好,这礼物可没那么简单,Vayko为了愚人,准备了一堆盒子,其中有一个盒子里面装了礼物。盒子里面可以再放零个或者多个盒子。假设放原创 2016-04-30 23:33:42 · 1059 阅读 · 0 评论 -
杭电1004
杭电1004题原创 2016-04-09 16:20:53 · 423 阅读 · 0 评论 -
杭电1061
Problem DescriptionGiven a positive integer N, you should output the most right digit of N^N. InputThe input contains several test cases. The first line of the input is a single integer T原创 2016-04-13 20:16:08 · 331 阅读 · 0 评论 -
构建链表 c++
构建一个链表。头插法和尾插法#include using namespace std;struct ListNode{ int m_key; ListNode* next;};void creatNodeList(ListNode *phead){ ListNode *p=phead; for(int i=1;i原创 2016-04-27 11:01:41 · 355 阅读 · 0 评论 -
链表 倒序输出
倒序输出可以用先逆转链表在顺序输出的思路。在不改变链表的结构的前提下,用栈来实现先进后出的目的。void PrintReverse(ListNode*phead){ std::stacknodes; ListNode*pNode=phead; while(pNode!=NULL) { nodes.push(pNode);原创 2016-04-27 11:16:17 · 416 阅读 · 0 评论 -
杭电1196
#include#include#include#includeusing namespace std;int main(){ int i,x,z,result; while(cin>>x) { if(x==0) { } else{ int j=0; i原创 2016-04-14 16:38:44 · 343 阅读 · 0 评论 -
重建二叉树
经典的题目,已知一颗树的前序遍历和中序遍历,求这个树的后序遍历。#include #include#includeusing namespace std;struct TreeNode{ int key; TreeNode* left; TreeNode* right;};TreeNode* CreateTree(int *pre_order,int pr原创 2016-04-28 11:19:12 · 294 阅读 · 0 评论 -
杭电1012
#include#include#include#includeusing namespace std;int fun(int x){ int a[11]; a[0]=1; for(int i=1;i { a[i]=i*a[i-1]; } return a[x];}int main()原创 2016-04-13 19:25:54 · 400 阅读 · 0 评论 -
建立一棵二叉树
采用递归的方法建立一棵二叉树,虽然很简单的算法,真正写起来确是漏洞百出,果然还是基础不够扎实。struct TreeNode{ char key; TreeNode* left; TreeNode* right;};TreeNode* CreatTree(){ TreeNode *T = new TreeNode; char a; cout<原创 2016-04-29 09:35:55 · 4241 阅读 · 0 评论 -
杭电2014
#include #include#include#includeusing namespace std;int main(){ int n; double X; while(cin>>n) { double a[100]={0}; double max1=0; double mi原创 2016-04-16 22:54:56 · 529 阅读 · 0 评论 -
杭电acm2015
#include #includeusing namespace std;int main(){ int n; int m; int temp; int ave; while(cin>>n) { int sum=0; int num=0; cin>>m;原创 2016-04-17 00:03:56 · 512 阅读 · 0 评论 -
归并排序
看很多人写得面经都被问到了手写归并排序归并排序的思路是分治和递归每次分成两个有序的数组之后,化解成合并两个有序数组的问题void merge(int a[],int first,int mid,int last,int temp[]){ int i=first; int j=mid+1; int m=mid; int n=last; int k原创 2016-08-22 16:19:00 · 287 阅读 · 0 评论