
基础算法练习
Yoc Lu
Do it yourself!
展开
-
A - The 3n + 1 problem
题目#include <cstdio>#include <algorithm>using namespace std;int main (){ int i, j; while ( scanf ("%d %d", &i, &j) != EOF ) { int temp_i = i;//用于记录输入顺序 int temp_j = j;原创 2018-01-20 00:22:07 · 351 阅读 · 0 评论 -
L1-009. N个数求和
//浮点错误,按理来说应该是出现了/0或者%0的情况,反复查找不知道错在哪里//再考虑累乘累加时是否会超出long long的范围,所以改为逐个有理数依次进行求和/*#include <iostream>using namespace std;const int MAX=100;long int fun(long int a,long int b);int main...原创 2017-05-29 17:34:58 · 829 阅读 · 0 评论 -
L1-008. 求整数段和
//注意格式要求#include <stdio.h>int main(){ int A,B,sum=0,n=0; scanf("%d%d",&A,&B); while(A<=B) { printf("%5d",A); sum+=A; A++; n++; ...原创 2017-05-29 13:00:23 · 442 阅读 · 0 评论 -
L1-011. A-B
//适用范围较小,且易导致运行超时/*#include <iostream>#include <string>using namespace std;int main(){ string A,B; getline(cin,A);//getline函数是遇回车符结束输入 getline(cin,B); string::iter...原创 2017-05-29 19:07:19 · 327 阅读 · 0 评论 -
NULL,'\0',0,'0',' '区别
1.在数值上NULL,'\0',0是一样的,都是0,但'0'不同,在ASCII码中编码为482.在内存中NULL 和'\0' 和'0'都是一个8位的char类型,NULL 和'\0' 值一样,都是0,以数字方式读取就是0,以字符串读取时就是'\0'或者null(和编译器有关),而‘0’在内存存储着48,以字符读取就是'0',以数字读取就是48,至于0,可能是char ,int ,float...原创 2017-05-29 19:42:14 · 501 阅读 · 0 评论 -
L1-017. 到底有多二
#include <iostream>#include <stdio.h>#include <string>using namespace std;int main(){ string str; cin>>str; int len=str.size(),cnt=0; for(int i=0;i<le...原创 2017-06-11 00:14:37 · 388 阅读 · 0 评论 -
L1-018. 大笨钟
//注意输入输出的格式问题#include <stdio.h>int main(){ int h,m; scanf("%d:%d",&h,&m); if((h==12&&m>0)||(h>12&&h<24)) { h-=12; int i; ..原创 2017-06-11 20:36:11 · 474 阅读 · 0 评论 -
L1-019. 谁先倒
/*//出错代码#include <iostream>using namespace std;int main(){ int a,b;//A-甲 B-乙 cin>>a>>b; int n; cin>>n; int a1[n],a2[n],b1[n],b2[n]; for(int i=0;i...原创 2017-06-11 21:45:30 · 301 阅读 · 0 评论 -
L1-020. 帅到没朋友
//只是部分正确#include <iostream>using namespace std;const int MAX=100000;int main(){ int visit[MAX]={0}; int N,K,M,id; cin>>N; for(int i=0;i<N;i++) { cin...原创 2017-07-13 23:03:18 · 698 阅读 · 0 评论 -
L1-023. 输出GPLT
#include <iostream>#include <cstring>using namespace std;int main(){ string str; cin>>str; int n=str.size(); int n1=0,n2=0,n3=0,n4=0; for(int i=0;i<n;i++...原创 2017-07-13 23:55:10 · 428 阅读 · 0 评论 -
L1-024. 后天
#include <iostream>using namespace std;int main(){ int n,m; cin>>n; m=(n+2)%7; if(m==0)m=7; cout<<m; return 0;}原创 2017-07-14 00:01:07 · 704 阅读 · 0 评论 -
头文件#include<bits>
#include<bits/stdc++.h>包含了目前c++所包含的所有头文件#include <iostream> #include <cstdio> #include <fstream> #include <algorithm> #include <cmath> #include <deque>原创 2017-09-15 21:58:33 · 802 阅读 · 0 评论 -
exit(0)与exit(1)、return区别
exit(0)与exit(1)、return区别参考博客 exit(0):正常运行程序并退出程序; exit(1):非正常运行导致退出程序; return():返回函数,若在主函数中,则会退出函数并返回一值。详细说: 1. return返回函数值,是关键字; exit 是一个函数。 2. return是语言级别的,它表示了调用堆栈的返回;而exit是系统调用级别的,它表示了一个...转载 2018-06-17 12:05:10 · 251 阅读 · 0 评论 -
光标变成下横线,变回竖线方法
光标变成下横线,变回竖线方法按一下键盘几个方向键上面的 Insert 键原创 2018-08-11 20:26:23 · 74529 阅读 · 17 评论 -
ASCII,ISO-8859-1,GBK,Unicode,UTF-8 编码介绍
ASCII,ISO-8859-1,GBK,Unicode,UTF-8 编码介绍编码基本知识最早的编码是ISO8859-1,和ascii编码相似。但为了方便表示各种各样的语言,逐渐出现了很多标准编码,重要的有如下几个:ASCIIASCII(发音: /ˈæski/ ASS-kee,American Standard Code for Information Interchange,美国信息...原创 2018-11-30 16:30:52 · 2932 阅读 · 0 评论 -
求解Hailstone峰值
求解Hailstone峰值#include <iostream>#include <stdio.h>using namespace std;int hailstone(int n){ //int length = 1; int maxValue = 0; while(n > 1) { printf("%5...原创 2018-12-10 22:01:06 · 384 阅读 · 0 评论 -
L1-007. 念数字
//注意:行末没有最后的空格#include <iostream>#include <string>using namespace std;int main(){ string str; cin>>str; int len=str.size()-1;//str.size()计算结果包含'\0' for(int i=...原创 2017-05-29 12:43:27 · 781 阅读 · 0 评论 -
L1-006. 连续因子
//暴力求解//12! < 2^31 < 13!//限定的最长连续因子的个数为12//若长度比1大,至少两个相邻的数相乘,这两个数的乘积是不可能超过N//x(x+1)<=N 即若不止一个因子相乘时,所求连续因子序列第一个数不可能超过sqrt(N)#include <iostream>#include <cmath>using names...原创 2017-05-29 12:14:25 · 351 阅读 · 0 评论 -
L1-005. 考试座位号
//注意准考证号是大数#include <iostream>#include <string>using namespace std;const int MAX=1000;struct student{ string number; int num1; int num2;}stu[MAX];int main(){ in...原创 2017-05-28 00:03:49 · 342 阅读 · 0 评论 -
编程语言分类
有一种说法:一个好的计算机专业毕业生,应该掌握 一门面向过程的语言 一门面向对象语言 一门脚本语言 如果学有余力,最好 对一门函数式语言有所了解 打个比方:毕业时熟悉C、Java和Python,对Scala有一定了解,就算是符合这个条件了。目前编程语言主要分为三大类: 1.机器语言 2.汇编语言...原创 2017-02-22 12:39:55 · 13205 阅读 · 2 评论 -
关于交换(swap)
用c/c++语言编程有多种方式处理“交换”1.//错误的方法#include<iostream>using namespace std;void swap(int a,int b);int main(){ int m=1,n=2; cout<<"m="<<m<<",n="<<n<原创 2017-02-22 16:24:37 · 759 阅读 · 0 评论 -
冒泡排序(BubbleSort)
//冒泡排序(BubbleSort) //从左向右扫描数据,选择最大的数据,放在右边//要点:比较相邻的两个数,如果左边大于右边就进行交换 #include<iostream> using namespace std;void BubbleSort(int b[],int n);int main(){ int i,a[]={2,4,6,8,0,1,3,5,7...原创 2017-02-22 17:43:07 · 595 阅读 · 0 评论 -
选择排序(SelectSort)
/*选择排序(SelectSort) *从当前未排序的整数中找一个最小的整数, *将它放在已排序的整数列表的最后。 *要点:选择排序选最小的,往左边选。 *选择排序(只在最后进行一次交换)比冒泡排序 *(交换次数太多,只要左边大于右边就交换)快, *两种排序都属于低级排序(同一个档次)。 */ #include <iostream>using namesp...原创 2017-02-26 18:30:47 · 599 阅读 · 0 评论 -
顺序查找
//顺序查找//常见的查找算法有两种://顺序查找和折半查找(又称二分查找)//对没有排序的数据用顺序查找(简单但速度慢) //对已排序的数据用二分查找 #include <iostream>using namespace std;int SequentialSearch(int *list,const int n,const int x);int main()...原创 2017-02-26 19:27:26 · 292 阅读 · 0 评论 -
二分查找
//二分查找//使用前提是数据已排序 ,否则需要用线性查找(顺序查找) //二分法是常见程序中最快的算法 //2^20=100万(就是1M)最多查找20次 //2^30=10亿(就是1G)最多查找30次 #include <iostream> using namespace std; int BinarySearch(int *list,const int n,co...原创 2017-02-26 20:12:16 · 347 阅读 · 0 评论 -
递归与迭代
//递归与迭代 //递归就是自己调用自己(占用内存多),迭代就是一般的循环 //递归需要用一定的条件进行控制,否则是死循环 //计算阶乘 (factorial) #include <iostream>using namespace std;long factorial1(int n)//递归 { if(n==0) return 1; else retu...原创 2017-02-26 20:49:21 · 365 阅读 · 0 评论 -
二分查找(补充)
//二分查找(补充)//用迭代和递归分别描述 #include <iostream>using namespace std;int BinarySearch_I(int *list,const int x,const int n)//迭代 { int left=0,right=n-1; while(left<=right){ int middle=left...原创 2017-02-26 21:37:37 · 318 阅读 · 0 评论 -
排列组合(Permutations)
//排列组合(Permutations)//a,b,c a,c,b//b,a,c b,c,a//c,a,b c,b,a#include <iostream>using namespace std;/*void Permutations(char *p,const int k,const int m){ //a开头的,后面跟着bc的所有排列 swap...原创 2017-03-05 22:04:15 · 744 阅读 · 0 评论 -
VS2013解决This function or variable may be unsafe编译问题
1.用VS2013打开出现错误的代码文件2.在工程文件名处右击鼠标打开快捷菜单,找到“属性”选项,进入项目属性页面3.在属性页面中找到“C/C++"—”预处理器“4.在预处理器定义的编辑窗口中添加一句命令:_CRT_SECURE_NO_WARNINGS,添加完成后应用并退出详解(点击打开链接)...原创 2017-04-02 16:09:58 · 724 阅读 · 0 评论 -
*&p和**p
int *a;int *&p=a;int b=10;p=&b;/** int &p=a;//p是a的别名,p和a是同一个整型变量,* //占用同一块内存空间* int *a;* int *&p=a;//把int* 看成一个类型(整型指针),* //p是a的别名,就是定义一个整型指针* ...原创 2017-04-22 17:32:29 · 779 阅读 · 0 评论 -
L1-002. 打印沙漏
//重要://打印每行时题目没有要求用空格补全后面的,//所以空格不要放太多#include <iostream>#include <cmath>using namespace std;int main(){ int N,m,a,b,d,k; double n; char c; cin>>N>>c;...原创 2017-05-27 21:06:44 · 844 阅读 · 0 评论 -
sizeof和strlen区别
char str[20]="0123456789";int a=strlen(str); //a=10; strlen计算字符串的长度,以结束符 0x00 为字符串结束标志int b=sizeof(str); //b=20; sizeof计算的则是分配的数组 str[20] 所占的内存空间的大小char* ss="0123456789";int c=sizeof(ss);//c=4; ...原创 2017-05-27 22:12:45 · 326 阅读 · 0 评论 -
字符串相关
string是C++标准库中的实现,CString 是MFC或者ATL中的实现,char*为C编程中最常用的字符串指针,一般以"\0"为结束标志string和CString均是字符串模板类,string为标准模板类(STL)定义的字符串类,已经纳入C++标准之中关于string类的详细介绍关于string类的详细介绍 ...原创 2017-05-27 22:50:40 · 286 阅读 · 0 评论 -
L1-003. 个位数统计
/*//位数较少时适用,无法运行大数#include <iostream>using namespace std;int main(){ int a[10]={0}; long int N; int i,j=0; cin>>N; while(N>0) { i=N%10; N...原创 2017-05-27 23:34:26 · 296 阅读 · 0 评论 -
快速排序(QuickSort)
快速排序(QuickSort)具体实现代码如下://QuickSort#include <iostream>#include <stdio.h>int Partition(int a[], int L, int R){ int p=a[L];//中轴 int i=L+1,j=R; while(1){ whil...原创 2019-04-10 17:13:16 · 560 阅读 · 0 评论