01
:输入自己的个人信息,并输出控制台
# include <iostream>
using namespace std;
int main()
{
cout << "学号 :102201118" << endl;
cout << "姓名 :陆旭东" << endl;
cout << "专业 :计算机类" << endl;
cout << "性别 :男" << endl;
system("pause");
return 0;
}
/*
运行结果:
学号 :102201118
姓名 :陆旭东
专业 :计算机类
性别 :男
*/
//ctrl k c注释 ctrl k u取消注释
02
:判断是否是闰年
# include <iostream>
using namespace std;
int main()
{
int year;
cin >> year;
if (year % 4 == 0)
{
if (year % 100 != 0 || year % 400 == 0)
cout << year << "年是闰年" << endl;
else
cout << year << "年不是闰年" << endl;
}
else
cout << year << "年不是闰年" << endl;
system("pause");
return 0;
}
//运行结果:
/*
0
0年是闰年
100
100年不是闰年
2000
2000年是闰年
*/
03
:输入一个正整数,反转这个数字
# include <iostream>
# include <math.h>
using namespace std;
int main()
{
int nump, nump2, numb = 0;//nump表示为反转前的数字 numb表示反转后的数字
int i = 0,j;//i存放表示nump的位数
cin >> nump;
nump2 = nump;
while (nump2 > 0)
{
nump2 /= 10;
i++;
}
nump2 = nump;
j = pow(10,i - 1);
while (nump2 > 0)
{
numb += (nump2 % 10) * j;
j /= 10; nump2 /= 10;
}
cout << numb << endl;
system("pause");
return 0;
}
/*
运行结果:
123
321
100
1
*/
04
:求给定两个正整数的最大公约数和最小公倍数
# include <iostream>
using namespace std;
int main()
{
int m, n;
cin >> m;
cin >> n;
int y = 1, b, i;//y 存放最大公因数 b 存放最小公倍数
for (i = 2; i <= n && i <= m; i++)
{
if (m % i == 0 && n % i == 0)
y = i;
}
for (i = (n > m ? n : m);; i++)
if (i % m == 0 && i % n == 0)
break;
b = i;
cout << y << " " << b << endl;
system("pause");
return 0;
}
/*
运行结果:
12 88
4 264
100001 11
11 100001
*/
05
:输出前1000的素数
# include <iostream>
# include <math.h>
# include <iomanip>
using namespace std;
//输出前1000 的素数
int main()
{
int i, j;
for (i = 2; i <= 1000; i++)
{
for (j = 2; j < sqrt(i); j++)
{
if (i % j == 0)
break;
}
if (j == (int)sqrt(i) + 1)
cout << setw(3) << i <<" " ;
}
cout << endl;
system("pause");
return 0;
}
/*
运行结果:
2 3 5 7 11 13 17 19 23 29 31 37 41
43 47 53 59 61 67 71 73 79 83 89 97 101
103 107 109 113 127 131 137 139 149 151 157 163
167 173 179 181 191 193 197 199 211 223 227 229
233 239 241 251 257 263 269 271 277 281 283 293
307 311 313 317 331 337 347 349 353 359 367 373
379 383 389 397 401 409 419 421 431 433 439 443
449 457 461 463 467 479 487 491 499 503 509 521
523 541 547 557 563 569 571 577 587 593 599 601
607 613 617 619 631 641 643 647 653 659 661 673
677 683 691 701 709 719 727 733 739 743 751 757
761 769 773 787 797 809 811 821 823 827 829 839
853 857 859 863 877 881 883 887 907 911 919 929
937 941 947 953 967 971 977 983 991 997
*/
06
:输出杨辉三角前n行
# include <iostream>
# include <iomanip>
using namespace std;
int fact(int n);
int spa(int n);
int main06()
{//输出杨辉三角的前n行
int n, n1, i, j;
cin >> n;//n代表输出的行数
for (i = 0,n1 = n; i < n ;i++, n1--)
{ // i控制第(i+1)行的输出
spa(n1);//使用占位函数填充首元素前的位置
for (j = 0; j <= i; j++)
{
cout << setw(6) << fact(i) / (fact(j) * fact(i - j)) << " ";
if (j == i) cout << endl;
}
}
system("pause");
return 0;
}
int fact(int n)
{//求阶乘的函数
int i = 1, sum = 1;
while (i <= n)
{
sum = sum * i;
i++;
}
return sum;
}
int spa(int n)
{//此函数作用是占位 为了输出对齐的杨辉三角
/*可以将n行的杨辉三角金字塔看做放在 2n-1 * n 的矩形中
每两个元素之间由空格填充
要让输出结果为等腰三角形 第i行第一个元素前须输出(n - i)组个空格*/
if (n == 0)
return 0;
int i = 1;
while (i < n)
{
cout << " ";/*
" "内表示一组空格 考虑到若只输出一个空格由于
杨辉三角数值占位可能不止一位会导致三角形排列混乱
*/
i++;
}
}
/*
运行结果:
10
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
*/
07
:统计单词数,输入要查询的单词以及一段短文,查询单词出现的次数,不区分大小写
# include <iostream>
# include <cstring>
# include <cctype>
using namespace std;
void in(char*);//输入带空格的字符串
int main()
{
char a[300], b[15], c[15];//a存放短文 c存放目标单词 b存放待检测单词
//scanf("%[^\n]%*c", a);
//scanf("%s", c);//将c中的字母全部转化为大写
in(a);
in(c);
char* pa = a;
int lena, lenc;//为什么给c输入hhh lenc的值是4???
lena = strlen(a);
lenc = strlen(c);
for (int l = 0; l < lenc; l++)
{
c[l] = toupper(c[l]);
}
for (int l = 0; l < lena; l++)
{
a[l] = toupper(a[l]);
}
//由于不区分大小写 都转换为大写形式
int i, j = 0, sum = 0;
a[lena] = ' ';//因为检测是以空格为分隔点 故将有效元素的后一个元素赋值为空格
for (i = 0, j = 0; i <= lena; i++, j++)
{
if (a[i] != ' ' && a[i] != '.' && a[i] != ',')
{
b[j] = a[i];
b[j + 1] = '\0';//不知道是否自动加 \0 前面若输出he you he 统计结果为1 猜测you三个字符 he无法将其覆盖 如果不加这句结果为0 加了结果为一
//是不是c++把\0也当做有效元素比较?******不是 有效
//故每次赋值后将下一个元素赋值为 /0
}
else
{
j = -1;//执行完会执行i++ j++ 保证下一次操作时j = 0 ***改为j = 0 结果又变成了0
if (strcmp(c, b) == 0)
{
sum = sum + 1;
}
b[0] = '\0';//防止多个相连的空格导致sum重复++ **1** 有效
}
cout << sum << endl;
system("pause");
return 0;
}
void in(char* a)
{
int i = 0;
a[0] = cin.get();
while (a[i] != '\n')
{
i++;
a[i] = cin.get();
a[i + 1] = '\0';//提示访问超出数组空间 查找len值为424 不明白 加上\0解决
}
if (a[i] == '\n')
a[i] = '\0';//这一步很重要 万恶之源!!!!开始执行结果一直是1 然后输出strcmp函数的执行结果 1 1 1 0
//只有最后一个相等 发现问题出在in函数 我的顺序是先给a[i]赋值后再判断 导致最后输入的换行符被赋值给了a[i]
//所以c[i]的值为 l e n '\n' '\0'
}
/*
输出结果:
when you are olf and gray and full of sleep,and nodding by the fire,take down this book,and slowly read,and dream of the soft look your eyes had once,and of their shadows deep how many loved your moments of gald grace,and loved your moments of beauty with love false or true,but one man loved the sorrows of your changing face
you
1
*/
08
:输入一个正整数,判断它是否为回文质数
# include <iostream>
# include <cmath>
using namespace std;
int iszhishu(int);
int ishuiwen(int);
int main()
{
int n;
cin >> n;
int t1, t2;
t1 = iszhishu(n);
t2 = ishuiwen(n);
if (t1 * t2 == 1)
cout << n << "是回文质数 " << endl;
else
cout << n << "不是回文质数" << endl;
system("pause");
return 0;
}
int iszhishu(int n)
{
int i;
if (n == 1)
return 0;
else if (n == 2)
return 1;
else
{
for ( i = 2; i <= n / 2; i++)
{
if (n % i == 0)
break;
}
if (i > n / 2)
return 1;
else
return 0;
}
}
int ishuiwen(int n)
{
int sum = 0;
int n1 = n;
//先判断一个数有几位
while (n1)
{
n1 = n1 / 10;
sum++;//sum为n的位数
}
//printf("%d\n", sum);
//接下来需要找到除数
int sum1, s = 1;
sum1 = sum / 2;
s = pow(10, sum1-1);
//sum代表位数 n代表原数 s代表除数
//接下来提取后半部分
int x1 = 0,s1 = s; n1 = n;
for (; sum1 > 0; sum1--, s1 /= 10)
{
x1 = x1 + (n1 % 10) * s1;
n1 /= 10;
}
//成功提取后半
int x2;
if (sum % 2 == 0)
{
x2 = n / (s * 10);
}
else
{
x2 = n / (s * 100);
}//奇数位去除最中间的数
if (x2 == x1)
return 1;
else
return 0;
}
//输出结果:
/*
151
151是回文质数
100001
100001不是回文质数
*/
09
:简单的栈
# include <iostream>
using namespace std;
typedef int ElemType;
struct SingleNode
{
ElemType data;
struct SingleNode* next;
};
typedef struct SingleNode Stack;
void initStack(Stack* S, ElemType item);//初始化栈
void push(Stack* S, ElemType item);//将元素压入栈
ElemType pop(Stack* S);//删除栈顶元素并返回
ElemType peek(Stack* S);//访问栈顶元素并返回
int emptyStack(Stack* S);//判断栈是否为空
void clearStack(Stack* S);//清空栈
int main09()
{
int i, x, y, z;
int a[8] = { 3,8,5,17,9,30,15,22 };
Stack r, * S = &r;
initStack(S, 1);
for (i = 0; i < 8; i++)
push(S, a[i]);
x = pop(S);
y = pop(S);
z = pop(S);
cout << x << " ";
cout << y << " ";
cout << z << " ";
cout << endl;
push(S, 68);
x = peek(S);
y = pop(S);
z = peek(S);
cout << x << " ";
cout << y << " ";
cout << z << " ";
cout << endl;
push(S, 60);
while (!emptyStack(S))
//printf("%d ", pop(S));
cout << pop(S) << " " ;
//printf("\n");
cout << endl;
clearStack(S);
system("pause");
return 0;
}
void initStack(Stack* S, ElemType item)
{
//初始化链栈 S指向节点的next域保存栈顶指针
S->next = NULL;
}
void push(Stack* S, ElemType item)
{//向栈内插入元素
Stack* p = (Stack*)malloc(sizeof(struct SingleNode));
p->data = item;
p->next = S->next;//将p指向节点的next域赋为null
S->next = p;//把地址p赋值给栈顶指针
}
ElemType pop(Stack* S)
{//删除栈顶元素
if (S->next == NULL)
{
printf("栈为空");
exit(1);
}
else
{
Stack* p = S->next;
ElemType x = p->data;
S->next = p->next;
free(p);
return x;
}
}
ElemType peek(Stack* S)
{//读取栈顶元素
if (S->next == NULL)
{
printf("栈为空!");
}
else
return S->next->data;
}
int emptyStack(Stack* S)
{//判断栈是否为空
return (S->next == NULL);
}
void clearStack(Stack* S)
{//清空栈
Stack* p = S->next;
while (p != NULL)
{
Stack* q = p->next;
free(p);
p = q;
}
S->next = NULL;
}
//D:\C study\collect2.exe [Error] ld returned 1 exit status报错
//原因 函数名定义时敲错了stasck
//在vs2022中的输出结果是:
/*
22 15 30
68 68 9
60 9 17 5 8 3
*/
10
:用指针和引用实现冒泡排序
# include <iostream>
using namespace std;
void change1(int *, int *);
void change2(int&, int&);
int main()
{//用指针和引用实现冒泡排序
int x1 = 2, x2 = 4;
int a[10] = { 1,5,7,6,9,3,4,2,0,8};
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < (9-i); j++)
{
if (a[j] > a[j + 1])
change2(a[j], a[j + 1]);
}
}
for (int i = 0; i < 10; i++)
printf("%d ", a[i]);
system("pause");
return 0;
}
void change1(int* p1, int* p2)
{
int temp;
temp = *p1; *p1 = *p2; *p2 = temp;
}
void change2(int& x1, int& x2)
{
int temp;
temp = x1; x1 = x2; x2 = temp;
}
//输出结果:
/*
0 1 2 3 4 5 6 7 8 9
*/