1、分段函数
有一函数:
编写程序,输入x,输出y的值。
思路:自变量x的范围为if的条件语句,函数为执行语句。
代码如下。
#include<iostream>
using namespace std;
int main()
{
float x,y;
cout<<"输入x:"<<endl;
cin>>x;
if(x<0)
{
y=2*x;
}
else if(x>=0,x<=5)
{
y=3*x+4;
}
else
{
y=4*x-15;
}
cout<<"y= "<<y<<endl;
return 0;
}
2、三种交换数值的方式(值传递、地址传递、引用传递)
#include <iostream>
using namespace std;
//值交换
void swap1(int a, int b)
{
int temp = a;
a = b;
b = temp;
}
//地址交换
void swap2(int* a, int* b)
{
int temp = *a;
*a = *b;
*b = temp;
}
//引用交换
void swap3(int& a, int& b)
{
int temp = a;
a = b;
b = temp;
}
int main()
{
int a = 1;
int b = 2;
swap1(a, b);
cout << a << "," << b << endl;
swap2(&a, &b);
cout << a << "," << b << endl;
swap3(a, b);
cout << a << "," << b << endl;
return 0;
}
3、冒泡法
例1:将十个整数从小到大顺序排序(冒泡排序法)
先说一下冒泡排序法的思路 若n个整数则需要进行n-1轮
每轮:将两个相邻元素进行对比 若左大于右则交换 第一轮需要比较n-1次 第二轮n-2次
每轮结束后 最大的元素就会去到最右边 第二轮倒数第二大就会去到最右边
所以 见代码:
int main()
{
const int n = 10;
void select_sort(int *p,int n);
int arr[n] = { -2,122,34,654,4364,32,54,4,65,0 };
select_sort(arr, n);
cout << "The sorted array:" << endl;
for (int i = 0; i < 10; i++)
{
cout << arr[i] << " ";
}
return 0;
}
冒泡排序法 共排序n-1次 第一次比较n-1次 第二次n-2次 最终要从小到大排列
void select_sort(int *p, int n)
{
for (int i = 0; i < n-1; i++) // 一共多少轮
{
for (int j = 0; j < n-1-i; j++) //一轮的内部比较
{
if (p[j]>p[j+1]) //若左边大于右边 则左右交换
{
int tmp = p[j];
p[j] = p[j + 1];
p[j + 1] = tmp;
}
}
}
}
例2:对输入的n个整数进行排序:冒泡排序
#include<iostream>
using namespace std;
int main()
{
int c[100],n=0; //n为1到100
while(cin>>n) //输入n
{
for(int i=0;i<n;i++)
{
cin>>c[i]; //循环n次,给数组赋值
}
for(int j=0;j<n;j++) //冒泡排序
{
for(int k=0;k<n-j-1;k++)
{
if(c[k]>c[k+1])
{
int temp=c[k];
c[k]=c[k+1];
c[k+1]=temp;
}
}
}
for(int p=0;p<n;p++) //输出排序
cout<<c[p]<<' ';
}
return 0;
}
3、九九乘法表
(1)使用for 循环
#include <iostream>
using namespace std;
int main()
{
int i, j;
for (int i = 1; i <= 9; i++)
{
for (int j = 1; j <= i; j++)
{
cout << j << "*" << i << "=" << i * j << "\t";
}
cout << endl;
}
return 0;
}
(2)使用while 循环
#include <iostream>
using namespace std;
int main()
{
int i = 1;
while (i < 10)
{
int j = 1;
while (j <= i)
{
cout << j << "*" << i << "=" << i * j << "\t";
j+=1;
}
cout << endl;
i+=1;
}
return 0;
}
(3)用两个数组打印九九乘法表(指针的方法)
#include <iostream>
using namespace std;
int main()
{
int a[9] = { 1,2,3,4,5,6,7,8,9 };
int b[9] = { 1,2,3,4,5,6,7,8,9 };
int* p = a;
int* p1 = b;
for (int i=0;i<9;i++)
{
for (int j=0;j<=i;j++)
{
cout << *(p+j)<< "*" << *(p1+i) << "=" << *(p+j) * *(p1+i)<<"\t";
}
cout<<endl;
}
4、寻找数组内最大最小值
(1)递归方法
#include <iostream>
using namespace std;
//定义求最值函数
int max(int a[], int len) {
if (len == 0) {
return a[0];
}
else {
max(a, len - 1);
return (a[len] > max(a, len - 1) ? a[len] : max(a, len - 1));
}
}
int main() {
int b[] = { 4,1,3,6,4,8 };
int len = sizeof(b) / sizeof(b[0]);//获取数组元素个数
int m = max(b, len-1); //因为索引从0开始,此处数组长度要减1
cout << m << endl;
system("pause");
return 0;
}
(2) C++里面有好多自带函数可以直接用,比如寻找数组中的最大最小值其实是有函数的,如下
#include <iostream>
using namespace std;
#include <algorithm>
int main() {
int n;
cin >> n;
int *p = new int[n];
for (int i = 0; i<n; i++)
{
cin >> p[i];
}
cout << (*min_element(p, p + n))<<' '<< (*max_element(p, p + n)) << endl;
return 0;
}
5、递归实现一个数的各项阶乘之和
#include <iostream>
using namespace std;
//前n项阶乘
int getjj(int n) {
if(n==0 ||n==1) {
return 1;
}
return n*getjj(n-1);
}
//各项阶乘和
int fun(int n) {
//0的阶乘是1
if(n==0 || n==1) {
return 1;
}
return fun(n/10)+getjj(n%10);
}
int main() {
cout<<"13各项阶乘和 "<<fun(13)<<endl;
cout<<"103各项阶乘和 "<<fun(103)<<endl;
cout<<"250各项阶乘和 "<<fun(250)<<endl;
return 0;
}
或者
#include <bits/stdc++.h>
using namespace std;
int fact(int n) //求某个数阶乘的函数
{
int i;
if(n == 1 || n == 0)
i = 1;
else
i = n*fact(n-1);
return i;
}
int factsum(int n) //求阶乘的和的函数
{
int j;
if(n == 0)
j = 0;
else if(n == 1)
j = 1;
else
j = factsum(n-1) + fact(n);\
return j;
}
int main(void)
{
int a;
cin>>a;
cout<<factsum(a);
cout<<endl;
return 0;
}
6、利用递归函数求Fibonacci数列:1,12,3,5,8,13
递归的目的是简化程序设计,增强程序易读性。对于一个能够写出递归通项的数学函数,总是很容易将其程序化,然后考虑一些边界条件,就可以达到目的。但是,递归也会迅速增加系统开销,在时间上,执行函数的调用与返回的次数明显要大于非递归函数;在空间上,栈空间资源也会遭到空前的劫掠,随着每递归一次,栈内存就会多占用一截。递归函数在时空开销上均影响程序的性能。
7、类与对象——时间类
设计一个 Time 类,要求如下。
(1)有一个无参构造函数,设置初始小时与分钟均为 0。
(2)有一个带参构造函数,其参数分别对应小时与分钟。
(3)用一个成员函数实现对时间的设置。
(4)用一个友元函数实现以 12 小时的方式输出时间。
(5)用一个成员函数实现以 24 小时的方式输出时间。
(6)用一个成员函数实现日期的获取。
#include<iostream>
using namespace std;
class Time
{
public:
Time()//初始化所有
{
this->fenzhong = 0;
this->xiaoshi = 0;
this->year = 0;
this->day = 0;
this->month = 0;
}
Time(int fenzhong, int xiaoshi)
{
this->fenzhong = fenzhong;
this->xiaoshi = xiaoshi;
}
void p()
{
if (xiaoshi > 12)
{
xiaoshi -= 12;
cout << "下午" << xiaoshi << endl;
}
else
{
cout << "早上:" << xiaoshi << endl;
}
}
friend void p2(Time&);
void p1()
{
cout << xiaoshi << endl;
}
void p2()
{
cout << year << " " << month << " " << day << endl;
}
void p3(int month, int day, int year)
{
this->day = day;
this->month = month;
this->year = year;
}
private:
int fenzhong, xiaoshi, year, month, day;
};
void p2(Time& c)
{
if (c.xiaoshi > 12)
{
c.xiaoshi -= 12;
cout << c.xiaoshi << endl;
}
else
{
cout << c.xiaoshi << endl;
}
}
int main()
{
Time v(30, 3);
v.p3(12, 3, 2011);//设置日期
v.p2();//输出年份
v.p();//24小时方式输出小时
v.p1();//输出小时
p2(v);//以12小时输出时间
system("pause");
}