打印九九乘法表

打印九九乘法表

/* 功能:九九乘法表 作者:汐枫 日期:2013-05-08 */ #include <stdio.h> #include <stdlib.h> #include <math.h> int main(void) { int i,j,k,sum; for (i = 1; i <= 9; i ++) { for (j=10-i,k=i,sum=0;j>=1;j—,k++) { sum = i * k ; printf(“%d*%d=%d “,i,k,sum); } printf(“n”); } printf(“n”); system(“pause”); return 0; }

数组循环左移

数组循环左移

/* 功能 日期:2013-05-20 */ #include <stdio.h> #include <stdlib.h> #include <math.h> #define LEN 6 int main(void) { int num[LEN]={1,2,3,4,5,6}; int tmp[LEN]={0}; int i,j,a; printf(“数组:”); for(j=0;j<=LEN-1;j++) { printf(“%d “,num[j]); } printf(“n”); printf(“请输入左移位数:”); scanf(“%d”,&i); if(i>LEN) { printf(“左移位数不可大于数组长度!n”); printf(“请重新输入:”); scanf(“%d”,&i); } printf(“n”); a=i; for(i=0,j=0;i<=a;i++,j++) { tmp[j] = num[i]; } for(j=0,i=a;i<=LEN-1;i++,j++) { num[j] = num[i]; } for(j=LEN-a,i=0;i<=a;i++,j++) { num[j] = tmp[i]; } printf(“左移后:”); for(j=0;j<=LEN-1;j++) { printf(“%d “,num[j]); } printf(“n”); system(“pause”); return 0; }

二分查找

二分查找

/* 功能:二分查找 日期:2013-05-16 */ #include <stdio.h> #include <stdlib.h> #include <math.h> #define LEN 10 int main(void) { int a[LEN] = {1,3,5,7,9,11,13,17,19}; int low,high,mid,num; low = 0; high = LEN - 1; num = 10; while(low<=high) { mid = (low + high) / 2; if(num>a[mid]) { low = mid + 1; } else if (num<a[mid]) { high = mid-1; } else { break; } } if(low<=high) { printf(“存在为%d的值”,num); } else { printf(“不存在为%d的值”,num); } printf(“n”); system(“pause”); return 0; }

坐标判断

坐标判断

/* 功能:坐标判断 日期:2013-05-08 */ #include <stdio.h> #include <stdlib.h> #include <math.h> int main(void) { double sum1,sum2; printf("请输入两个实数:"); scanf("%lf%lf",&sum1,&sum2); if (sum1>=0) { if (0 == sum1) { if (0 == sum2) { printf("原点!"); } else { if(sum2 > 0) { printf("Y轴正半轴!"); } else { printf("Y轴负半轴!"); } } } else { if(sum2 > 0) { printf("第一象限!"); } else if(sum2 < 0) { printf("第四象限!"); } else if(0 ==sum2) { printf("x轴正半轴!"); } } } else { if (0 ==sum2 ) { printf("x轴负半轴!"); } else if(sum2 > 0) { printf("第二象限!"); } else if(sum2 < 0) { printf("第三象限!"); } } printf("n"); system(“pause”); return 0; }

数据加密

数据加密

/* 功能:数据加密 日期:2013-05-26 */ #include<stdio.h> #include<stdlib.h> #include<math.h> #define LEN 4 int main(void) { int A[LEN]={0}; int i,tmp; printf(“请输入一组数据:”); for(i=0;i<LEN;i++) { scanf(“%d”,&A[i]); } for(i=0;i<LEN;i++) { A[i] = ( A[i] + 5 ) % 10; } tmp = A[0]; A[0] = A[3]; A[3] = tmp; tmp = A[1]; A[1] = A[2]; A[2] = tmp; printf(“加密后的数据为:”); for(i=0;i<LEN;i++) { printf(“%d “,A[i]); } printf(“n”); system(“pause”); return 0; }

数字插入

数字插入

/* 功能:数字插入 日期:2013-05-17 */ #include <stdio.h> #include <stdlib.h> #include <math.h> #define LEN 7 int main(void) { int num[LEN]={1,8,10,16,23,40,0}; int i,tmp,j; printf(“请输入一个数字:”); scanf(“%d”,&num[LEN-1]); printf(“插入后的数组为:”); for (i = 0;i<=LEN-1;i++) { for (j = 0;j<=LEN-i;j++) { if(num[j]>num[j+1]) { tmp = num[j]; num[j] = num[j+1]; num[j+1] = tmp; } } } for (i=0;i<=LEN-1;i++) { printf(“%d “,num[i]); } printf(“n”); system(“pause”); return 0; }

绘制自定义迷宫地图

绘制自定义迷宫地图

/* 功能:绘制自定义迷宫地图 日期:2013-05-26 */ #include<stdio.h> #include<stdlib.h> #include<math.h> #define LEN 8 int main(void) { int A[LEN][LEN]={0}; int i,j; char ch1; //自定义wall printf(“Please input the picture of the wall:”); scanf(“%c”,&ch1); //输入map数组 printf(“Please input your map:(1 0)n”); for(i=0;i<LEN;i++) { for(j=0;j<LEN;j++) { scanf (“%1d”,&A[i][j]); } } //生成map printf(“Now drawing the map…n”); for(i=0;i<LEN;i++) { for(j=0;j<LEN;j++) { if(0==A[i][j]) { printf(”_”); } else if((1==A[i][j])) { printf(“%c”,ch1); } } printf(“n”); } system(“pause”); return 0; }

分数四则运算

分数四则运算

/* 功能:分数四则运算 日期:2013-05-11 */ #include <stdio.h> #include <stdlib.h> #include <math.h> int main(void) { int first1,first2,second1,second2; int i,n,p; printf(“请输入两个分数(用逗号隔开):”); scanf(“%d/%d,%d/%d”,&first1,&first2,&second1,&second2); if (0 == first2|| 0 ==second2) { printf(“分母不可以为0!”); } else { i = abs(first1 * second1); n = first2 * second2; p = abs(first2 * second1); //和 while (n>0) { if(0==((first1 * second2 + second1 * first2) % n + (first2 * second2) % n)) { if ((first1 * second2 + second1 * first2)==(first2 * second2)) { printf(“和为:1n”); break; } else if (0 == (first1 * second2 + second1 * first2) ) { printf(“和为:0n”); break; } else { printf(“和为:%d/&dn”,(first1 * second2 + second1 * first2) % n,(first2 * second2) % n); break; } } n—; } //差 while (n>0) { if(0==((first1 * second2 - second1 * first2) % n + (first2 * second2) % n)) { if ((first1 * second2 - second1 * first2)==(first2 * second2)) { printf(“差为:1n”); break; } else if (0 == (first1 * second2 - second1 * first2) ) { printf(“差为:0n”); break; } else { printf(“差为:%d/&dn”,(first1 * second2 - second1 * first2) % n,(first2 * second2) % n); break; } } n—; } //积 while (i>0) { if(0==((first1 * second1) % i + (first2 * second2) % i)) { if ((first1 * second1)==(first2 * second2)) { printf(“积为:1n”); break; } else { printf(“积为:%d/&dn”,(first1 * second1 ) % i,(first2 * second2) % i); break; } } i—; } //商 while (p>0) { if(0==((first1 * second2) % p + (first2 * second1) % p)) { if ((first1 * second2)==(first2 * second1)) { printf(“积为:1n”); break; } else { printf(“积为:%d/&dn”,(first1 * second2 ) % p,(first2 * second1) % p); break; } } p—; } } system(“pause”); return 0; }