cho tệp DATA.TXT các số nguyên, các số cách nhau bởi một dấu cách.Hãy lập chương trình đếm các số lẻ. Ghi kết quả vào tệp TES.TXT.
const fi='input.txt'
fo='chiahet.txt'
var f1,f2:text;
a:array[1..100]of integer;
i,n,dem:integer;
begin
assign(f1,fi); reset(f1);
assign(f2,fo); rewrite(f2);
n:=0;
while not eoln(f1) do
begin
inc(n);
read(f1,a[n]);
end;
dem:=0;
for i:=1 to n do
if a[i] mod 3=0 then inc(dem);
writeln(f2,dem);
close(f1);
close(f2);
end.
Cho tệp BAIKT.INP chứa các số nguyên bất kì. Viết chương trình đếm xem trong tệp có bao nhiêu số lẻ rồi ghi kết quả vào tệp KETQUA.OUT
tệp data inp chứa một dãy số gồm 10 số cách nhau bởi dấu cách em hãy viết chương trình đọc toàn bộ các chữ số trong tệp tính và ghi kết quả vào tệp data.out tổng các chữ số chia hết cho 5
tệp sn.inp gồm các số nguyên dương ngăn cách nhau bởi ít nhất một dấu cách. các số nguyên là cạnh a,b của hình chữ nhật. viết chương trình đọc dữ liệu từ tệp sn.inp vào hai biến a,b. tính diện tích hình chữ nhật. ghi kết quả vào tệp cn.out
#include <bits/stdc++.h>
using namespace std;
long long a,b;
int main()
{
freopen("sn.inp","r",stdin);
freopen("cn.out","w",stdout);
cin>>a>>b;
cout<<a*b;
return 0;
}
Cho tệp DATA.TXT chứa liên tiếp các số nguyên cách nhau bằng dấu cách và không kết thúc bằng kí tự xuống dòng, hãy lập trình tìm và in các số chẵn trong tệp DATA.TXT ra màn hình
uses crt;
const fi='data.txt'
var f1:text;
a:array[1..100]of integer;
n,i:integer;
begin
clrscr;
assign(f1,fi); reset(f1);
n:=0;
while not eof(f1) do
begin
n:=n+1;
read(f1,a[n]);
end;
for i:=1 to n do
if a[i] mod 2=0 then write(a[i]:4);
close(f1);
readln;
end.
cho tệp songuyen.inp chứa dãy các số nguyên ( mỗi số cách nhau một dấu cách). Đọc dữ liệu từ tệp đã cho và
a) Tính tổng các số lẻ trong tệp và đưa kết quả lên màn hình.
b) tính tổng các số lẻ trong tệp và ghi kết quả vào tệp songuyen2
c) đưa ra màn hình các số lẻ trong tệp
uses crt;
const fi='songuyen.inp'
fo='songuyen2.out'
var a:array[1..100]of integer;
i,n,t1:integer;
f1,f2:text;
begin
clrscr;
assign(f1,fi); reset(f1);
assign(f2,fo); rewrite(f2);
readln(f1,n);
for i:=1 to n do readln(f1,a[i]);
t1:=0;
for i:=1 to n do if a[i] mod 2<>0 then t1:=t1+a[i];
writeln(t1);
writeln(f2,t1);
for i:=1 to n do
if a[i] mod 2<>0 then write(a[i]:4);
close(f1);
close(f2);
readln;
end.
cho tệp Data.txt chứa liên tiếp các số nguyên, mỗi số trên 1 dòng. viết chương trình đọc các số vào chương trình pascal và tính trùng bình cộng các số đó. ghi kết quả vào TBC.txt
const fi='data.txt'
fo='tbc.txt'
var f1,f2:text;
a:array[1..100]of integer;
n,i,t:integer;
begin
assign(f1,fi); reset(f1);
assign(f2,fo); rewrite(f2);
n:=0;
while not eof(f1) do
begin
n:=n+1;
readln(f1,a[n]);
end;
t:=0;
for i:=1 to n do
t:=t+a[i];
writeln(f2,t/n:4:2);
close(f1);
close(f2);
end.
Cho tệp dulieuvao.txt gồm nhiều dòng, mỗi dòng chứa 2 số nguyên a, b. Các số cách nhau bởi dấu cách. Viết chương trình đọc dữ liệu từ tệp, tính trung bình cộng và ghi kết quả ra tệp dulieura
Tệp "DULIEU.INP" chứa hai số nguyên dương ngăn cách nhau ít nhất một dấu cách, số trước nhỏ hơn số sau và nhỏ hơn 100.
Viết chương trình: Đọc từ tệp "DULIEU.INP" vào biến M và N (M<N). Tính tổng các số lẻ trong phạm vi từ M đến N. Ghi kết quả ra tệp "KETQUA.OUT".
#include <bits/stdc++.h>
using namespace std;
long long m,n,t,i;
int main()
{
freopen("dulieu.inp","r",stdin);
freopen("ketqua.out","w",stdout);
cin>>m>>n;
t=0;
for (i=m; i<=n; i++)
if (i%2!=0) t+=i;
cout<<t;
return 0;
}