中大-复试-2011-编程题二
来源:互联网 发布:君麻吕实力 知乎 编辑:程序博客网 时间:2024/06/03 03:08
题目:从in.txt输入数据,排序,在输出到out.txt,每行数据开始第一个数是元素的个数
输入:14 3 35 4 72 19 8 -3 29 -2 18 -25 4 72 100
输出:-25 -3 -2 3 4 4 8 18 19 29 35 72 72 100
#include <stdio.h>#include <string.h>#include <stdlib.h>void creatNewFile(){ FILE *file; //char t[100]="3 35 4 72 19 8 -3 29 -2 18 -25 4 72 100"; char t[100]="14 3 35 4 72 19 8 -3 29 -2 18 -25 4 72 100"; if ((file = fopen("in.txt", "w+")) == NULL) { printf("can't open the file!"); exit(0); } fputs(t, file); fclose(file);}void quickSort(int arr[], int s, int e){ if (s<e) { int i=s, j=e, t=arr[s]; while (i<j) { while (i<j & arr[j]>=t) { j--; } if (i<j) { arr[i] = arr[j]; } while (i<j && arr[i]<=t) { i++; } if (i<j) { arr[j] = arr[i]; } } arr[i] = t; quickSort(arr, s, i-1); quickSort(arr, i+1, e); }}int main(int argc, const char * argv[]){ creatNewFile();//创建数据文件 FILE *in, *out; char ch; int i=0, c[100], j; if ((in = fopen("in.txt", "r")) == NULL) { printf("can't open the file in!"); exit(0); } fscanf(in, "%d", &j); while (j--) { fscanf(in, "%d", &c[i]); i++; } /* while ((fscanf(in, "%d", &c[i])) != EOF) { i++; }*/ fclose(in); quickSort(c, 0, i-1); if ((out = fopen("out.txt", "w+")) == NULL) { printf("can't open the file out!"); exit(0); } for (j=0; j<i; j++) { fprintf(out, "%d", c[j]); fputc(' ', out); } fclose(out); /*测试 if ((out = fopen("out.txt", "r")) == NULL) { printf("can't open the file out!"); exit(0); } while ((ch = fgetc(out)) != EOF) { printf("%c", ch); } fclose(out); */ return 0;}
0 0
- 中大-复试-2011-编程题二
- 中大-复试-2012-编程题二
- 中大-复试-2007-编程题二
- 中大-复试-2012(2011)-编程题一
- 中大-复试-2010-编程题一
- 中大-复试-2009-编程题一
- 中大-复试-2008-编程题一
- 中大-复试-2005-编程题一
- 北理工2011年计算机复试上机题(二)
- 2008年华中大软件学院复试流程
- 2011年北理复试上机题
- 前天面试的复试题中gird
- 浙大复试(二)
- 华中科大复试上机题-找位置
- 2011华中科技大学研究生复试机试之二
- 2011---2013年杭电计算机历年研究生复试---笔试编程
- 浙江大学复试编程题之——Sharing
- 考研复试准备中
- git commit 提交最新更改
- Java正则表达式Pattern和Matcher
- IE默认连接首页被恶意修改解决办法
- LeetCode之Sum Root to Leaf Numbers
- 数据结构经典算法(8)八皇后
- 中大-复试-2011-编程题二
- pthread编程基础
- Internet路由-主机路由表和转发表
- 网络百科——交换机
- 删除rac中ASM实例
- u014331702
- 第三周作业——冒泡排序(BubbleSort)与归并排序(MergeSort)
- Android项目怎么使用外部工程作为jar包
- Qt窗口与视口坐标转换