博文

【原创】insertion sort(2009-12-08 15:43:00)

摘要:#include <stdio.h>int main(){ int arr[]={9,7,5,8,4,1,3,2,6}; int i; void insertion_sort(int arr[],int size); insertion_sort(arr,sizeof(arr)/sizeof(int));  for(i=0;i<sizeof(arr)/sizeof(int);i++)  printf("%d\n",arr[i]);  getchar(); return 0;}void insertion_sort(int arr[],int size){ int i,j,k; int temp; for(i=1;i<size;i++) {  for(j=0;j<=i-1;j++)   if(arr[i]<arr[j])    break;  if(j>=0)  {   temp=arr[i];   for(k=i-1;k>=j;k--)    arr[k+1]=arr[k];   arr[j]=temp;  } }}......

阅读全文(1226) | 评论:0

十进制数转任意三十六以内进制数递归算法(2009-12-08 08:40:00)

摘要:#include <stdio.h>#include <conio.h>#include <assert.h>#include <string.h>void itob(int n,  int b, char *s){    int sign;    if((sign=n)<0)        n=-n;    if(n/b>0)        itob(n/b,  b, s+1);    else        *(s+1)='\0';    if("%d",n%b<10)        *s=n%b+'0';    else        *s=n%b-10+'A';}int main(){    int n, b;    char s[20];        printf("Recursive convert a decimal number to its counterpart in another number s......

阅读全文(2364) | 评论:0

求N个句子中的单词数并打印(2009-12-07 08:03:00)

摘要:昨天有个网友问相似的问题,不过只有一个句子,稍微改了下弄成能处理多个句子的。 #include <stdio.h>const int MAX_NUM_OF_WORDS=30;const int MAX_LEN_OF_WORDS=30;int main(){        char sample_sentence[]="You can, count,  how    many words these sample sentences has  . This is the second sentence  .  Third one.";        char words[MAX_NUM_OF_WORDS][MAX_LEN_OF_WORDS];    int CountWords(char* str,char words[][MAX_LEN_OF_WORDS]);    int num_of_sentences=0;    int num_of_words;     //    =CountWords(sample_sentence,words);    char *p_first_char=sample_sentence;    int i,j,iSentence=0;    &n......

阅读全文(303) | 评论:0

【原创】谁是小偷?一道推理题的C语言描述(2009-12-07 04:05:00)

摘要:题目: a说:“我不是小偷。”b说:“c是小偷。”c说:“小偷肯定是d。”d说:“c冤枉人!”四人中有三人说的是真话,问到底谁是小偷。 #include <stdio.h>#define N_PEOPLE 4    //定义人的个数#define NUM_OF_TRUTH 3    //说真话人的个数int main(){    int hypothesis[N_PEOPLE]; //定义数组来描述假设,1表示真话,0表示假话    int num_of_true[N_PEOPLE]={0,0,0,0};    int i,j;    int judge(int index,int hypothesis[]);        //假设其中一个人是小偷,比如A是小偷,看几个人的描述跟这个假设吻合    //如果有三个描述和假设吻合,则假设成立,这个人就是小偷    //下面的循环从假设A是小偷开始一直到D,计算和假设吻合的描述个数    for(i=0;i<N_PEOPLE;i++)    {        //被假设成小偷的人被赋值成1,假设不是小偷的赋值成0        for(j=0;j<N_PEOPLE;j++)            if(j==i)&nb......

阅读全文(2710) | 评论:0

【原创】归并排序算法的原理及实现(2009-12-06 17:45:00)

摘要:#include <stdio.h>int main(){    int arr[]={9,7,5,8,4,1,3,2,6};    int i;    void merge_sort(int vec1[],int vec2[],int size1,int size2);    void recursive_merge(int arr[],int size);    recursive_merge(arr,sizeof(arr)/sizeof(int));    for(i=0;i<sizeof(arr)/sizeof(int);i++)        printf("%d\n",*(arr+i));    getchar();    return 0;}void merge_sort(int vec1[],int vec2[],int size1,int size2){    int i=0,j=0,k=0;        int *pvec= (int*) malloc(size1+size2);    while(1)    {        *(pvec+k++)=vec1[i]<=vec2[j]?vec1[i++]:vec2[j++]; &......

阅读全文(1175) | 评论:0

冒泡排序 C版块问题提问(2009-12-06 09:13:00)

摘要:题目: 某班10人期中考试共有五门成绩用函数求按每个学生的平均分排序 #include <stdio.h>#define NUM_STUDENTS 5#define NUM_CLASSES 2int main(){ float grades[NUM_STUDENTS][NUM_CLASSES]={{80,50},{70,40},{90,68},{70,80},{77,31}}; float ave_grades[NUM_STUDENTS];  float cal_average(float grades[]); void bubble_sort(float ave_grades[]); for(int i=0;i<NUM_STUDENTS;i++)  ave_grades[i]=cal_average(grades[i]); bubble_sort(ave_grades);  for(int i=0;i<NUM_STUDENTS;i++)  printf("%f \n",ave_grades[i]); return 0;}float cal_average(float grades[]){ float sum=0.0; for(int i=0;i<NUM_CLASSES;i++)  sum+=grades[i]; return sum/NUM_CLASSES;}void bubble_sort(float ave_grades[]){ float temp; for(int i=NUM_STUDENTS-1;i>0;i--)  for(int j=0;j<i;j++)   if(ave_grades[j]<......

阅读全文(273) | 评论:0

一道字符串操作习题 C++版块提问(2009-12-06 07:19:00)

摘要:写过的代码还是记录下来好 题目:给定字符串,其内容为英文长句,其中包含英语单词,标点符号,空格等内容,每个英语单词使用标点符号,一个或多个空格分隔(注意!!是多个空格也可以!!).将英语长句分隔成英语单词系序列输出,并输出去单词数目.例如长句"You can count how many words this sample sentence has."的单词数目为10,其单词序列为"You" "can" "count"..... #include <iostream>using namespace std;const int MAX_NUM_OF_WORDS=50;const int MAX_LEN_OF_WORDS=30;int main(){        char sample_sentence[]="You can, count,  how    many words this sample sentence has.";        char words[MAX_NUM_OF_WORDS][MAX_LEN_OF_WORDS];    int CountWords(char* str,char words[][MAX_LEN_OF_WORDS]);    int num_of_words=CountWords(sample_sentence,words);    cout<<"\"You can, count, how   ......

阅读全文(203) | 评论:0