viết chương trình đọc dữ liệu từ file văn bản so.inp gồm 2 dòng. dòng 1 ghi số nguyên dương N (N<= 100).dòng 2 dãy A gồm n số nguyên dương: a1,a2,...,an.ghi ra file so.out các số chẵn trong dãy A
#include <bits/stdc++.h>
using namespace std;
long long a[1000],n,i;
int main()
{
freopen("so.inp","r",stdin);
freopen("so.out","w",stdout);
cin>>n;
for (i=1; i<=n; i++) cin>>a[i];
for (i=1; i<=n; i++)
if (a[i]%2==0) cout<<a[i]<<" ";
return 0;
}
Bài 1: Viết chương trình nhập dữ liệu từ file input.txt gồm hai số nguyên n, m (n, m <= 10^18) và một phép tính. Thực hiện phép tính vơi hai số đó và in ra file ouput.txt?
Bài 1: Viết chương trình nhập dữ liệu từ file input.txt gồm hai số nguyên n, m (n, m <= 10^18) và một phép tính. Thực hiện phép tính vơi hai số đó và in ra file ouput.txt?
Bài 2: Tương tự như trên nhưng đầu vào là một chuỗi gồm hai số và 1 phép tính xen giữa. Nhập và xuất kết quả ra bằng file?
Câu 1: Tìm số lớn nhất File chương trình: TimMax.pas
Yêu cầu: Tìm số lớn nhất trong dãy số nguyên cho trước.
Dữ liệu vào: Đọc từ file TimMax.inp, gồm:
- Dòng thứ nhất: Số nguyên dương N (1<N<103) là số lượng các số nguyên trong dãy số.
- Dòng thứ hai: là các số nguyên trong dãy số, các số nguyên được viết cách nhau 1 khoảng trắng và không vượt quá 109.
Dữ liệu ra: Ghi vào file TimMax.out, gồm 1 giá trị duy nhất là kết quả của bài toán.
Ví dụ:
TIMMAX.INP |
TIMMAX.OUT |
4 5 6 1 2 |
6 |
Ai giúp giải câu này với
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;
}
Lập trình C++. Đếm số lần xuất hiện các phần tử trong mảng sau đó in ra tần suất xuất hiện các số trong mảng. Dữ liệu vào: Cho file văn bản MAP1.INP gồm : dòng 1 chứa số nguyên N. Dòng 2: gồm N phần tử a1, a2,... Kết quả: ghi vào file MAP1.OUT gồm: mỗi dòng là phần tử và tần suất xuất hiện ( trên thứ tự xuất hiện từ trái sang phải)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
map<ll,ll> mp;
int main()
{
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
freopen("MAP1.INP","r",stdin);
freopen("MAP1.OUT","w",stdout);
ll n; cin >> n;
ll a[n+5];
for(ll i=1;i<=n;i++) cin >> a[i], mp[a[i]]++;
for(pair<ll,ll> it:mp) cout << it.first << " " << it.second << "\n";
}
Chúc bạn học tốt!
viết ctrinh n≤ 10000 nguyên dương. Viết ra số lượng số nguyên tố và 10 giá trị số nguyên tố lớn hơn cả Dữ liệu vào: file B5.INP.TXT. Có dòng đầu là số n. Từ dòng kế ghi lần lượt n số cách nhau dấu cách hoặc xuống dòng KQ ra: ghi file B5.OUT.TXT dòng đầu ghi số lượng số nguyên tố dòng kế ghi 10 giá trị số nguyên tố lớn hơn cả được cách nhau bằng dấu cách BÀI 2 Viết chương trình đọc các dòng trong một file văn bản độ dài dòng (255 ký tự, số dòng ≤ 50000). Tính ra số từ trong file( từ nằm trọn trên dòng), và dòng có số từ nhiều nhất là bao nhiêu từ, có bao nhiêu dòng như vậy. Dữ liệu vào : file B6-INT.TXT gồm nhiều dòng văn bản. KQ ra: file B6-OUT.TXT dòng đầu ghi tổng số từ vủa file, dong 2 ghi số từ nhiều nhất của 1 dòngvà số dòng có số từ nhiều nhất
Bài 1:
const fi='b5.inp.txt'
fo='b5.out.txt'
var f1,f2:text;
a:array[1..255]of integer;
i,n,kt,j,dem:integer;
begin
assign(f1,fi); reset(f1);
assign(f2,fo); rewrite(f2);
readln(f1,n);
for i:=1 to n do
read(f1,a[i]);
dem:=0;
max:=1;
for i:=1 to n do
if a[i]>1 then
begin
kt:=0;
for j:=2 to trunc(sqrt(a[i])) do
if a[i] mod j=0 then kt:=1;
if kt=0 then
begin
inc(dem);
if max<a[i] then max:=a[i];
end;
end;
writeln(f2,dem);
if dem=0 then writeln(f2,'Khong co so nguyen to trong day')
else writeln(f2,max);
close(f1);
close(f2);
end.
Sắp xếp dãy số Tên chương trình: sorting.pas
Cho dãy N số nguyên a1, a2,..., an. Hãy sắp dãy số đã cho theo thứ tự tăng dần các số lẻ
và giảm dần các số chẵn?
Dữ liệu vào: File sorting.inp gồm hai dòng
+ Dòng 1: ghi số nguyên N (0< N<1000)
+ Dòng 2: ghi N số a1, a2,...< aN (với ai <=109
)
Dữ liệu ra: File sorting.out gồm hai dòng
+ Dòng 1: Ghi các số lẻ tăng dần
+ Dòng 2: ghi các số chẵn giảm dần
Ví dụ:
Sorting.inp Sorting.out
6
2 3 5 1 4 6
1 3 5
6 4 2
Dưới đây là mã chương trình Pascal để sắp xếp dãy số theo yêu cầu đã cho:
```pascal
program sorting;
const
MAX_N = 1000;
var
N, i, j, temp: integer;
arr: array[1…MAX_N] of integer;
oddArr, evenArr: array[1…MAX_N] of integer;
oddCount, evenCount: integer;
inputFile, outputFile: text;
begin
// Mở file input và đọc dữ liệu
assign(inputFile, 'sorting.inp');
reset(inputFile);
readln(inputFile, N);
for i := 1 to N do
read(inputFile, arr[i]);
close(inputFile);
// Sắp xếp mảng theo yêu cầu
oddCount := 0;
evenCount := 0;
for i := 1 to N do
begin
if arr[i] mod 2 = 1 then
begin
oddCount := oddCount + 1;
oddArr[oddCount] := arr[i];
end
else
begin
evenCount := evenCount + 1;
evenArr[evenCount] := arr[i];
end;
end;
// Sắp xếp mảng số lẻ tăng dần
for i := 1 to oddCount - 1 do
for j := i + 1 to oddCount do
if oddArr[i] > oddArr[j] then
begin
temp := oddArr[i];
oddArr[i] := oddArr[j];
oddArr[j] := temp;
end;
// Sắp xếp mảng số chẵn giảm dần
for i := 1 to evenCount - 1 do
for j := i + 1 to evenCount do
if evenArr[i] < evenArr[j] then
begin
temp := evenArr[i];
evenArr[i] := evenArr[j];
evenArr[j] := temp;
end;
// Mở file output và ghi kết quả
assign(outputFile, 'sorting.out');
rewrite(outputFile);
for i := 1 to oddCount do
write(outputFile, oddArr[i], ' ');
writeln(outputFile);
for i := 1 to evenCount do
write(outputFile, evenArr[i], ' ');
close(outputFile);
end.
```
Bạn có thể sao chép mã chương trình trên vào một tệp tin có tên `sorting.pas`, sau đó tạo một tệp tin `sorting.inp` và nhập dữ liệu theo định dạng đã cho. Chạy chương trình và kết quả sẽ được ghi vào tệp tin `sorting.out`.
var i,n:longint; a:array[1..1000] of longint;
begin
readln(n);
for i:=1 to n do read(a[i]);
for i:=1 to n do
if a[i] mod 2=0 then
begin
inc(k);
b[k]:=a[i];
end
else
begin
inc(t);
c[t]:=a[i];
end;
for i:=1 to k-1 do
for j:=i+1 to k do
if b[i]<b[j] then
begin
d:=b[i];
b[i]:=b[j];
b[j]:=d;
end;
for i:=1 to t-1 do
for j:=i+1 to t do
if c[i]>c[j] then
begin
d:=c[i];
c[i]:=c[j];
c[j]:=d;
end;
for i:=1 to k do write(b[i],' ');
for i:=1 to t do write(c[i],' ');
end.
Viết chương trình Pascal, đặt tên chương trình là tên của con sau đó tải file ảnh lên đây. Chương trình thực hiện công việc sau: Khai báo 1 mảng A chứa tối đa 50 số nguyên, nhập n từ bàn phím, nhập vào mảng A gồm n số, viết mảng A ra màn hình (tất cả các phần tử trên cùng 1 dòng?
uses crt;
var a:array[1..50]of integer;
i,n:integer;
begin
clrscr;
readln(n);
for i:=1 to n do
readln(a[i]);
for i:=1 to n do write(a[i]:4);
readln;
end.