1.输入4个正整数,按从小到大的顺序输出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<iostream>
using namespace std;
int main(){
int a,b,c,d,m;
cout<<"请输入4个整数"<<endl;
cin>>a>>b>>c>>d;
m = a;
if(a>b){t=a;a=b;b=t;}
if(a>c){t=a;a=c;c=t;}
if(a>d){t=a;a=d;d=t;}
if(b>c){t=b;b=c;c=t;}
if(b>d){t=b;b=d;d=t;}
if(c>d){t=c;c=d;d=t;}
cout<<"从小到大的排序为"<<endl;
cout<<a<<" "<<b<<" "<<c<<" "<<d<<endl;

return 0;
}

2.编程创建一个简单的计算器,可以实现“+,-,*,/”(利用函数实现)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include<iostream>
using namespace std;

double add(double num1,double num2){return num1+num2;}
double sub(double num1,double num2){return num1-num2;}
double mul(double,double); //这样写只是想复习一下函数的声明
double div(double,double);
int main(){
double a,b;
char c;
double result;
cout<<"请输入两个数据"<<endl;
cin>>a>>b;
cout<<"请输入计算方式(+,-,*,/)"<<endl;
cin>>c;
switch(c){ //用switch要在括号里写明是对谁选择
case '+':result=add(a,b);break;
case '-':result=sub(a,b);break;
case '*':result=mul(a,b);break;
case '/':
if(b==0){cout<<"除数不得为0"<<endl;return 1;} //这个判断最好这样写在外
result=div(a,b);
break;
default:
cout<<"输入无效!"<<endl;
return 1; //这里是return 1;否则会向下执行,导致错误result
}
cout<<"结果为:"<<result<<endl;
return 0;
}
double mul(double num1,double num2){return num1*num2;}
double div(double num1,double num2){return num1/num2;}

3.求Sn=a+aa+aaa+…+aa…a(n个a)的值,其中a表示一个数字。例如:3+33+333+3333+33333(此时n=5),n的值由键盘输入。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<iostream>
using namespace std;
int main(){
int a,n,s=0,d=0; //s和d要初始化,要记得这个d
cin>>a>>n;
//int b = a; 如果那样的话,要用b存一下a的初值
for(int i=1;i<=n;i++){
d = d+a;
s = s+d;
a = a*10;
}
cout<<"Sn="<<s<<endl;
//若要输出如2+22+222+...=
//cout<<b<<"+"<<(b*10+b)<<"+"<<(b*100+b*10+b)<<"+···="<<s<<endl;
return 0;
}

4.如果整数A的全部因子(包括1,但不包括A本身)之和等于E,且整数E的全部因子(包括1,但不包括E本身)之和等于A,则将整数A和E成为亲密数,求3000以内的全部亲密数。(例如:220的因子有:1,2,4,5,10,11,20,22,44,55,110,220的因子之和为284;而284的因子有:1,2,4,71,142,因子之和为220,因此我们称220和284是亲密数)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<iostream>
using namespace std;

int yinzi(int n){
int sum = 0; //初始化
for(int i=1;i<=n/2;i++) //大于n/2的数不可能是n的因子
{
if(n%i==0){sum += i;} //累加
}
return sum;
}
int main(){
cout<<"3000以内的全部亲密数:"<<endl;
for(int a=1;a<=3000;a++){
int s = yinzi(a);
if(a<s && yinzi(s)==a){ //声明a<s,避免重复冗余
cout<<a<<"和"<<s<<endl;
}
}
return 0;
}

5.从键盘输入n(n<100)个整数(以0结束),存放在一个一维数组中,将它们按奇数在前、偶数在后,同为奇数或偶数的按从小到大的顺序排序,并输出排序后的结果。(冒泡排序)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include<iostream>
using namespace std;
#define Max 100 //注意不加分号,不过const int Max=100;也行

