Mọi người ơi giúp mình bài này với
Cho tệp songuyen.txt gồm 5 hàng 4 cột. Tìm số lớn nhất trong tệp songuyen.txt và ghi vào tệp gtln.txt
const fi='songuyen.txt'
fo='kluan.txt'
var f1,f2:text;
n:integer;
begin
assign(f1,fi); reset(f1);
assign(f2,fo); rewrite(f2);
readln(f1,n);
if n mod 2=0 then writeln(f2,'La so chan')
else writeln(f2,'La so le');
close(f1);
close(f2);
end.
cho tệp songuyen.txt gồm n phần tử ,các số được nhập cách nhau một dấu cách.Tìm những số chẵn trong dãy số trên rồi ghi vào tệp ketqua.txt
with open('songuyen.txt', 'r') as f:
with open('ketqua.txt', 'w') as g:
for line in f:
numbers = line.split()
for number in numbers:
if int(number) % 2 == 0:
g.write(number + '\n')
Cho tệp SONGUYEN.TXT chứa cac số nguyên dương N. Viết ct in ra các số chẵn nhỏ hơn hoặc bằng N; Tính tổng các số chẵn đó. Kết quả ghi vào tệp SOCHAN.TXT a) XĐ input, output bài toán b) Viết ct giải quyết bài toán trên Mô tả - Tệp SONGUYEN.TXT chứa các số nguyên dương N - Tệp SOCHAN.TXT dòng ban đầu chứa các số chẵn nhỏ hơn hoặc bằng N - Dòng 2 chứa tổng số chẵn AI GIÚP EM VỚI Ạ Cứuuuuu e
a)
Input: Số nguyên dương N, dãy số a1,a2,a3,...,an
Output: Tổng các số chẵn nhỏ hơn hoặc bằng N
b)
Program HOC24;
var i,n: integer;
t: longint;
a: array[1..1000] of integer;
f1,f2: text;
const fi='SONGUYEN.TXT'
fo='SOCHAN.TXT'
begin
assign(f1,fi);
assign(f2,fo);
reset(f1);
rewrite(f2);
readln(f1,n);
for i:=1 to n do read(f1,a[i]);
for i:=1 to n do if (a[i] mod 2=0) and (a[i]<=n) then
begin
write(f2,a[i],' ');
t:=t+a[i];
end;
writeln(f2);
write(f2,t);
close(f1); close(f2);
end.
viết chương trình tạo một tệp văn bản có tên songuyen.txt trong ổ đĩa C và ghi vào tệp các số nguyên từ 1 đến 20. Sau đó đọc các số nguyên từ tệp và in ra màn hình
#include <bits/stdc++.h>
using namespace std;
long long i,a[100],n;
int main()
{
freopen("songuyen.txt","r",stdin);
n=20;
for (i=1; i<=n; i++) cin>>a[i];
for (i=1; i<=n; i++) cout<<a[i]<<" ";
return 0;
}
Cho một dãy số nguyên, mỗi số trên một dòng được ghi trong tệp 'SONGUYEN.TXT'. Viết chương trình pascal tạo tệp 'SOCHAN.TXT' chứa các số chẵn có trong tệp 'SONGUYEN.TXT', mỗi số cách nhau một dấu cách.
const fi='songuyen.txt';
fo='sochan.txt';
var f1,f2:text;
a:array[1..100]of integer;
i,dem:integer;
begin
assign(f1,fi); reset(f1);
assign(f2,fo); rewrite(f2);
dem:=0;
while not eof(f1) do
begin
inc(dem);
readln(f1,a[dem]);
end;
for i:=1 to dem do
if a[i] mod 2=0 then write(f2,a[i]:4);
close(f1);
close(f2);
end.
cho tệp SONGUYEN.TXT gồm 2 dòng, dòng 1 chứa số nguyên dương n, dòng 2 chứa n số nguyên. Các số nguyên ngăn cách nhau bởi dấu cách. Viết chương trình đọc dữ liệu từ tệp SONGUYEN.TXT. Tính và đưa ra kết quả số lượng các số nguyên tố và tệp KETQUA.TXT
Program HOC24;
const fi='SONGUYEN.TXT';
fo='KETQUA.TXT';
var n,i,d:integer;
a: array[1..32000] of integer;
function nt(x: integer): boolean;
var j: integer;
begin
nt:=true;
if (x=2) or (x=3) then exit;
nt:=false;
if (x=1) or (x mod 2=0) or (x mod 3=0) then exit;
j:=5;
while j<=trunc(sqrt(x)) do
begin
if (x mod j=0) or (x mod (j+2)=0) then exit;
j:=j+6;
end;
nt:=true;
end;
procedure ip;
begin
assign(f,fi);
reset(f);
readln(f,n);
for i:=1 to n do read(f,a[i]);
close(f);
end;
procedure out;
begin
assign(f,fo);
rewrite(f);
d:=0;
for i:=1 to n do
if nt(a[i]) then inc(d);
write(f,d);
close(f);
end;
begin
ip;
out;
end.
Cho tệp data.txt gồm 2 dòng , dòng đầu ghi số nguyên dương m, dòng 2 ghi m số nguyên. Hãy đọc dữ liệu từ tệp
a, ghi vào tệp mới songuyen.txt các số dương từ tệp data.txt
b, đếm số lượng các số là bội của một số nguyên k cho trước biết k nhập vào từ bàn phím
uses crt;
const fi='data.txt';
fo='songuyen.txt';
var f1,f2:text;
a:array[1..100]of integer;
m,i,dem,k:integer;
begin
clrscr;
assign(f1,fi); reset(f1);
assign(f2,fo); rewrite(f2);
readln(f1,m);
for i:=1 to m do
read(f1,a[i]);
for i:=1 to m do
if a[i]>0 then write(f2,a[i]:4);
writeln(f2);
write('Nhap so k='); readln(k);
dem:=0;
for i:=1 to m do
if a[i] mod k=0 then inc(dem);
writeln(f2,'So cac phan tu la boi cua ',k,' la: ',dem);
close(f1);
close(f2);
readln;
end.
cho tệp songuyen.txt gồm các số nguyên cách nhau 1 dấu cách tính tổng các số dương đua kq ra màn hình
uses crt;
const fi='songuyen.txt'
var f1:text;
a:array[1..100]of integer;
i,n,t:integer;
begin
clrscr;
assign(f1,fi); reset(f1);
n:=0;
while not eof(f1) do
begin
inc(n);
read(f1,a[n]);
end;
t:=0;
for i:=1 to n do
if a[i]>0 then t:=t+a[i];
writeln(t);
close(f1);
readln;
end.
giả sử có 1 tệp chứa các số nguyên tên là songuyen.txt viết chương trình đưa các số nguyên dương sang tệp soduong.out
const fi='songuyen.txt'
fo='soduong.out'
var f1,f2:text;
a:array[1..100]of integer;
i,n:integer;
begin
assign(f1,fi); reset(f1);
assign(f2,fo); rewrite(f2);
n:=0;
while not eof(f1) do
begin
inc(n);
read(f1,a[n]);
end;
for i:=1 to n do
if a[i]>0 then write(f2,a[i]:4);
close(f1);
close(f2);
end.