日期转换
摘要:static char daytab[2][13] = { {0,31,28,31,30,31,30,31,31,30,31,30,31}, {0,31,29,31,30,31,30,31,31,30,31,30,31}};/* 将某日某月的日期表示形式转换为某年中第几天的表示形式*/int day_of_year(int year, int month, int day){ int i, leap; leap = year%4 == 0 && year%100 != 0 || year%400 == 0; for (i = 1; i daytab[leap][...
阅读全文
posted @
2013-11-06 16:19
欧小弟
阅读(212)
推荐(0)
快速排序
摘要:void qsort(int v[], int left, int right){ int i, last; void swap(int v[], int i, int j); if (left >= right) return; swap(v, left, (left + right) / 2); /*将划分元素移到*/ last = left; /*v[0]处*/ for (i = left + 1; i <= right; i++) if (v[i] < v[left]) s...
阅读全文
posted @
2013-11-04 11:18
欧小弟
阅读(191)
推荐(0)
连续多个空格用一个空格代替
摘要:# include# define NONBLANK 'a'/*replace string of blanks with a single blank*/main (){ int c,lastc; lastc = NONBLANK; while((c = getchar())!=EOF) { if (c != ' ') putchar(c); if (c == ' ') if (lastc != ' ') putchar(c); lastc = c; }}
阅读全文
posted @
2013-10-17 20:07
欧小弟
阅读(192)
推荐(0)
文件复制
摘要:# include/* 将输入复制到输出*/main(){ int c; while((c = getchar())!=EOF) putchar(c);}
阅读全文
posted @
2013-10-17 19:51
欧小弟
阅读(116)
推荐(0)
字符计数
摘要:# include/* 统计输入的字符数*/main(){ double nc; for (nc = 0;getchar()!=EOF;++nc) ; printf("%.0f\n",nc);}
阅读全文
posted @
2013-10-17 19:50
欧小弟
阅读(168)
推荐(0)
strcpy
摘要:/*数组表示*/void strcpy(char *s, char *t){ int i; i=0; while ((s[i]=t[i])!='\0') { s++; t++; }}/*指针1*/void strcpy(char *s, char *t){ while((*s=*t)!='\0') { s++; t++; }}/*指针2*/vo...
阅读全文
posted @
2013-06-21 09:53
欧小弟
阅读(168)
推荐(0)
快速排序
摘要:对于一个给定的数组,从中选择一个元素(叫作分区元素),并把其余元素划分成两个子集合--一个是由所有小于分区元素的元素组成的子集合,另一个是由所有大于等于分区元素的元素组成的子集合。对这样两个自己和递归应用同一过程。当某个子集合中的元素数小于两个时,这个子集合不需要再排序,故递归停止。/*qsort: 以递增顺序对v[left],,,v[right]进行排序*/void qsort(int v[],int left,int right){ int i,last; void swap(int v[],int i,int j); ...
阅读全文
posted @
2013-06-14 11:00
欧小弟
阅读(151)
推荐(0)
查找包含字母字符串‘ould’的行
摘要:ah love! could you and i with fate conspireto grasp this sorry scheme of things entire,would not we shatter it to bits--and thenre-mould it nearer to the heart's desire!产生如下输出:ah love! could you and i with fate conspirewould not we shatter it to bits--and thenre-mould it nearer to the heart'
阅读全文
posted @
2013-06-05 10:42
欧小弟
阅读(256)
推荐(0)
shell排序法
摘要:基本思想是:先对隔得比较远的元素进行比较,而不是像简单交换顺序算法中那样比较相邻的元素。这样可以快速地减少大量的无序情况,以后就可以少做些工作。各个被比较的元素之间的距离在逐步减少,一直减少到1,此时排序变成了相邻元素的互换。/*以递增顺序对v[0]、v[1]、......、v[n-1]进行排序*/void shellsort(int v[],int n){ int gap,i,j,temp; for(gap=n/2;gap>0;gap/=2) for(i=gap;i<n;i++) for(j=i-gap;j>=0&&v[j]>v[j+gap];...
阅读全文
posted @
2013-06-04 17:10
欧小弟
阅读(156)
推荐(0)
循环打印
摘要:for(i=0;i<n;i++) printf("%6d%c",a[i],(i%10==9||i==n-1)?'\n':' ');循环打印一个数组n个元素,每行打印10个元素,每一列之间用一个空格隔开,每行用一个换行符结束。i%9==0 ---- i从0开始算起。
阅读全文
posted @
2013-06-04 15:28
欧小弟
阅读(153)
推荐(0)
统计各个数字、空白符(空格符,制表符,换行符)以及所有其他字符出现次数
摘要:#include <stdio.h>main(){ int c,i,nwhite,nother; int ndigit[10]; nwhite=nother=0; for(i=0;i<10;i++) ndigit[i]=0; while ((c=getchar())!=EOF) if(c>='0'&&c<='9') ++ndigit[c-'0']; else if(c==' '||c=='\n'||c=='\t') ++nwhite; else ...
阅读全文
posted @
2013-06-03 16:19
欧小弟
阅读(448)
推荐(0)
统计行数、单词数、字符数
摘要:#include <stdio.h>#define IN 1 /*在单词外*/#define OUT 0 /*在单词内*/main (){ int c,nl,nw,nc,state; state=OUT; nl=nw=nc=0; while((c=getchar()!=EOF) { ++nc; if (c=='\n') ++nl; if (c==' '||c=='\n'||c=='\t') state=OUT; else if (state==OUT) { ...
阅读全文
posted @
2013-06-03 15:47
欧小弟
阅读(115)
推荐(0)
二进制转换为十进制
摘要:算法思想:把用户的二进制数(1011)当作十进制进行处理,分别拆开成1 0 1 1四个数,然后用2*(2*((1*2)+0)+1)+1 = 11 得出十进制的值为11模块设计:除10取余函数:1011通过%10(除10取余),可得到1 1 0 1(逆序)计算十进制:循环处程序代码:#include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { while(1) { printf("enter num:"); int a; scanf("%d"...
阅读全文
posted @
2013-06-02 17:00
欧小弟
阅读(247)
推荐(0)