博文
【原创】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; } }}......
十进制数转任意三十六以内进制数递归算法(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......
求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......
【原创】谁是小偷?一道推理题的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......
【原创】归并排序算法的原理及实现(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++]; &......
【原创】关于fortran读取各行长度不一格式不一具体行数不知数据文件(2009-12-06 09:21:00)
摘要:iwanfly网友曾在一段时间前发过贴探讨过这个问题http://bbs.pfan.cn/post-312402.html当时思路不是很清晰现思考结果如下,虽然比较麻烦,但最终还是解决了读取数据的问题,前提是知道每行最大的数据个数,比如7个。不知道论坛上的网友是否有更好的方法,不吝赐教。测试数据如下:12 89 7712 999 878 777398 789 765 98 76578 8912 89 7712 999 878 777398 789 765 98 765。。。。总共78144行。大概需要5,6秒时间把数据全部读进。program mainimplicit noneinteger,allocatable :: int_array(:,:),temp_array(:,:)integer :: count,length,step,i,ierrcharacter*256 :: str_one_recordopen(100,file='data.txt')count=0step=100000length=stepallocate(int_array(length,7))int_array=0do while(.not. eof(100)) count=count+1 loop1: do i=7,1,-1 read(100,'(A)',advance='no',eor=168) str_one_record168 read(str_one_record,*,......
冒泡排序 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]<......
一道字符串操作习题 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  ......
【原创】计算100和10000的阶乘(2009-12-05 08:24:00)
摘要:方法一:
program mainimplicit nonereal*8 :: Rfact100integer,allocatable :: main_digits(:),sub_digits(:,:)integer :: num_digitsinteger :: i,j!先用双精度数算出100!,有效位数只有15位Rfact100=1.0d0do i=1,100 Rfact100=Rfact100*dble(i)end do!求得100阶乘有多少位数字,包括低位上的0num_digits=dlog10(Rfact100)+1!分配一个数组,数组每个元素代表一个数字allocate(main_digits(num_digits),sub_digits(num_digits,3))!赋初值main_digits=0main_digits(1)=1do i=2,100 !每一位数字都乘以i,每个数都不会超过900,所以最多3位数字 main_digits=main_digits*i !分解每位数,进位处理 do j=1,num_digits call int2digits(main_digits(j),sub_digits(j,:)) if(j<=num_digits-2) then main_digits(j)=sub_digits(j,1) main_digits(j+1)=main_digits(j+1)+sub_digits(j,2) main_digits(j+2)=main_digits(j+2)+sub_digits(j,3) else&nb......
