Viết chương trình đọc từ tệp văn bản 'INPUT.TXT' 2 dòng
Dòng đầu tiên chứa số nguyên n là chỉ số phần tử
Dòng tiếp theo chứa n số nguyên, các số cách nhau 1 khoảng cách và không có kí hiệu xuống dòng
Tính và in ra màn hình tích các số lẻ đọc được trong n số đó
tìm các số nguyên tố trong dãy số nguyên có n phấn tử a1, a2...an. dữ liệu vào đọc từ file nguyên tố.INP, gồm 2 dòng:
- dòng đầu chứa số nguyên dương n,n<= 250.
-dòng 2 chứa các phần tử của dãy số gồm a1, a2...an.
-dữ liệu ra ghi vào tệp nguyên tố .OUTP ,các phần tử là số nguyên tố nằm trên 1 dòng
giúp mình với ạ, mình cảm ơn!!
#include <bits/stdc++.h>
using namespace std;
long long a[1000],i,n,j;
bool kt;
int main()
{
freopen("nguyento.inp","r",stdin);
freopen("nguyento.out","w",stdout);
cin>>n;
for (i=1; i<=n; i++) cin>>a[i];
for (i=1; i<=n; i++)
if (a[i]>1)
{
kt=true;
for (j=2; j*j<=a[i]; j++)
if (a[i]%j==0) kt=false;
if (kt==true) cout<<a[i]<<" ";
}
return 0;
}
Cho tệp DL.TXT có nội dung như sau: - Số nguyên n ( dòng 1 ) - Dãy gồm n số nguyên ( dòng 2 ) ( Các số cách nhau bởi dấu cách ) Viết chương trình ghi các số chẵn của dãy số trên vào tệp KQ.TXT
1. Tệp Dulieu.txt có một dòng chứa các số nguyên (cách nhau khoảng trắng). Viết chương trình kiểm tra và đưa vào tệp Ketqua.txt những số nguyên dương chẵn trên cùng một dòng và cách nhau một khoảng trắng. 2.Viết chương trình nhập vào một mảng số nguyên, tínhvà đưa ra màn hình tổng của các số không âm có trong mảng.Yêu cầu sử dụng chương trình con để viết các thủ tục nhập, xuất mảng và hàm để tính tổng.
1:
const fi='Dulieu.txt'
fo='ketqua.txt'
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) and (a[i] mod 2=0) then write(f2,a[i]:4);
close(f1);
close(f2);
end.
P/s: Cái này mình viết chương trình chính thôi nhé, còn hàm và thủ tục bạn tự viết nhé(chỉ cần lấy ý tưởng trong chương trình chính ra viết là được rồi)
2:
uses crt;
var a:array[1..100]of integer;
i,n,t:integer;
begin
clrscr;
write('Nhap n='); readln(n);
for i:=1 to n do
begin
write('A[',i,']='); readln(a[i]);
end;
t:=0;
for i:=1 to n do
if a[i]>=0 then t:=t+a[i];
writeln(t);
readln;
end.
cho dãy số gồm N số tự nhiên a1,a2,a3,...aN .Viết chương trình in ra trung bình cộng của các phần tử trên .Dữ liệu vào bởi tệp DULIEU.INP có cấu trúc : dòng đầu tiên chứa số nguyên N ,dòng thứ 2 chứa các số a1,a2,a3,...aN các số cách nhau ít nhất một kí tự trống
program du_lieu;
uses crt;
var i,n:integer;
a:array[1..100]of integer;
tbc:real;
f:text;
begin
clrscr;
assign(f,'DULIEU.INP');reset(f);
readln(f,n);
for i:=1 to n do
begin
read(f,a[i]);
end;
close(f);
for i:=1 to n do
tbc:=tbc+a[i];
writeln(tbc/n);
readln;
end.
Cho tệp D:\DuLieu.txt. gồm nhiều dòng, mỗi dòng chứa 2 số nguyên. VCT đọc các cặp số nguyên trong tệp đã cho và xuất ra màn hình tổng của từng cặp số tương ứng
uses crt;
const fi='dulieu.txt'
var f1:text;
a,b: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;
readln(a[n],b[n]);
end;
for i:=1 to n do
writeln(a[i]+b[i]);
close(f1);
readln;
end.
cho tệp tinhtng.inp chứa
- dòng 1: 1 số nguyên dương n
- dòng 2: dây gầm n số nguyên (các số cách nhau 1 dấu cách)
yêu cầu tính tổng dây đã cho và ghi vào tệp tinhtong.out
Uses crt;
Var i,n,tong:integer;
a:array[1..10000] of integer;
f1,f2:text;
Begin
clrscr;
Assign(f1,'tinhtong.inp');
Assign(f2,'tinhtong.out');
tong:=0;
Reset(f1);readln(f1,n);
For i:= 1 to n do
Begin
read(f1,a[i]);
tong:=tong+a[i];
End;
close(f1);
rewrite(f2);
Writeln(f2,tong);
Readln;
End.
Cho tệp số nguyên Dl.txt chứa các số nguyên , mỗi số cách nhau một kí tự trắng .Viết chương trình đọc các số nguyên dương từ tệp rồi đưa ra tổng các số dương và ghi vào tệp soam.txt các số nguyên âm .
uses crt;
const fi='dl.txt'
fo='soam.txt'
var f1,f2:text;
a:array[1..100]of integer;
i,n:integer;
begin
clrscr;
assign(f1,fi); reset(f1);
assign(f2,fo); rewrite(f2);
n:=0;
while not eoln(f1) do
begin
n:=n+1;
read(f1,a[n]);
end;
for i:=1 to n do
begin
if a[i]>0 then writeln('Can bac hai cua ',a[i],' la: ',sqrt(a[i]):4:2)
else if a[i]<0 then write(f2,a[i]:4);
end;
close(f1);
close(f2);
readln;
end.
Cho tệp số nguyên Dl.txt chứa các số nguyên , mỗi số cách nhau một kí tự trắng .Viết chương trình đọc các số nguyên dương từ tệp rồi đưa ra tổng các số dương và ghi vào tệp soam.txt các số nguyên âm .
uses crt;
const fi='dl.txt'
fo='soam.txt'
var f1,f2:text;
a:array[1..100]of integer;
i,n:integer;
begin
clrscr;
assign(f1,fi); reset(f1);
assign(f2,fo); rewrite(f2);
n:=0;
while not eoln(f1) do
begin
n:=n+1;
read(f1,a[n]);
end;
for i:=1 to n do
begin
if a[i]>0 then writeln('Can bac hai cua ',a[i],' la: ',sqrt(a[i]):4:2)
else if a[i]<0 then write(f2,a[i]:4);
end;
close(f1);
close(f2);
readln;
end.