1.打印杨辉三角
1 #include <stdio.h>
2 #include <string.h>
3
4 int main()
5 {
6 int x;
7 int a[100][100];
8 printf("输入行数\n");
9 scanf("%d",&x);
10 for(int i = 0;i<x;i++)
11 {
12 for(int j = 0;j<x;j++)
13 {
14 a[i][j] = 0;
15 }
16 }
17
18 for(int i = 0;i<x;i++)
19 {
20 a[i][0] = 1;
21 }
22
23 for(int i = 1;i<x;i++)
24 {
25 for(int j = 1;j<=i;j++)
26 {
27 a[i][j] = a[i-1][j] + a[i-1][j-1];
28 }
29 }
30
31 for(int i = 0;i<x;i++)
32 {
33 for(int j = 0;j<=i;j++)
34 {
35 printf("%d ",a[i][j]);
36 }
37 printf("\n");
38 }
39
40 return 0;
41 }
2.斐波那契数列
1 #include <stdio.h>
2 #include <string.h>
3
4 int func(int n)
5 {
6 if(0 == n) return 0;
7 if(1 == n) return 1;
8 else
9 return func(n-2)+func(n-1);
10 }
11
12 int main()
13 {
14 int n;
15 scanf("%d",&n);
16 printf("%d\n",func(n));
17
18 return 0;
19 }
3.请使用递归算法编写求N的阶乘函数
1 #include <stdio.h>
2 #include <string.h>
3
4 int func(int n)
5 {
6 if(1 == n) return 1;
7 return n * func(n-1);
8 }
9
10 int main()
11 {
12 int n;
13 scanf("%d",&n);
14 printf("%d\n",func(n));
15
16 return 0;
17 }
4.输入两个正整数 m 和 n,求其最大公约数和最小公倍数
1 #include <stdio.h>
2 #include <string.h>
3
4 int main()
5 {
6 int x,y,z,j;
7 scanf("%d%d",&x,&y);
8 if(x>y)
9 {
10 z = x;
11 }else{
12 z = y;
13 }
14
15 for(int i = z;i>0;i--)
16 {
17 j = i;
18 if(0 == x%i && 0 == y%i)
19 {
20 break;
21 }
22 }
23 printf("最大公约数为:%d\n",j);
24 printf("最小公倍数为:%d\n",(x*y)/j);
25
26 return 0;
27 }
5.判断从101到200间有多少个素数,并输出
1 #include <stdio.h>
2 #include <string.h>
3
4 int main()
5 {
6 int i,j;
7 for(i = 101;i<=200;i++)
8 {
9 for(j = 2;j<i;j++)
10 {
11 if(0 == i%j)
12 {
13 break;
14 }else{
15 if(j == i-1)
16 {
17 printf("%d\n",i);
18 }
19 }
20 }
21 }
22
23 return 0;
24 }
6.写出一个程序,接受一个由字母和数字组成的字符串,和一个字符,然后输出输入字符串中含有该字符的个数。不区分大小写
1 #include <stdio.h>
2 #include <string.h>
3
4 int main()
5 {
6 int len,cout = 0;
7 char a[] = "ABCDEFGAa";
8 char b = 'a';
9 len = strlen(a);
10 for(int i = 0;i<len;i++)
11 {
12 if(a[i] == b || a[i]-32 == b || a[i]+32 == b)
13 {
14 cout++;
15 }
16 }
17 printf("%d\n",cout);
18
19 return 0;
20 }
7.打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。
例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。
1 #include <stdio.h>
2 #include <string.h>
3
4 int main()
5 {
6 int i,a,b,c,cout = 0;
7 printf("打印100-999水仙花个数\n");
8 for(i = 100;i<999;i++)
9 {
10 a = i/100;
11 b = i/10 %10;
12 c = i%10;
13 if(i == (a*a*a)+(b*b*b)+(c*c*c))
14 {
15 cout++;
16 printf("%d ",i);
17 }
18 }
19 printf("水仙花个数为:%d\n",cout);
20
21 return 0;
22 }
8.输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
1 #include <stdio.h>
2 #include <string.h>
3
4 int main()
5 {
6 int i = 0,b = 0,c = 0,d = 0,e = 0;
7 char a[] = "ASsfDGG& adS A18";
8 while(a[i] != '\0')
9 {
10 if(a[i] >= '1' && a[i] <= '9')
11 {
12 b++;
13 }
14 else if(a[i] >= 'a' && a[i] <= 'z' || a[i] >= 'A' && a[i] <= 'Z')
15 {
16 c++;
17 }
18 else if(a[i] == ' ')
19 {
20 d++;
21 }
22 else
23 {
24 e++;
25 }
26 i++;
27 }
28 printf("数字的个数为:%d,字母的个数为:%d,空格的个数为:%d,其他符号的个数为:%d",b,c,d,e);
29
30 return 0;
31 }
9.输出9*9口诀。
1 #include <stdio.h>
2 #include <string.h>
3
4 int main()
5 {
6 int i,j,num;
7 printf("输出9*9乘法口诀\n");
8 for(i = 1;i<=9;i++)
9 {
10 for(j = 1;j<=i;j++)
11 {
12 num = i * j;
13 printf("%d * %d = %d ",i,j,num);
14 }
15 printf("\n");
16 }
17
18 return 0;
19 }
10.用*打印菱形图案
1 #include <stdio.h>
2 #include <string.h>
3
4 int main()
5 {
6 int i,j,k;
7 for(i = 1;i<=4;i++)
8 {
9 for(j = 0;j<4-i;j++)
10 {
11 printf(" ");
12 }
13 for(k = 0;k<(2*i)-1;k++)
14 {
15 printf("*");
16 }
17 printf("\n");
18 }
19
20 for(i = 1;i<=3;i++)
21 {
22 for(j = 0;j<i;j++)
23 {
24 printf(" ");
25 }
26 for(k = 0;k<7-(2*i);k++)
27 {
28 printf("*");
29 }
30 printf("\n");
31 }
32
33 return 0;
34 }
11.题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?有多少个。
1 #include <stdio.h>
2 #include <string.h>
3
4 int main()
5 {
6 int a[] = {1,2,3,4};
7 int i,j,k,cout = 0;
8 for(i = 0;i<4;i++)
9 {
10 for(j = 0;j<4;j++)
11 {
12 for(k = 0;k<4;k++)
13 {
14 if(i != j && j != k && i != k)
15 {
16 printf("%d%d%d ",a[i],a[j],a[k]);
17 cout++;
18 }
19 }
20 }
21 printf("\n");
22 }
23 printf("可以组成%d个互不相同且无重复数字的三位数\n",cout);
24 return 0;
25 }
12.求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加有键盘控制。
1 #include<stdio.h>
2
3 int main()
4 {
5 int a,n,s = 0,b;
6 printf("请输入相加个数n和加数a\n");
7 scanf("%d%d",&a,&n);
8 printf("s = %d ",a);
9 b = a;
10 for(int i = 0;i<n-1;i++)
11 {
12 s = s + a;
13 a = b + (a * 10);
14 printf("* %d ",a);
15 }
16 s = s + a;
17 printf(" = %d",s);
18
19 return 0;
20 }
13.一个数如果恰好等于它的因子之和,这个数就称为"完数"。例如6=1+2+3.编程 找出1000以内的所有完数。
https://blog.csdn.net/m0_55028858/article/details/125577635
1 #include<stdio.h>
2
3 int main()
4 {
5 int s;
6 for(int i = 2;i<1000;i++)
7 {
8 s = 0;
9 for(int j = 1;j<i-1;j++)
10 {
11 if(0 == i%j)
12 {
13 s = s + j;
14 }
15 }
16 if(i == s)
17 {
18 printf("%d是完数\n",s);
19 }
20 }
21 return 0;
22 }
14.一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在第10次落地时,共经过多少米?第10次反弹多高?
https://blog.csdn.net/qq_45385706/article/details/110697089
1 #include<stdio.h>
2
3 int main()
4 {
5 double s = 100,h = s/2,k = 0;
6 for(int i = 0;i<9;i++)
7 {
8 k = k + (2 * h);
9 h = h/2;
10 }
11 k = k + s;
12 printf("总共经过%lf米,第10次反弹的高度为%lf",k,h);
13
14 return 0;
15 }
15.猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个第二天早上又将剩下的桃子吃掉一半,又多吃了一个。
以后每天早上都吃了前一天剩下的一半零一个。到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少。
1 #include<stdio.h>
2
3 int main()
4 {
5 int y = 1;
6 for(int i = 0;i<9;i++)
7 {
8 y = (y + 1) * 2;
9 }
10 printf("第一天总共有%d颗桃子\n",y);
11
12 return 0;
13 }
16.有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前20项之和。
1 #include<stdio.h>
2
3 int main()
4 {
5 double x = 2,y = 1,s = 0,x1 = 0;
6 for(int i = 0;i<20;i++)
7 {
8 s = s + (x/y);
9 x1 = x;
10 x = x + y;
11 y = x1;
12 }
13 printf("前20项的和为%lf\n",s);
14
15 return 0;
16 }
17.一个5位数,判断它是不是回文数。即12321是回文数,个位与万位相同,十位与千位相同
https://blog.csdn.net/daonanya/article/details/123506362
1 #include<stdio.h>
2
3 int main()
4 {
5 int a = 12321,b,c,d,e;
6 b = a/10000;
7 c = (a%10000)/1000;
8 d = (a%100)/10;
9 e = a%10;
10 if(b == e && c == d)
11 {
12 printf("是回文数\n");
13 }
14 else
15 {
16 printf("不是回文数\n");
17 }
18
19 return 0;
20 }
18.两数之和
https://blog.csdn.net/azulgrana02/article/details/109644046
1 #include<iostream>
2 #include<vector>
3 #include<unordered_map>
4 using namespace std;
5
6 class node{
7 public:
8 vector<int> twosun(vector<int>& nums,int target)
9 {
10 unordered_map<int,int> record;
11 for(int i = 0;i<nums.size();i++){
12 int num = target - nums[i];
13 if(record.find(num) != record.end()){
14 return {record[num],i};
15 }
16 record[nums[i]] = i;
17 }
18 return {-1,-1};
19 }
20 };
21
22 int main()
23 {
24 node n;
25 vector<int> cur;
26 vector<int> nums = {2,7,11,15};
27 cur = n.twosun(nums,9);
28 for (auto i : cur)
29 cout << i << endl;
30
31 return 0;
32 }
19.整数反转
1 #include <iostream>
2 #include <vector>
3 using namespace std;
4
5 class node{
6 public:
7 int reverse(int x){
8 int ans = 0;
9 while(x){
10 ans = ans*10 + x%10;
11 x /= 10;
12 }
13 return ans;
14 }
15 };
16
17 int main()
18 {
19 node n;
20 cout << n.reverse(-123) << endl;
21
22 return 0;
23 }
20.合并区间(力扣56题)
1 #include <iostream>
2 #include <vector>
3 #include<algorithm>
4 using namespace std;
5
6 class node{
7 public:
8 vector<vector<int>> merge(vector<vector<int>>& cur){
9 vector<vector<int>> ans;
10 sort(cur.begin(),cur.end());
11 int strat = cur[0][0],end = cur[0][1];
12 for(int i = 1;i<cur.size();i++){
13 if(cur[i][0]>end){
14 ans.push_back({strat,end});
15 strat = cur[i][0];
16 end = cur[i][1];
17 }else{
18 end = max(end,cur[i][1]);
19 }
20 }
21 ans.push_back({strat,end});
22 return ans;
23 }
24 };
25
26 int main()
27 {
28 node n;
29 vector<vector<int>> top;
30 vector<vector<int>> tem;
31 tem.push_back({1,3});
32 tem.push_back({2,6});
33 tem.push_back({8,10});
34 tem.push_back({15,18});
35 top = n.merge(tem);
36 int x = top.size(),y = top[0].size();
37 for(int i = 0;i<x;i++){
38 for(int j = 0;j<y;j++){
39 cout << top[i][j] << " ";
40 }
41 cout << endl;
42 }
43
44 return 0;
45 }
21.插入区间(力扣57题)
1 #include <iostream>
2 #include <vector>
3 #include<algorithm>
4 using namespace std;
5
6 class node{
7 public:
8 vector<vector<int>> insert(vector<vector<int>>& a,vector<int>& b){
9 vector<vector<int>> ans;
10 int n = a.size(),i = 0;
11 while(i<n && a[i][1]<b[0]){
12 ans.push_back(a[i++]);
13 }
14 if(i<n){
15 b[0] = min(a[i][0],b[0]);
16 while(i<n && a[i][0]<=b[1]){
17 b[1] = max(a[i++][1],b[1]);
18 }
19 }
20 ans.push_back(b);
21 while(i<n){
22 ans.push_back(a[i++]);
23 }
24 return ans;
25 }
26 };
27
28 int main()
29 {
30 node n;
31 vector<int> tur ={2,5};
32 vector<vector<int>> tem;
33 tem.push_back({1,3});
34 tem.push_back({6,9});
35 vector<vector<int>> top;
36 top = n.insert(tem,tur);
37 int x= top.size(),y = top[0].size();
38 for(int i = 0;i<x;i++){
39 for(int j = 0;j<y;j++){
40 cout << top[i][j] << " ";
41 }
42 cout << endl;
43 }
44
45 return 0;
46 }
22.加一(力扣66题)给定一个数组,在原数组的基础上加一、
1 #include <iostream>
2 #include <vector>
3 using namespace std;
4
5 class node{
6 public:
7 vector<int> piusone(vector<int>& cur){
8 for(int i = cur.size()-1;i>=0;i++){
9 if(cur[i]<9){
10 cur[i]++;
11 break;
12 }else{
13 cur[i] = 0;
14 if(i==0){
15 cur.insert(cur.begin(),1);
16 }
17 }
18 }
19 return cur;
20 }
21 };
22
23 int main()
24 {
25 node n;
26 vector<int> tem;
27 vector<int> top = {1,2,3};
28 tem = n.piusone(top);
29 for(int i = 0;i<tem.size();i++){
30 cout << tem[i] << " ";
31 }
32
33 return 0;
34 }