排序,檔案輸入輸出
阿新 • • 發佈:2020-12-12
技術標籤:C/C++
編寫兩個檔案合併程式,將兩個檔案中的數字重新排序,兩個檔案裡的數字是有序的,輸出到第三個檔案中。
#include<stdio.h>
const int N = 1e5+5;
int a[N],b[N],res[N*2];
int main(){
freopen("ina.txt", "r", stdin);
int lena = 0;
while(scanf("%d",&a[lena])!=EOF)
lena++;
freopen("inb.txt" , "r", stdin);
int lenb = 0;
while(scanf("%d",&b[lenb])!=EOF)
lenb++;
int posres = 0 ,posa = 0, posb = 0;
freopen("out.txt","w",stdout);
while( posa<lena && posb<lenb ){
if(a[posa]<b[posb]){
printf("%d ",a[posa]);
posa++ ;
}
else{
printf("%d ",b[posb]);
posb++;
}
}
while( posa<lena ) printf("%d ",a[posa++]);
while( posb<lenb ) printf("%d ",b[posb++]);
fclose(stdin);
fclose(stdout);
return 0;
}
檔案的輸入輸出可以看這個
https://blog.csdn.net/weixin_43202635/article/details/97923608
https://baike.baidu.com/item/freopen/10942366?fr=aladdin