《c语言 一些小代码.doc》由会员分享,可在线阅读,更多相关《c语言 一些小代码.doc(4页珍藏版)》请在金锄头文库上搜索。
1、c语言 一些小代码矩阵转置Int main() int A33; int B33; int i,j; for (i=0;i3;i+) for (j=0;j3;j+) scanf(%d,&Aij); /*found*/ for (i=0;i3;i+) for(j=0;j3;j+) Bji=Aij; /*found*/ printf(The reversed matrix is:n); for (i=0;i3;i+) for (j=0;j3;j+) printf(%4d,Bij); printf(n); return 0; 枚举四季#includeenum seasonspring,summer,
2、autumn,winter;typedef struct dateint year,month,day;date;int main()date d;enum season s;scanf(%d%d%d,&d.year,&d.month,&d.day);if(d.month=3&d.month=6&d.month=9&d.month=11)s=autumn;else if(d.month=12|d.month=1|d.month=2)s=winter;switch(s)case spring:printf(NOW it is Springn);break;case summer:printf(N
3、OW it is Summern);break;case autumn:printf(NOW it is Autumnn);break;case winter:printf(NOW it is Wintern);break;求最大公约数#include int gcd(int m,int n) int r; /*found*/ r=m%n; while(r) m=n; n=r; r=m%n; return n; /*found*/ int main() int a,b; scanf(%d%d,&a,&b); printf(%dn,gcd(a,b); return 0; 删除相同元素只留下一个#
4、include int fun(int a,int n) int i,j,k; for(i=0;in-1;i+) for(j=i+1;jn;j+) if(ai=aj) for(k=j;kn;k+) ak=ak+1; j-;n-; return n; int main() int a100,i,n,num; scanf(%d,&n); for(i=0;in;i+) scanf(%d,&ai); num=fun(a,n); for(i=0;inum;i+) printf(%5d,ai); printf(n); return 0; 字符串转值#include int change(char *m,c
5、har *des) int i=0; while(*m) if(*m=0&*m=9) desi+=*m; m+; desi=0; return i; double calcu(char*s) double result=0; while(*s) result=result*10+(s0-48); s+; return result; int main() char str50,trades50; gets(str); if(change(str,trades) printf(%.4fn,calcu(trades); else printf(the digital string is empty!n); return 0;