1、水仙花数
题目:打印出所有的“水仙花数”,所谓“水仙花数”是指一个三位数,其各位数字立方和等于该数
本身。例如:153是一个“水仙花数”,因为153=1的三次方+5的三次方+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
29
30
31
32
|
方法一: #include <stdio.h> int main( int argc, const char *argv[]) { for ( int i=1;i<10;i++){ for ( int j=0;j<10;j++){ for ( int k=0;k<10;k++){ if (i*i*i+j*j*j+k*k*k==i*100+j*10+k) printf ( "%d\n" ,i*100+j*10+k); } } } return 0; } 方法二: #include <stdio.h> int main() { int i,j,k,n; printf ( "'water flower'number is:" ); for (n=100;n<1000;n++) { i=n/100; /*分解出百位*/ j=n/10%10; /*分解出十位*/ k=n%10; /*分解出个位*/ if (i*100+j*10+k==i*i*i+j*j*j+k*k*k) { printf ( "%-5d" ,n); } } printf ( "\n" ); } |
2、整型数组内函数求和。
使用函数封装,实现一个整型数组内数据的求和。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
#include <stdio.h> int array_sum( int *data, int n); int main( int argc, const char *argv[]) { int a[]={1,2,3,4,5,6,7,8}; int sum=0; sum=array_sum(a, sizeof (a)/ sizeof ( int )); printf ( "sum=%d\n" ,sum); return 0; } int array_sum( int *data, int n){ int ret=0; int i; for (i=0;i<n;i++){ ret+=data[i]; } |
3、斐波那契数列
斐波那契数列(Fibonacci sequence),又称黄金分割数列,因数学家莱昂纳多·斐波那契(Leonardo Fibonacci)以兔子繁殖为例子而引入,故又称为“兔子数列”,指的是这样一个数列:1、1、2、3、5、8、13、21、34、……
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
#include <stdio.h> int main( int argc, const char *argv[]) { int arr[15]; arr[0]=1; arr[1]=1; for ( int i=2;i<15;i++) { arr[i]=arr[i-2]+arr[i-1]; } for ( int i=0;i<15;i++){ printf ( "%d、" ,arr[i]); } printf ( "\n" ); return 0; } |
4、杨辉三角
杨辉三角的每行行首与每行结尾的数都为1.而且,每个数等于其左上及其正上二数的和。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
#include <stdio.h> int main( int argc, const char *argv[]) { int a[15][15]={{0}}; int i,j; for (i=0;i<15;i++) { a[i][0]=1; for (j=1;j<=i;j++) a[i][j]=a[i-1][j-1]+a[i-1][j]; } for (i=0;i<15;i++){ for (j=0;j<=i;j++) printf ( "%8d" ,a[i][j]); printf ( "\n" ); } return 0; } |
5、猴子吃桃子
一只小猴子一天摘了许多桃子,第一天吃了一半,然后忍不住多吃了一个。第二天又吃了一半,再加上一个;后面每天都是这样吃.到第10天的时候,小猴子发现只有一个桃子了。
问小猴子第一天共摘了多少个桃子?
用递归函数求得小猴子第一天共摘了多少个桃子。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
#include <stdio.h> int tao( int n); int main( int argc, const char *argv[]) { printf ( "%d\n" ,tao(10)); return 0; } int tao( int n){ if (n==1){ return 1; } return (tao(n-1)+1)*2; } |
6、编写一个时钟
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
|
#include <unistd.h> int main( int argc, const char *argv[]) { int year,month,day, hour,min,sec; scanf ( "%d %d %d %d %d %d" ,&year,&month,&day,&hour,&min,&sec); while (1){ sleep(1); if (sec<59){ sec++;} else if (min<59){ min++; sec=00;} else if (hour<23){ hour++; min=0; sec=0; } else if ((month==1||month==3||month==5||month==7||month==8||month==10||month==12)&&day<31){ day++; hour=00; min=00; sec=00; } else if (month==02&&day<28){ day++; hour=00; min=00; sec=00; } else if ((month==4||month==6||month==9||month==11)&&day<30){ day++; hour=00; min=00; sec=00; } else if (month<12){ month++; day=01; hour=00; min=00; sec=00; } else { year++; month=01; day=01; hour=00; min=00; sec=00; } printf ( "%02d:%02d:%02d:%02d:%02d:%02d\r" ,year,month,day,hour,min,sec); fflush (stdout); } return 0; } |
7、字符串中计算某字符出现的次数
写一个函数有两个参数,第一个参数是个字符,第二个参数是个char *,
函数功能为返回这个字符串中该字符的个数。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
#include <stdio.h> int func( char a, char *b); int main( int argc, const char *argv[]) { char c; char s[30]={0}; printf ( "Please input char:\n" ); scanf ( "%c" ,&c); getchar (); printf ( "Please input string:\n" ); gets (s); func(c,s); return 0; } int func( char a, char *b){ int i=0; while (*b){ if (a==*b){ i++; } *b++; } printf ( "%d\n" ,i); } |
8、逆序输出
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
|
一、逆序输出字符串 #include <stdio.h> int main( int argc, const char *argv[]) { int arr[10]; for ( int i=0;i<10;i++){ scanf ( "%d" ,&arr[i]); } printf ( "逆序输出为" ); int n, i; n= sizeof (arr)/ sizeof ( int ); for (i=n-1;i>=0;i--) printf ( "%d\t" ,arr[i]); putchar ( '\n' ); return 0; } 二、逆序输出数组中的数据 #include <stdio.h> int main( int argc, const char *argv[]) { int a[]={1,2,3,4,5,6}; int *p,*q,n,i,temp; n= sizeof (a)/ sizeof ( int ); p=a; q=&a[n-1]; while (p<q){ temp=*p; *p=*q; *q=temp; p++; q--; } for (i=0;i<n;i++){ printf ( "%d\n" ,a[i]); } return 0; } |
9、字符串中删除重复字符
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
#include <stdio.h> #include<string.h> int main( int argc, const char *argv[]) { char s[100]; printf ( "input:" ); gets (s); int n= strlen (s); for ( int i=0;i<n;i++){ int k=i+1; for ( int j=i+1;j<n;j++){ if (s[j]!=s[i]) s[k++]=s[j]; } s[k]= '\0' ; } puts (s); return 0; } |
10、用函数封装实现字符串拼接
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
#include <stdio.h> char * strcat ( char *a, char *b); int main( int argc, const char *argv[]) { char a[50]= "hello" ; char b[]= "word" ; puts ( strcat (a,b)); return 0; } char * strcat ( char *a, char *b){ char *c=a; while (*a){ a++; } while (*b){ *a=*b; a++; b++; } *a= '\0' ; return c; } |
11、删除字符串中的空格
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
|
#include <stdio.h> #include <string.h> void del_space( char *s1); int main( int argc, const char *argv[]) { char s[]= "a d gg sd " ; puts (s); del_space(s); puts (s); return 0; } void del_space( char *s1) { char *s2; s2=s1; while (*s1){ if (*s1== ' ' ) { s1++; } else { *s2=*s1; s1++; s2++; } } *s2= '\0' ; } |
12、求字符串中数字字符个数及把数字字符转换成数字求和。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
#include <stdio.h> int main( int argc, const char *argv[]){ char s[100]; int i=0; int j=0; int sum=0; gets (s); //puts(s); while (s[i]!= '\0' ){ if ( '0' <=s[i]&&s[i]<= '9' ){ j++; sum+=(s[i]- '0' ); } i++; } printf ( "字符串中数字字符的个数为:%d\n" ,j); printf ( "字符串中数字字符求和为:%d\n" ,sum); return 0; } |
13、二维数组中求出最大值及最大值所在的行数和列数。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
#include <stdio.h> int main( int argc, const char *argv[]) { int a[3][3]={{1,2,3},{4,5,6},{7,8,9}}; int i,j,row,cloumn; row=cloumn=0; for (i=0;i<3;i++){ for (j=0;j<3; j++){ if (a[row][cloumn]<a[i][j]){ row=i; cloumn=j; } } } printf ( "max=%d 最大值所在行为%d行 %d列\n" ,a[row][cloumn],row,cloumn); return 0; } |
14、冒泡排序
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
|
#include <stdio.h> int main( int argc, const char *argv[]) { int i,j,temp; int arr[5]={0}; printf ( "input:" ); for (i=0;i<5;i++){ scanf ( "%d" ,&arr[i]); } for (i=0;i<4;i++){ for (j=0;j<4-i;j++){ if (arr[j]>arr[j+1]){ temp=arr[j]; arr[j]=arr[j+1]; arr[j+1]=temp; } } } printf ( "排序后为:" ); for (i=0;i<5;i++){ printf ( "%d " ,arr[i]); } putchar (10); return 0; } |
15、实现两个数的交换(函数封装)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
#include <stdio.h> void swap( int *x, int *y); int main( int argc, const char *argv[]) { int a; int b; scanf ( "%d" ,&a); scanf ( "%d" ,&b); swap(&a,&b); printf ( "%d\n" ,a); printf ( "%d\n" ,b); return 0; } void swap( int *x, int *y){ int temp; temp=*x; *x=*y; *y=temp; } |
16、简易超市结账系统
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
|
#include <stdio.h> int main( int argc, const char *argv[]) { int n; double m; printf ( "***********************************************\n" ); printf ( "欢迎你来给我送钱,请按以下步骤进行送钱程序\n" ); printf ( "1、是尊贵会员请输入1以及消费金额\n" ); printf ( "2、不是尊贵会员请输入2以及消费金额\n" ); printf ( "***********************************************\n" ); scanf ( "%d %lf" ,&n,&m); if (n=1){ if (m<100){ printf ( "money is %lf\n" ,m*0.99); } else if (m<200){ printf ( "money is %lf\n" ,m*0.95); } else if (m<300){ printf ( "money is %lf\n" ,m*0.92); } else if (m<500){ printf ( "money is %lf\n" ,m*0.88); } else { printf ( "money is %lf\n" ,m*0.8); } } else { if (m<100){ printf ( "money is %lf\n" ,m); } else if (m<200){ printf ( "money is %lf\n" ,m*0.98); } else if (m<300){ printf ( "money is %lf\n" ,m*0.95); } else if (m<500){ printf ( "money is %lf\n" ,m*0.9); } else { printf ( "money is %lf\n" ,m*0.88); } } return 0; } |
17、完数
一个数如果恰好等于它的因子之和,这个数就称为“完数”。例如6=1+2+3.编程
找出1000以内的所有完数。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
#include <stdio.h> int main( int argc, const char *argv[]) { int i,j,sum; for (i=3;i<=1000;i++){ sum=1; for (j=2;j<i;j++){ if ((i%j)==0) sum+=j; } if (sum==i) printf ( "%d\n" ,i); } return 0; } |
18、简单选择排序
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
|
#include <stdio.h> #define N 5 int main( int argc, const char *argv[]) { int i,j,k,temp; int num[N]; printf ( "input num:" ,N); for (i=0;i<N;i++){ scanf ( "%d" ,&num[i]); } for (i=0;i<N-1;i++){ k=i; for (j=i+1;j<N;j++){ if (num[j]<num[k]){ k=j; } } if (k!=i){ temp=num[i]; num[i]=num[k]; num[k]=temp; } } printf ( "output:\n" ); for (i=0;i<N;i++){ printf ( "%d" ,num[i]); } putchar (10); return 0; } |
19、用函数封装思想实现strncpy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
#include <stdio.h> int main( int argc, const char *argv[]) { printf ( "please input two string and n:\n" ); char a[100]; gets (a); char b[100]; gets (b); int n; scanf ( "%d" ,&n); char *p=a; char *q=b; int i=0; while (*q&&i<n){ *p=*q; i++; p++; q++; } printf ( "strncpy:\n" ); puts (a); return 0; } |
20、实现strcmp的功能
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
|
#include <stdio.h> int main( int argc, const char *argv[]) { printf ( "please input two string :\n" ); char a[100]; gets (a); char b[100]; gets (b); char *p=a; char *q=b; while (*p&&*q){ if (*p<*q){ printf ( "-1\n" ); return 0; } else if (*p>*q){ printf ( "1\n" ); return 0; } else { p++; q++; if ((*p== '\0' )&&(*q== '\0' )){ printf ( "0\n" ); return 0; } } } if (*p){ printf ( "1" ); } else { printf ( "-1" ); } return 0; } |
21、用函数封装实现strcat的功能
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
64
65
66
67
68
69
|
方式一(最后添加 '\0' ): #include <stdio.h> #include<string.h> void mystrcat( char *x, char *y); int main( int argc, const char *argv[]) { char a[100]; char b[100]; gets (a); gets (b); mystrcat(a,b); puts (a); return 0; } void mystrcat( char *x, char *y){ x=x+ strlen (x); while (*y){ *x=*y; x++; y++; } *x= '\0' ; } 方式二(初始化数组 '\0' ): #include <stdio.h> #include<string.h> void mystrcat( char *x, char *y); int main( int argc, const char *argv[]) { char a[100]={0}; char b[100]={0}; gets (a); gets (b); mystrcat(a,b); puts (a); return 0; } void mystrcat( char *x, char *y){ x=x+ strlen (x); while (*y){ *x=*y; x++; y++; } //*x='\0'; } 方式二(定义全局变量 '\0' ): #include <stdio.h> #include<string.h> void mystrcat( char *x, char *y); char a[100]; char b[100]; int main( int argc, const char *argv[]) { gets (a); gets (b); mystrcat(a,b); puts (a); return 0; } void mystrcat( char *x, char *y){ x=x+ strlen (x); while (*y){ *x=*y; x++; y++; } //*x='\0'; } |
22、用sqrt 实现求三角形面积
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
#include <stdio.h> //编译时加-lm #include <math.h> int main( int argc, const char *argv[]) { double area; double a,b,c,s,n; printf ( "请输入三角形的三边长\n" ); scanf ( "%lf" ,&a); scanf ( "%lf" ,&b); scanf ( "%lf" ,&c); if ((a+b)>c&&(a+c)>b&&(b+c)>a){ s=1.0/2*(a+b+c); area= sqrt (s*(s-a)*b*c); printf ( "%lf" ,area); putchar ( '\n' ); } else { printf ( "erro" ); putchar ( '\n' ); } return 0; } |
23、输入整数转换为字符串
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 <stdio.h> char *zhuan( char *p, int n); int main( int argc, const char *argv[]) { char s[50],*r; int n; printf ( "请输入要转换为字符串的整数\n" ); scanf ( "%d" ,&n); r=zhuan(s,n); puts (r); puts (s); return 0; } char *zhuan( char *p, int n){ int r,i=0,j; while (n){ r=n%10; n=n/10; p[i]=r+ '0' ; i++; } p[i]= '\0' ; j=i-1; i=0; while (i<j){ r=p[i]; p[i]=p[j]; p[j]=r; i++; j--; } return p; } |
24、简易实现手机商城功能
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
#include <stdio.h> typedef struct { int ID; char Brand[10]; char Model[20]; char CPU[20]; float Price; }PH; void ui(); void input(PH ph[], int a); void output(PH ph[], int a); void selec(PH *p); int num; int main( int argc, const char *argv[]) { while (1){ sleep(1); ui(); int s; PH ph[10]; PH *p=ph; printf ( "please selec:\n" ); scanf ( "%d" ,&s); switch (s) { case 1: input(ph,s); break ; case 2: output(ph,s); break ; case 3: return 0; case 4:selec(p); break ; default : puts ( "xia hu shu" ); } while ( getchar ()!= '\n' ); } return 0; } void input(PH ph[], int a){ int n=0; printf ( "please input iphone num:\n" ); scanf ( "%d" ,&n); printf ( "ID\tBrand\tModel\tCPU\tPrice\n" ); for ( int i=num;i<num+n;i++){ scanf ( "%d %s %s %s %f" ,&ph[i].ID,ph[i].Brand,ph[i].Model,ph[i].CPU,&ph[i].Price); } puts ( "input success" ); num=num+n; } void output(PH ph[], int a){ printf ( "ID\tBrand\tModel\tCPU\tPrice\n" ); for ( int i=0;i<num;i++){ printf ( "%d\t%s\t %s\t %s\t %f\n" ,ph[i].ID,ph[i].Brand,ph[i].Model,ph[i].CPU,ph[i].Price); } } void ui(){ puts ( "*************************************" ); puts ( "****iphone management systerm********" ); puts ( "*************1、input****************" ); puts ( "*************2、output***************" ); puts ( "*************3、exit*****************" ); puts ( "*************4、selec*****************" ); puts ( "*************************************" ); } void selec(PH *p){ float min,max; printf ( "please input max:\n" ); scanf ( "%f" ,&max); printf ( "please input min:\n" ); scanf ( "%f" ,&min); puts ( "the iphone in this rage have:i\n" ); for ( int i=0;i<num;i++){ if ((p+i)->Price>=min&&(p+i)->Price<=max){ printf ( "%d\n" ,(p+i)->ID); } else { printf ( "sorry,no phone" ); } } } |
总结
到此这篇关于C语言必背的一些经典程序代码的文章就介绍到这了,更多相关C语言经典程序代码内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/qq_52049228/article/details/130159806