int main(){
int a[Max],n=0,i,j,t;
cout<<"请输入一组整数,以0结束:"<<endl;
cin>>a[n]; //这里的逻辑是先输入a[0],然后进入while里去判断,也可以用do~while替代
while(a[n]!=0){
n++;
cin>>a[n];
}
/*do{
cin>>a[n]; // 先输入,再判断
if(a[n] == 0) break;// 如果输入0,直接跳出循环
n++; // 只有输入非0,才让n++
}while(true);*/
//冒泡排序
for(i=0;i<n-1;i++){ //每轮 “沉” 1 个大数到正确位置,n个位置要做n-1轮的比较
for(j=0;j<n-1-i;j++){ //已经排好的 i 个数在数组末尾不用比了
if((a[j]%2==0 && a[j+1]%2==1)||a[j]%2==a[j+1]%2&&a[j]>a[j+1]){
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
for(i=0;i<n;i++){
cout<<a[i]<<" ";
}
return 0;
}

6.编写程序,输入字符串(不包含空格)和子串,统计该字符串中指定子串的个数。

样例输入:abababab bab 样例输出:3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include<iostream>
#include<cstring>
using namespace std;

int main(){
int i, j, k, sum=0;
char s1[100], s2[100];
cout<<"Enter main String:"<<endl;
cin>>s1;
cout<<"Enter sub String:"<<endl;
cin>>s2;

// 外层循环:遍历主串的每个起始位置(i是主串匹配起始下标)
for(i=0; s1[i]!='\0'; i++){
// 空循环(仅执行条件判断+自增,无循环体)
// 初始化:k=0(子串起始下标)、j=i(主串当前匹配下标)
// 循环条件:1.主串当前字符=子串当前字符 2.主串没到末尾
// 分号表示循环体为空,仅执行上面的“条件判断+自增”
for(k=0, j=i; (s1[j]==s2[k]) && (s1[j]!='\0'); j++, k++);

// 内循环结束后判断:如果k走到子串末尾(s2[k]='\0'),说明子串完全匹配
if(s2[k]=='\0'){
sum++; // 匹配成功,计数+1
}
}
cout<<"子串个数为:"<<sum<<endl;
return 0;
}

7.找出一个二维数组中的鞍点(可以假设二维数组为4行5列),即该位置上的元素在该行上最大,在该列上最小。(一个二维数组最多有一个鞍点,也可能没有)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include<iostream>
using namespace std;

int main()
{
// 假设数组为4行5列,用const固定
const int n = 4, m = 5;
int i, j, a[n][m], max, maxj; //maxj存最大值所在列号
bool flag; // 标记当前行的最大值是否是鞍点(这里很重要···)

cout << "请输入20个数组元素(4行5列):" << endl;
for (i = 0; i < n; i++) //二维数组输入方式
{
for (j = 0; j < m; j++)
{
cin >> a[i][j];
}
}
for (i = 0; i < n; i++)
{
// 假设当前行的第一个元素是最大值,记录其列号为0
max = a[i][0];
maxj = 0;

// 遍历当前行的所有列,找到真正的最大值及其列号
for (j = 0; j < m; j++)
{
if (a[i][j] > max)
{
max = a[i][j];
maxj = j;
}
}
// 假设当前行的最大值是鞍点,标记flag为true
flag = true;
// 遍历maxj列的所有行,验证max是否是该列的最小值
for (int k = 0; k < n; k++)
{
// 如果列中存在比max更小的数,说明max不是列最小值
if (max > a[k][maxj])
{
flag = false; // 标记为非鞍点
continue; // 无需继续比较,跳出当前列的遍历
}
}

// 如果flag为true,说明找到鞍点,输出并终止程序
if (flag)
{
cout << "鞍点:a[" << i << "][" << maxj << "] = " << max << endl;
break; // 停止遍历
}
}

// 遍历完所有行都没找到鞍点(flag为false),输出不存在
if (!flag)
{
cout << "It does not exist!" << endl;
}

return 0;
}
/*continue:跳过当前循环的剩余代码,直接进入下一次循环;break:直接终止整个列遍历循环*/

8.有n个整数,使前面各数顺序向后移m个位置,最后m个数变成最前面的m个数。如下图所示:写一函数实现以上功能,在主函数中输入n个整数,并输出调整后的n个数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include<iostream>
using namespace std;

// *arr :指向number数组的首地址
void move(int *arr, int n, int m)
{
// 处理m大于n
m = m % n;

// 定义临时数组,保存最后m个元素
int temp[20];
int i, j = 0; // j是临时数组的下标
// 原数组最后m个元素的起始下标:n -m
for (i = n - m; i < n; i++)
{
temp[j] = arr[i]; // 把最后m个元素挨个存进temp
j++; // 临时数组下标后移
}

// 把原数组前n-m个元素整体向后移动m位
// 注意:要从后往前移!否则会被覆盖
for (i = n - 1; i >= m; i--)
{
arr[i] = arr[i - m]; // 后移m位:下标i的位置 = 原下标i-m的值
}

// 把临时数组里的m个元素放回最前面
for (i = 0; i < m; i++)
{
arr[i] = temp[i]; // 临时数组的第i个元素 → 原数组第i个位置
}
}

int main()
{
int number[20], n, m, i;

cout << "元素个数是: ";
cin >> n;
cout << "input " << n << " numbers: " << endl;
for (i = 0; i < n; i++)
{
cin >> number[i]; // 挨个输入
}

cout << "后移步数:";
cin >> m;
move(number, n, m);
cout << "Now,they are:" << endl;
for (i = 0; i < n; i++)
{
cout << number[i] << " ";
}
cout << endl;//可省略

return 0;
}