几道编程题

来源:互联网 发布:浙江省乡村旅游数据 编辑:程序博客网 时间:2024/05/29 18:57

1、读文件file1.txt的内容(例如):

12 34 56

输出到file2.txt:

56 34 12

#include <stdio.h>#include <stdlib.h>int main(){FILE *fp_src = NULL;FILE *fp_des = NULL;int i = 0, j = 0;intmax = 10;int *a = NULL, *b = NULL;a = (int *)malloc(max * sizeof(int));if (NULL == a){return -1;}fp_src = fopen("./a.txt", "r");if (NULL == fp_src){return -2;}fp_des = fopen("./b.txt", "w");if (NULL == fp_des){fclose(fp_src);return -3;}while (EOF != fscanf(fp_src, "%d", &a[i])){i++;j++;if (i >= max){max += 10;b = (int*)realloc(a, max * sizeof(int));if (NULL == b){return -4;}}}for (; --j >= 0;){fprintf(fp_des, "%d ", a[j]);}fclose(fp_src);fclose(fp_des);free(a);a = NULL;return 0;}

2、输出和为一个给定整数的所有组合

例如n=5

5=1+4;5=2+3(相加的数不能重复)

则输出

1,4;2,3。

#include <stdio.h>#include <stdlib.h>int main(){int result;int a, b;int i, j;printf("please input a number:");scanf("%d", &result);printf("the num:%d\n", result);if (1 & result){j = result/2 + 1;}else{j = result/2;}for (i = 0; i < j; i++){printf("%d, %d, ", i, result -i);}printf("\n");return 0;}


原创粉丝点击