重邮复试题
目录
加法器与累乘器
#include<iostream>
using namespace std;
void main()
{
int sum = 0;
int i;
for (int i = 0; i < 100; i++)
{
sum += i;
}
cout<<"从一加到一百的值为 "<< sum<<endl;
}
#include<iostream>
using namespace std;
void main()
{
int i;//局部变量
int p = 1;
int j;
printf("请输入j的值");
cin >> j;
for (int i = 1; i <= j; i++)
{
p = p * i;
}
cout<<"从一乘到"<<j<<"的值为:"<< p<<endl;
}
求最大数与最小数
#include<stdio.h>
int main()
{//求输入数字的最大值与最小值
int arr[100];
int n;
int min = 9999;
int max = -999;
printf("请输入需要比较的数字的个数");
scanf_s("%d", &n);
printf("请输入需要比较的数字");
for (int j = 0; j <= n; j++)
{
scanf_s("%d", &arr[j]);
}
for (int i = 0; i <= n; i++)
{
if (arr[i] > max)
max = arr[i];
else if(arr[i] < min)
min = arr[i];
}
printf("最小值为:%d", min);
printf("最大值为:%d", max);
return 0;
}
大小写字母转换
#include<iostream>
using namespace std;
void main()
{
//大写字母的ascall码值为65-90 小写字母为97-122
char a;
cout<<"请输入字母";
cin>>a;
if (a < 91)
{
a = a + 32;
printf("对应的小写字母为%c\n", a);
}
else
{
a = a - 32;
printf("对应的大写字母为%c\n", a);
}
}
判别键盘输入字符的类别
#include<iostream>
using namespace std;
void main()
{
char a;
cout<<"请输入字符: ";
a = getchar();
if (a < 32)
printf("控制字符\n");
else if (a >= 48 && a <= 57) //else if (ch <= '9' && ch >= '0')
printf("数字字符\n");
else if (a >= 65 && a <= 90) //if (ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z')
printf("大写字母\n");
else if (a >= 97 && a <= 122)
printf("小写字母\n");
else
printf("其他字符\n");
}
判别闰年
#include<iostream>
using namespace std;
void main()
{
int year, flag;
cout<<"please input a year:";
cin>>year;
if ((year % 4 == 0) && ((year % 100) != 0) || year % 400 == 0) { //能被4整除,但不能被100整除。或者能被400整除,flag=1
flag = 1;
}
else { //不是闰年,flag=0
flag = 0;
}
if (flag == 1) {
cout<<"yes";
}
else {
cout<<"no";
}
}
百分制成绩与等级制成绩互相转换
#include <stdio.h>
int main(){
int score;
scanf("%d",&score);
if(score>=95 && score<=100)
printf("A");
else if(score>=85 && score<90)
printf("B");
else if(score>=70 && score<85)
printf("C");
else if(score>=60 && score<70)
printf("D");
else if(score>=0 && score<60)
printf("E");
else
printf("Error");
return 0;
}
求两个数的最大公因数和最小公倍数
#include<iostream>
using namespace std;
void main()
{
int a = 0;
int b = 0;
int tmp = 0;
int m = 0;
cout<<"请输入两个整数:";
cin >> a >> b;
m = a * b;
while (tmp = a % b)
{
a = b;
b = tmp;
}
tmp = m / b;
cout << "最大公因数=" << b << endl;
cout << "最小公倍数=" << tmp;
}
求菲比拉契数列有限项
#include<iostream>
using namespace std;
//递归
int fibonac_1(int n)
{
if (n == 1 || n == 2)
{
return 1;
}
else
{
return fibonac_1(n - 1) + fibonac_1(n - 2);
}
}
//循环
int fibonac2(int n) {
int temp = 0, num1 = 1, num2 = 1;
for (int i = 0; i < n - 2; i++) {
temp = num1 + num2;
num1 = num2;
num2 = temp;
}
return temp;
}
int main()
{
int num = 5;
int number_1 = fibonac_1(num);
printf("递归方法:%d\n", number_1);
printf("循环方法:%d\n", fibonac2(num));
return 0;
}
验证哥德巴赫猜想
数学领域著名的“哥德巴赫猜想”的大致意思是:任何一个大于2的偶数总能表示为两个素数之和。比如:24=5+19,其中5和19都是素数(质数)。
任务是将输入的偶数n ,分解为两个素数的所有可能打印出来。
例如:
8 = 3 + 5.
20 = 3 + 17 20 = 7 + 13.
42 = 5 + 37 40= 11 + 31 40= 13 + 29 40= 19 + 23.
先分解成c=a+b;在依次对a和b进行判断是否是质数,如果同时是质数,就打印。
一个数是否是质数,就是一个数只能被1和它自身整除,没有其他因子。就需要尝试它能否被从2到它的平方根之间的数整除。
#include<iostream>
#include <math.h>
using namespace std;
int main()
{
int number;
int Firstnum;
int Secondnum;
int x, y;
int Temp;
cout << "Please input Number: ";
cin >> number;
for (x = 2; x < number/2; x++)
{
Firstnum = x;
Temp = sqrt(Firstnum) + 1;
for (y = 2; y < Temp; y++)
{
if (Firstnum % y == 0)
{
Firstnum = 0;
break;
}
}
Secondnum = number - x;
Temp = sqrt(Secondnum) + 1;
for (y = 2; y < Temp; y++)
{
if (Secondnum % y == 0)
{
Secondnum = 0;
break;
}
}
if (Firstnum && Secondnum)
printf("%d = %d + %d\r\n", number, Firstnum, Secondnum);
}
return 0;
}
统计学生成绩,包括总成绩、平均成绩、各分数段人数等
#include<iostream>
using namespace std;
void main()
{
int sum,n;
float ave;
sum = ave = 0;
cout << "请输入学生个数:";
cin >> n;
int stu[100];
int i;
int count1 = 0, count2 = 0, count3 = 0, count4 = 0, count5 = 0;
cout<<"请输入"<<n<<"位学生的成绩:";
for (i = 0; i < n; i++)
{
scanf_s("%d", &stu[i]); //n位学生成绩分别用空格隔开
sum = +stu[i];
if (stu[i] >= 90)
count1++;
else if (stu[i] >= 80)
count2++;
else if (stu[i] >= 70)
count3++;
else if (stu[i] >= 60)
count4++;
else
count5++;
}
ave = sum / n;
printf("%d %.2f", sum, ave);
cout << "总成绩:" << sum << endl;
cout << "平均成绩:" << ave << endl;
printf("90及90分以上有%d人\n", count1);
printf("80~89分有%d人\n", count2);
printf("70~79分有%d人\n", count3);
printf("60~69分有%d人\n", count4);
printf("60分以下有%d人\n", count5);
}
指针
#include<stdio.h>
int main() {
int a = 3, * p; // 定义整型变量a和整型指针p
p = &a; // 把变量a的地址赋给指针p,即p指向a
printf("a = %d, *p = %d\n", a, *p); // 输出变量a的值和指针p说指向变量的值
*p = 10; //对指针p所指向的变量赋值,相当于对变量a赋值
printf("a = %d, *p = %d\n", a, *p);
}
在进行指针初始化的时候需要注意一下几点。
在指针变量定义或者初始化时变量名前面的“ * ”只表示该变量是个指针变量,它既不是乘法运算又不是间接访问符。
把一个变量的地址作为初始化值赋给指针变量时,该变量必须在此之间已经定义。因为变量只有在定义后才被分配存储单元,它的地址才能赋给指针变量。
可以用初始化了的指针变量给另一个指针变量作初始化值。
不能用数值作为指针变量的初值,但可以将一个指针帮你初始化为一个空指针。例如:int p = 1000; 是不对的,而int p = 0; 是将指针变量初始化为空指针。这里0是ASCII字符NULL的值。
指针变量定义时的数据类型和它说指向的目标变量的数据类型必须一致,因为不同的数据类型所占用的存储单元的字节数不同。
用穷举法求某数段的素数、水仙花数、完全平方数等
用穷举法输出所有三位数的水仙花数(即每位数的立方和与该数相等,比如:1³+5³+3³=153)
#include<iostream>
#include<math.h>
using namespace std;
void zhishu(int z_number)
{
int temp;
int number;
for (int j = 2; j <= z_number ; j++)
{
number = j;
temp = sqrt(number) + 1;
for (int i = 2; i < temp; i++)
{
if (number % i == 0)
{
number = 0;
break;
}
}
if (number != 0)
cout << number << " ";
}
}
void waterflower(int s_number)
{
int a, b, c;
int count = 0;
for (int i = 100; i < s_number; i++)//穷举法求三位数水仙花数目
{
a = i / 100;
b = i % 100 / 10;
c = i % 10;
if (a * a * a + b * b * b + c * c * c == i)
{
printf("%d %d %d\n", a, b, c);
count++;
}
}
printf("三位数水仙花的数目为:%d\n", count);
}
void sqrt_num(int sqrtnum)
{
int a;
int count = 0;
for (int i = 1; i <= sqrtnum; i++)
{
for (int j = 1; j <= sqrtnum / 2 +1; j++)
{
if (j * j == i)
{
printf("%d * %d = %d\n", j, j, i);
count++;
}
}
}
printf("完全平方数目为:%d\n", count);
}
void main()
{
int z_number;
cout << "请输入数字:";
cin >> z_number;
cout << "质数为:";
zhishu(z_number);
printf("水仙花数为:\n");
waterflower(z_number);
printf("完全平方数为:\n");
sqrt_num(z_number);
}
两矩阵相乘
#include<stdio.h>
int main()
{
int i, j, k;
int matrix_2_3[2][3];
int matrix_3_2[3][2];
int result[2][2];
printf("请为第一个矩阵赋值:\n");
for (i = 0; i < 2; i++)
{
for (j = 0; j < 3; j++)
{
scanf_s("%d", &matrix_2_3[i][j]);
}
}
printf("请为第二个矩阵赋值:\n");
for (i = 0; i < 3; i++)
{
for (j = 0; j < 2; j++)
{
scanf_s("%d", &matrix_3_2[i][j]);
}
}
//三层循环计算两矩阵相乘
for (i = 0; i < 2; i++)
{
for (j = 0; j < 2; j++) //注意条件,这段代码块是为result数组计算并赋值
{
int temp = 0;
for (k = 0; k < 3; k++)
{
temp += matrix_2_3[i][k] * matrix_3_2[k][j];
}
result[i][j] = temp;
}
}
printf("两矩阵相乘后:\n");
for (i = 0; i < 2; i++)
{
for (j = 0; j < 2; j++)
{
printf("%d ", result[i][j]);
}
printf("\n");
}
}
统计字符中单词个数
#include<stdio.h>
int main() {
//定义一个string字符数组来接收输入的字符串
//定义一个c字符用来比较当前字符是否是空格
char string[100], c;
//count用来统计单词个数,
// 使用flag来标识如何为一个新的单词,为一个新单词的标准是前面的一个字符为空格,且初始值为0
int i, count = 0, flag = 0;
gets(&string);
//输入一个字符串给字符数组string
for (i = 0; (c = string[i]) != '\0'; i++) {
if (c == ' ')//如果是空格字符,使word置0
{
flag = 0;
}
else if (flag == 0)//如果不是空格字符,且flag原来的值为0,即前面是空格,使flag置1
{
flag = 1;
count++;
}
}
printf("count=%d\n", count);
return 0;
}
求定积分近似值
#include<stdio.h>
#include<math.h>
int main()
{
int i, n = 100;
float s1=0, h, x, a, b, t1, t2;
printf("请输入定积分区间");
scanf_s("%f,%f", &a, &b);
h = (b - a) / n;
for (int i = 1; i <= n; i++)
{
x = a + (i - 1) * h;
t1 = (float)exp(-x * x / 2);//f(x)
t2 = exp(-(x + h) * (x + h) / 2);//f(x+h)
s1= s1 + (t1 + t2) * h / 2;
}
printf("梯形法算得积分值:%f\n", s1);
}
牛顿迭代法求近似值
#include<stdio.h>
#include<math.h>
float Fl(float x) {
float y;
y = cos(x) - x;//y=f(x)
return y;
}
float newtoon(float x)
{
float y;
float d;
d = Fl(x) / (-sin(x) - 1);//y=f'(x)
y = x - d;
return y;
}
void main()
{
float x0, x1;
printf("Please input x0:\n");
scanf_s("%f", &x1);
do
{
x0 = x1;
x1 = newtoon(x0);
} while (fabs(x1 - x0) >= 1e-5);
printf("The root of equation is %f\n", x1);
}
二分法
#include<iostream>
#include<math.h>
#include<iomanip>
using namespace std;
#define f(x) ((x*x-1)*x-1)
void main()
{
float a = 1, b = 1.5, y = f(a), x;
int i = 0;
if (y * f(b) >= 0) // 同号 无解
{
printf("输入范围有误!");
return;
}
else
do
{
x = (a + b) / 2;
if (f(x) == 0)
break;
if (y * f(x) < 0)
b = x;
else
a = x;
} while (fabs(a - b) > 1e-2);
printf("近似值为:%.4f", x);
//cout<< fixed << setprecision(4) << x <<endl;
}
读写操作
#include<iostream>
#include<stdlib.h>
using namespace std;
void main() {
FILE* fin, * fout;
char ch, filename[10], str[2][10];
cout << "请输入文件名称:";
cin >> filename;
cout << "请输入:";
for (int i = 0; i < 2; i++)
cin >> str[i];
if ((fin = fopen(filename, "r")) == NULL) {
cout << "无法打开此文件!" << endl;
exit(0);
}
if ((fout = fopen("E:\\数据结构\\作业\\lyx.txt", "w")) == NULL) { // \为转义字符,在“‘中需要写成 \\
cout << "无法打开此文件!" << endl;
exit(0);
}
ch = fgetc(fin);
while (ch != EOF) {
cout << ch;
fputc(ch, fout);
ch = fgetc(fin);
}
fputs("\n", fout);
for (int i = 0; i < 2; i++) {
//fputs("\n", fout);
fputs(str[i], fout); fputs("\n", fout);
cout<<" " << str[i] ;
}
fclose(fin);
fclose(fout);
}
编程输出一个字符串的所有排列
示例:
Input a string:ABC↙
ABC
ACB
BAC
BCA
CBA
CAB
#include <stdio.h>
#include <string.h>
void diaprange(char* str, int k);
void swap(char* str, int k, int i);
int main()
{
char s[40];
printf("Input a string: ");
gets_s(s);
diaprange(s, 0);
return 0;
}
/* 显示字符串全排列函数 */
void diaprange(char* str, int k)
{
int i, len;
len = strlen(str);
/* 如果需要保持不变的字符长度为字符串长度,则直接显示该字符串 */
if (k == len)
printf("%s\n", str);
else
{
for (i = k; i < len; i++) /* 对k之后的每个字符进行排列 */
{
swap(str, k, i); /* 交换i和k的字符 */
diaprange(str, k + 1); /* 生成前k+1个字符固定时的所有排列 */
swap(str, k, i); /* 重新交换i和k的字符,恢复原先的字符串 */
}
}
}
/* 交换指定位置两个字符函数 */
void swap(char* str, int k, int i)
{
char temp;
temp = str[k];
str[k] = str[i];
str[i] = temp;
}
输出螺旋矩阵
#include<stdio.h>
using namespace std;
int main()
{
int i, j, k = 0, n, a[10][10], m;
printf("Enter n(n<10)");
scanf_s("%d", &n);
if ((n % 2) == 0)
m = n / 2;
else
m = n / 2 + 1;
for (i = 0; i <m; i++)//按螺旋方阵的层数循环,从最外层一层层开始输出
{
for (j = i; j < n - i; j++)
a[i][j] = ++k;
for (j = i + 1; j < n - i; j++)
a[j][n - i - 1] = ++k;
for (j = n - i - 2; j >= i; j--)
a[n - i - 1][j] = ++k;
for (j = n - i - 2; j >= i + 1; j--)
a[j][i] = ++k;
}
for (i = 0;i<n;i++)
{
for (j = 0;j<n;j++)
printf("%5d", a[i][j]);
printf("\n");
}
}
用弦截法求方程 f(x)=x3-5x2+16x-80=0 的根
(1) 取两个不同点x1,x2,如果f(x1)和f(x2)符号相反,则(x1,x2)区间内必有一个根。如果f(x1)与f(x2)同符号,则应改变x1,x2,直到f(x1)、f(x2)异号为止。注意x1、x2的值不应差太大,以保证(x1,x2)区间内只有一个根。
(2) 连接(x1,f(x1))和(x2,f(x2))两点,此线(即弦)交x轴于x。
(3) 若f(x)与f(x1)同符号,则根必在(x,x2)区间内,此时将x作为新的x1。如果f(x)与f(x2)同符号,则表示根在(x1,x)区间内,将x作为新的x2。
(4) 重复步骤 (2) 和 (3) , 直到 |f(x)|<ε 为止, ε为一个很小的数, 例如 10-6. 此时认为 f(x)≈0 。
N-S流程图
#include <stdio.h>
#include <math.h>
float f(float x); /* 自定义函数声明 */
float xpoint(float x1,float x2);
float root(float x1,float x2);
void main()
{ float x1,x2,f1,f2,x;
/* 输入x1和x2并保证其函数值反号 */
do
{ printf("Input x1,x2:\n");
scanf("%f,%f",&x1,&x2);
f1=f(x1); /* 计算x1的函数值并赋给f1 */
f2=f(x2);
}while(f1*f2>=0);
x=root(x1,x2); /* 调用求根函数 */
printf("A root of equation is %8.4f",x);
}
/* 定义f函数,求f(x)的值 */
float f(float x)
{ float y;
y=((x-5.0)*x+16.0)*x-80.0; /* 计算f(x)的值 */
return(y);
}
/* 定义xpoint函数,求弦与x轴的交点 */
float xpoint(float x1,float x2)
{ float x;
x=(x1*f(x2)-x2*f(x1))/(f(x2)-f(x1));
return(x);
}
/* 定义root函数,求近似根 */
float root(float x1,float x2)
{ float x,y,y1;
y1=f(x1);
do
{ x=xpoint(x1,x2);
y=f(x);
if (y*y1>0) /* 新的f(x)与f(x1)同号时,用x替换x1,否则替换x2 */
{ y1=y;
x1=x;
}else
x2=x;
}while(fabs(y)>=0.0001); /* 当f(x)约等于0时退出循环 */
return(x);
}
三阶魔方阵
魔方阵:一个N阶魔方阵即N行N列的数组,其每一行的和每一列的和对角线的和。
三阶(奇数)魔方阵填充规则:
1.1放在第一行中间
2.当前数字放在前一个数字的上一行后一列
3.如果当前位置已经有值,则放在前一个数字的下一行同列
有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?
unsigned int rabbits(unsigned int month)
{
if ( month==2 || month==1 )
return 2;
return rabbits(month-1)+rabbits(month-2);
}