C语言经典例题
一、古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?
#include <stdio.h> int main() { int month[23]; int i; month[0]=month[1]=1; for(i=0;i<24;i++) { if(i==0 || i==1) printf("%2d: 2只\n", i+1); else month[i]=month[i-1]+month[i-2]; printf("%2d: %d只\n", i+1, 2*month[i]); } getch(); }
#include <stdio.h> int Rabbit(int n) { if (n==1 || n==2) return 2; else if (n==0) return 0; else return Rabbit(n-1)+Rabbit(n-2); } int main(int i) { printf("输入查寻月份: "); scanf("%d",&i); printf("%d月: %ld", i, Rabbit(i)); getch(); }
二、打印九九乘法表
#include <stdio.h> int main() { int i,j,temp; for (i=1;i<10;i++) { for(j=1;j<=i;j++) { temp=i*j; printf("%d*%d=%d\t", j, i, temp); } printf("\n"); } getch(); }
三、判断是某天是该年第几天
int main() { int n, year, month, day, days, sum; int month_day[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; printf ("请输入\"year month day\": "); scanf ("%d %d %d", &year, &month, &day); if ((year%400==0) || (year%4==0&&year%100!=0)) month_day[2]=29; for (n=0;n<month;n++) { days+=month_day[n]; sum=days+day; } printf ("该天是:%d", sum); getch(); }
四、求最大公倍数
#include <stdio.h> int gcd(int, int); int main() { int a,b,temp; scanf("%d %d", &a, &b); if (a<b) { temp=a; a=b; b=a; } printf("GAD is : %d", gcd(a, b)); getch(); } int gcd(int a, int b) { if(!b) return a; else return gcd(b, a%b); }
五、判断回文
#include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char str[1001]; int len, flag, i; while(scanf("%s",str) != EOF) { flag = 1; len = strlen(str); for(i = 0; i < len/2; i ++) { if(str[i] != str[len - 1 -i]) { flag = 0; break; } } if(flag) printf("Yes!\n"); else printf("No!\n"); } return 0; }
六、求s=a+aa+aaa+aaaa+aa...a的值(长整型数据表示范围为4294967296,因此当次数输入超过10次或者:次数为10次且a>3时会发生溢出。)
#include<stdio.h> int main() { int a,n,i,M; long sum; printf("请输入一个个位数和运算的次数:\n"); scanf("%d%d",&a,&n); if((n>10)||((n==10)&&(a>3))) { printf("error!\n"); getch(); return 0; } if(n==1) sum=a; else { M=a*10+a; sum=a+M; for(i=1;i<(n-1);i++) { M=M*10+a; sum=sum+M; } } printf("结果为:%ld\n",sum); getch(); return 0; }
七、利用递归函数调用方式,将所输入的5个字符,以相反顺序打印出来。
#include<stdio.h> #include<string.h> void printchar(char *pc,int n) { char *px; px=pc-1; printf("%c",*pc); if(n!=0) printchar(px,n-1); } int main() { char c[1000]; int length; printf("输入一串字符:\n"); scanf("%s",c); length=strlen(c); printchar(&c[length-1],length); getch(); return 0; }
八、求字符串的长度
#include<stdio.h> int str_len(char *p) { int i=0; while(*(p+i)!='\0') i++; return i; } int main() { char ps[1000]; //分配一段安全的内存空间 printf("input a string:\n"); gets(ps); printf("your string:%s, length is %d",ps,str_len(ps)); getch(); return 0; }
#include <stdio.h> #define IN 1 /* inside a word */ #define OUT 0 /* outside a word */ main() { int nw = 0, c, state; state = OUT; while ((c = getchar()) != '\n') { if (c == ' ' || c == '\t') state = OUT; else if (state == OUT) { state = IN; ++nw; } } printf("%d\n", nw); }
九.Write an alernative version of squeeze(s1, s2) that deletes each characher in s1 that matches any character in the string s2.
#include <stdio.h> squeeze(char s1[], char s2[]) { int i, j, k=0; int flag = 1; for (i=0; s1[i] != '\0'; i++) { for (j=0 ; s2[j] != '\0' && flag; j++) if (s1[i] == s2[j]) flag = 0; if (flag) s1[k++] = s1[i]; flag = 1; } s1[k] = '\0'; } int main(void) { char a[] = "ab"; char b[] = "ba"; squeeze(a, b); printf("%s", a); }