摘要: 2.1 Insertion sort Insertion Sort#include <iostream>using namespace std;int main(){ int a[]={5,2,4,6,1,3}; int n=5; for (int j=1;j<=n;++j) { int key=a[j]; int i=j-1; while (( i >= 0)&& (a[i]>key)) { a[i+1]=a[i]; --i; } a... 阅读全文
posted @ 2013-06-05 16:19 StanleyWu 阅读(211) 评论(0) 推荐(0) 编辑
摘要: 插入排序: 阅读全文
posted @ 2013-05-27 18:07 StanleyWu 阅读(86) 评论(0) 推荐(0) 编辑
摘要: 在数组score中将要存储某小组C++程序设计的成绩,请设计完成下面的各功能函数,并将它们组合成一个完整的应用:(1)输入小组人数及成绩;(2)输出该小组的最高成绩、最低成绩、平均成绩和成绩的标准偏差(标准偏差公式:,其中为样本,为均值,为样本数目);(3)输出考得最高成绩和最低成绩的同学的人数及对应的学号(设成绩对应的下标即学号,可能有相同的成绩)(4)(选做)输出前3名同学的学号——可以先不考虑有并列名次的情况,再考虑有并列的情况。运行结果可以参考下图:#include <iostream>#include<Cmath>using namespace std;//在 阅读全文
posted @ 2013-05-27 15:59 StanleyWu 阅读(168) 评论(0) 推荐(0) 编辑
摘要: 创建一个长度为20的整型数组,通过初始化,为数组中的前10个元素赋初值,然后通过键盘输入,使后10个元素获得值,将所有元素值加倍后保存在数组中, 最后由前往后输出数组中所有元素的值,再由后往前输出数组中所有元素的值,再输出数组中的所有偶数,以及下标为3的倍数的元素值。#include <iostream>using namespace std;int main(){ int a[20]={1,2,3,4,5,6,7,8,9,10}; for (int i=10;i<20;++i) cin>>a[i]; for (int i=0;i<20;++i) ... 阅读全文
posted @ 2013-05-27 01:43 StanleyWu 阅读(120) 评论(0) 推荐(0) 编辑
摘要: 输出Fibnacci序列的第20个数#include <iostream>using namespace std;int fib(int n);int main(){ cout<<fib(20)<<endl; return 0;}int fib(int n){ if(n==1) return 0; else if(n==2) return 1; else return(fib(n-1)+fib(n-2));} 阅读全文
posted @ 2013-05-26 14:32 StanleyWu 阅读(116) 评论(0) 推荐(0) 编辑
摘要: 输入四个数,并求出其最大公约数#include <iostream>using namespace std;//自定义函数的原型(即函数声明)int gcd(int,int);int gcds(int,int,int,int);int main(){ int a,b,c,d; cin>>a>>b>>c>>d; cout<<"最大公约数是: "<<gcds(a,b,c,d)<<endl; return 0;}int gcd(int x,int y) //用辗转相除法,求两数的最大公 阅读全文
posted @ 2013-05-26 03:40 StanleyWu 阅读(132) 评论(0) 推荐(0) 编辑
摘要: #include <iostream>using namespace std;void printchs(char c, int m){ for (int j=1;j<=m;j++) cout<<c;}int main(){ int n=6; for (int i=1;i<=n;++i) { printchs(' ',n-i); printchs('*',2*i-1); cout<<endl; } return 0;}在由多个函数构成的程序中,程序员常用的做法是,main()函数先定义,其他自定义函数后... 阅读全文
posted @ 2013-05-26 03:07 StanleyWu 阅读(142) 评论(0) 推荐(0) 编辑
摘要: function x = gauss(A,B)%The sizes of matrices A,B are supposed to be NA x NA and NA x NB.%This function solves Ax = B by Gauss elimination algorithm.NA = size(A,2); [NB1,NB] = size(B);if NB1 ~= NA error('A and B must have compatible dimensions');endN = NA + NB; AB = [A(1:NA,1:NA) B(1:NA,1:NB 阅读全文
posted @ 2013-05-22 16:29 StanleyWu 阅读(281) 评论(0) 推荐(0) 编辑
摘要: 用二分法找出的根function y=funcbisect01(x);y=3*x^5-2*x^3+6*x-8;x1=1;x2=2; tol=0.00001;while (abs(x1-x2)>tol) f1=funcbisect01(x1); f2=funcbisect01(x2); xm=(x1+x2)/2; fm=funcbisect01(xm); if (f1*fm<0) x2=xm; else x1=xm; endend 阅读全文
posted @ 2013-05-22 00:36 StanleyWu 阅读(247) 评论(0) 推荐(0) 编辑
摘要: 求1000以内所有偶数的和#include <iostream>#include <cmath>using namespace std;int main(){ int sum=0; for(int i=2;i<=1000;i=i+2) { sum=sum+i; } cout<<sum<<endl; return 0;} 阅读全文
posted @ 2013-05-21 01:28 StanleyWu 阅读(279) 评论(0) 推荐(0) 编辑