Cho tệp văn bản có tên A.TXT lưu trữ dữ liệu của một mảng N số nguyên và có dạng sau: - Dòng đầu ghi số nguyên dương N (N< 100)
- Dòng 2 ghi N số nguyên, mỗi số cách nhau 1 dấu cách
Bài 1. Cho tệp văn bản input.txt gồm nhiều dòng. dòng đầu tiên ghi số nguyên dương N, N dòng tiếp theo, mỗi dòng ghi một số nguyên không lớn hơn 10000. a. Đọc dữ liệu từ tệp, đếm xem có bao nhiêu số dương chẵn. b. Đọc dữ liệu từ tệp, sắp xếp N số theo thứ tự tăng dần rồi ghi kết quả vào tệp output.txt. Ai biết làm giúp mk vớii
Cho dãy số nguyên A[1], A[2], …, A[n], các số đôi một khác nhau và số nguyên dương k (1 ≤ k ≤ n). Hãy đưa ra gá trị nhỏ thứ k trong dãy.
Dữ liệu vào: Tệp văn bản MINK.INP gồm:
+ Dòng đầu ghi hai số nguyên n và k (1 ≤ k ≤ n ≤ 105) cách nhau một dấu cách.
+ Dòng thứ hai ghi n số nguyên A[1], A[2], …, A[n] có giá trị tuyệt đối không vượt quá 106, giữa các số cách nhau một dấu cách.
Kết quả ra: Đưa ra tệp văn bản MINK.OUT, chỉ ghi một số duy nhất là số nhỏ thứ k trong dãy. Ví dụ dãy: 6, 7,8, 4, 3, 2 và k = 4 thì giá trị nhỏ thứ k là 6.
MINK.INP | MINK.OUT |
6 46 7 8 4 3 2 | 6 |
c++ ạ
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int n,k;
cin >> n >> k;
int a[n];
for (int i=0;i<n;i++)
cin >> a[i];
sort(a,a+n);
cout << a[k-1];
return 0;
}
Mn giúp mik bt Tin Học với ạ..! Mn lm đc bài nào thì làm nha ...!
Câu 1 (7,0 điểm): Số chính phương.
Cho trước số nguyên dương N (0< N≤ 106 ). Yêu cầu: Tìm số nguyên dương K nhỏ nhất sao cho tích của K và N là một số chính phương. Dữ liệu vào: File CP.INP chứa số N. Dữ liệu ra: File CP.OUT ghi số nguyên K tìm được.
Câu 2 (6,0 điểm): Dòng lớn nhất.
Cho một tệp tin gồm nhiều dòng. Trên mỗi dòng chứa một xâu kí tự chỉ gồm các kí tự chữ cái và chữ số, độ dài của mỗi xâu không quá 255 kí tự.
Yêu cầu: Đưa ra dòng có nhiều kí tự chữ cái nhất, nếu có nhiều dòng thỏa mãn thì đưa ra dòng đầu tiên có nhiều kí tự chữ cái nhất. Dữ liệu vào: File DLN.INP gồm:
+ Dòng đầu ghi số N là số lượng dòng chứa các xâu kí tự.
+ N dòng tiếp theo: mỗi dòng ghi một xâu kí tự. Dữ liệu ra: File DLN.OUT ghi ra dòng có nhiều kí tự chữ cái nhất, nếu có nhiều dòng thỏa mãn thì đưa ra dòng đầu tiên có nhiều kí tự chữ cái nhất.
Câu 3 (4,0 điểm): Dãy con đối xứng.
Một dãy số liên tiếp gọi là dãy đối xứng nếu đọc các số theo thứ tự từ trái sang phải cũng giống như khi đọc theo thứ tự từ phải sang trái. Cho dãy số A gồm N số nguyên dương: a1, a2,..., aN (1≤ N≤ 10000; 1≤ ai≤ 32000; 1≤ i≤ N)
Yêu cầu: Hãy tìm dãy con đối xứng dài nhất của dãy A. Nếu có nhiều dãy con thoả mãn thì lấy dãy con xuất hiện đầu tiên trong dãy A. Dữ liệu vào: File DX.INP gồm 2 dòng:
- Dòng 1: ghi số nguyên dương N.
- Dòng 2: ghi N số nguyên dương lần lượt là giá trị của các số trong dãy A, các số được ghi cách nhau ít nhất một dấu cách.
Dữ liệu ra: File DX.OUT ghi dãy tìm được trên cùng một dòng, các số được ghi cách nhau một dấu cách.
Câu 4 (3,0 điểm): Dãy nguyên tố.
Cho một dãy số B gồm n số nguyên dương (n ≤ 1000), mỗi phần tử trong dãy có giá trị không quá 30000. Yêu cầu:
+ Tìm dãy con dài nhất (liên tiếp hoặc không liên tiếp) các phần tử là những số nguyên tố có giá trị tăng dần của dãy B và thứ tự của các phần tử không đổi so với ban đầu. Ví dụ: Dãy 8 phần tử {4, 2, 5, 6, 3, 3, 7, 9} có dãy con nguyên tố tăng dài nhất là {2, 5, 7}.
+ Nếu có nhiều dãy con thoả mãn thì lấy dãy con xuất hiện đầu tiên trong dãy B. Dữ liệu vào: File NT.INP gồm 2 dòng:
- Dòng 1: Ghi số nguyên dương n.
- Dòng 2: Ghi n số nguyên dương, các số được ghi cách nhau một dấu cách. Dữ liệu ra: File NT.OUT ghi dãy con tìm được trên cùng 1 dòng, giữa 2 phần tử liền kề trong dãy có một dấu cách.
Cho tệp văn bản DAYSO.INP có cấu trúc:
+ Dòng 1: Ghi số nguyên dương N (0<N<=100)
+ Dòng 2: Ghi dãy gồm n số nguyên Ai (-30000<=Ai<=30000).
Yêu cầu: Viết chương trình đọc dữ liệu từ tệp trên và thực hiện các công việc sau:
a) Tính tổng các số ở dòng 2, ghi kết quả vào tệp TONG.OUT theo cấu trúc:
+ Dòng 1: Ghi số nguyên S là tổng tìm được
b) Tính tổng các số dương ở dòng 2, ghi kết quả vào tệp TD.OUT theo cấu trúc:
+ Dòng 1: Ghi số nguyên S là tổng các số dương tìm được
c) Đếm số lượng các số chẵn ở dòng 2, ghi kết quả vào tệp SOCHAN.OUT theo cấu trúc:
+ Dòng 1: Ghi số nguyên k là số lượng số chẵn
+ Dòng 2: Ghi các số chẵn tìm được, các số ghi cách nhau 1 dấu cách trống.
d) Đếm số lượng các số âm chẵn ở dòng 2, ghi kết quả vào tệp SOAMCHAN.OUT theo cấu trúc:
+ Dòng 1: Ghi số nguyên k là số lượng số âm chẵn
+ Dòng 2: Ghi các số âm chẵn tìm được, các số ghi cách nhau 1 dấu cách trống.
e) Sắp xếp các số ở dòng 2 để được dãy không giảm, ghi kết quả vào tệp SAPXEP.OUT theo cấu trúc:
+ Dòng 1: Ghi dãy số đã được sắp xếp, các số ghi cách nhau 1 dấu cách trống.
f) Đếm số lượng các số nguyên tố ở dòng 2, ghi kết quả vào tệp NTO.OUT theo cấu trúc:
+ Dòng 1: Ghi số nguyên k là số lượng số nguyên tố
+ Dòng 2: Ghi các số nguyên tố tìm được, các số ghi cách nhau 1 dấu cách trống.
HƠI DÀI NHMA MONG MẤY BẠN GIÚP CHỨ MÌNH CHỊU R
Cho tệp SN3.INP chứa n số nguyên dương (mỗi số cách nhau một dấu cách trên một dòng). Viết chương trình đọc dữ liệu từ tệp đã cho Tính ƯCLN dãy các số nguyên dương đó và lưu kết quả vào tệp UC.OUT .
#include <bits/stdc++.h>
using namespace std;
unsigned long long a[1000],i,n,uc;
//chuongtrinhcon
unsigned long long ucln(long long a,long long b)
{
if (b==0) return(a);
else return(ucln(b,a%b));
}
//chuongtrinhchinh
int main()
{
freopen("sn3.inp","r",stdin);
freopen("uc.out","w",stdout);
cin>>n;
for (i=1; i<=n; i++) cin>>a[i];
uc=ucln(a[1],a[2]);
for (i=3; i<=n; i++)
uc=ucln(uc,a[i]);
cout<<uc;
return 0;
}
Bài 1: Đọc/ghi mảng 1 chiều 1. Nhập mảng một chiều A có n phần tử từ bàn phím, sau đó dữ liệu vào tệp DaySo.inp theo quy cách:
- Dòng 1: ghi n là số lượng phần tử
- Dòng 2: Chữ n số, mỗi số cách nhau một dấu cách
- Ví dụ: 5
2 4 1 8 5 2. Đọc tệp DaySo.inp ra mảng B, sau đó in mảng B ra màn hình 3. Sắp xếp dãy số trong mảng B tăng dần, sau đó ghi vào tệp SapXep.inp, quy cách ghi như câu 1.
giúp em với ạ :((
Bài 4: Tìm số dư của phép chia cho 9. CHIA9.PAS Cho một số nguyên dương N có M chữ số. Yêu cầu: Tìm số dư của phép chia số N cho 9. Dữ liệu vào: Cho trong file văn bản CHIA9.INP, có cấu trúc như sau: - Dòng 1: Ghi số nguyên dương M là số lượng chữ số của số N (1 ≤ M ≤ 100). - Dòng 2: Ghi M chữ số của số N, các chữ số được ghi liền nhau. Dữ liệu ra: Ghi ra file văn bản CHIA9.OUT, theo cấu trúc như sau: - Dòng 1: Ghi số nguyên dương Q, là số dư tìm được. Ví dụ: CHIA9.INP CHIA9.OUT 5 74283 6
Bài 5: Tìm số sát sau - SOSATSAU.PAS Cho số tự nhiên A có N chữ số. Hãy hoán vị các chữ số trong A để thu được số B thoả mãn đồng thời hai điều kiện sau: - B lớn hơn A. - B nhỏ nhất. Dữ liệu vào: Cho trong file SOSATSAU.INP có cấu trúc như sau: - Dòng 1: Ghi số N là số lượng chữ số của A (0a[i-1]. Do đoạn cuối giảm dần, điều này thực hiện bằng cách tìm từ cuối dãy lên đầu gặp chỉ số k đầu tiên thỏa mãn a[k]>a[i-1] (có thể dùng tìm kiếm nhị phân) - Đảo giá trị a[k] và a[i-1] - Lật ngược thứ tự đoạn cuối giảm dần (từ a[i] đến a[k]) trở thành tăng dần + Nếu không tìm thấy tức là toàn dãy đã sắp xếp giảm dần, đây là hoán vị cuối cùng.
Bài 2. MẬT KHẨU. Cu Tí thường xuyên tham gia thi lập trình trên mạng. Vì đạt được thành tích cao nên Tí được gửi tặng một phần mềm diệt virus. Nhà sản xuất phần mềm cung cấp cho Tí một mã số là một dãy gồm các bộ ba chữ số ngăn cách nhau bởi dấu chấm và có chiều dài không quá 255 (kể cả chữ số và dấu chấm). Để cài đặt được phần mềm, Tí phải nhập vào mật khẩu của phần mềm. Mật khẩu là một số nguyên dương M được tạo ra bằng cách tính tổng giá trị các bộ ba chữ số trong dãy mã số, các bộ ba này được đọc từ phải sang trái. - Yêu cầu: Cho biết mã số của phần mềm, hãy tìm mật khẩu của phần mềm đó. - Dữ liệu vào: Cho từ tệp văn bản có tên BL2.INPgồm một dòng chứa xâu ký tự S (độ dài xâu không quá 255 ký tự) là mã số của phần mềm. - Kết quả: Ghi ra tệp văn bản có tên BL2.OUTgồm một số nguyên là mật khẩu tìm được. MK.INP MK.OUT 123.234 257
Bài 6: Biến đổi số BIENDOI.PAS Cho một số nguyên dương M có K chữ số (0 < M; 1 ≤ K ≤ 200). Người ta thực hiện biến đổi số M bằng cách xóa đi trong M các chữ số 0 và sau đó sắp xếp các chữ số còn lại theo thứ tự không giảm của giá trị từng chữ số. Gọi số nguyên dương N là số thu được sau khi thực hiện biến đổi số M. Yêu cầu: Hãy tìm số nguyên dương N. Dữ liệu vào: Nhập vào từ tệp biendoi.inp số M Dữ liệu ra: Ghi ra tệp biendoi.out số N Ví dụ: M=3880247 N=234788
Viết chương trình tính tổng của n số nguyên có trong tệp Tong.inv. đưa giá trị tìm được ra tệp Tong.out. Dữ liệu vào gồm hai dòng dòng 1 ghi số nguyen dương n dòng 2 ghi n số nguyên dương dữ liệu ra một số nguyên duy nhất là tổng tìm được
const fi='tong.inp'
fo='tong.out'
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);
readln(f1,n);
for i:=1 to n do
read(f1,a[i]);
t:=0;
for i:=1 to n do
t:=t+a[i];
writeln(f2,t);
close(f1);
close(f2);
end.
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.