Professional Documents
Culture Documents
Ph
p
bi
n
Php bin i
1 2 3 4
Trong bi ny chng ta xt
B
phng n khi mi hnh vung
4 1 2 3
1 2 3 4
Php bin
c t mt mu khc nhau. Mu
i A
Php bin i 5 8
7 6
8 7 6 5
c nh bng tm s nguyn
B
Trng thi ban u
Php bin
dng u tin (xem hnh v).
1 7 2 4
i C
Cu hnh ca bng c xc nh
8 6 3 5
bng cch cho mu ca cc hnh
vung, bt u t hnh gc trn
tri v i theo chiu kim ng h. V d, hnh bn cu hnh ban u ca bng
c xc nh bi dy (1, 2, 3, 4, 5, 6, 7, 8).
p
Ph
n
bi
OUTPUT.TXT
2/64
2 6 8 4 5 7 3 1
7
B
C
A
B
C
C
B
Phn tch: S lng trng thi khc nhau ca bng l 8! = 40 320. Vic t chc
loang v lu tr vt loang t trng thi ban u ti trng thi ch trong iu
kin hin nay khng phi l mt chuyn kh. Nhng vo thi im din ra k
thi, b nh c th c s dng ch l 64KB + 64KB cp pht ng v my
thc hin thuc loi 386 th l c mt vn ln.
Chng ta t c nhng kt qu rc r trong thi k ny v i ng gio
vin tin hc phn ln xut thn t gio vin chuyn ngnh ton. Hc sinh ca
chng ta c trang b kin thc c s v ton kh tt.
Giai on II t nhng nm 2001 n 2007, s tin b v cng ngh v s ph
cp ca cc h thng phn mm tin tin a n nhng s chuyn dch
trong vic o to chuyn gia trong lnh vc tin hc. iu ny dn n nhng
thay i trong cng tc pht hin, o to v bi dng hc sinh gii tin hc.
Do ni dung thi Tin hc Quc t cng c nhiu thay i. Cc kin thc gii
thut c coi l nh cao trc y by gi tr thnh bng cu chng
m ai cng phi bit v phi thuc. Nhng gii thut phc tp, t dng th khng
nht thit phi thuc, nhng bt c ai v lc no cng c th tra cu, tm kim
chng trn Internet khi cn thit. S thng minh v tnh sng to by gi phi
th hin khng phi ch bn thuc nhiu hay t cc gii thut khc nhau, cng
khng phi bn ci t chng nhanh ti mc no. Th thch by gi l ch
bn c th tm ra nhng gii php hu hiu gii quyt mt cch c hiu qu cc
bi ton cc vn c m hnh ton hc n gin nhng c kch thc ln hay
khng?
t c mc ch ngi lp trnh phi bit tn dng ti a kh nng m
phn cng v h iu hnh cung cp. Cc h thng lp trnh mi nh Free
Pascal, Dev C++ (nhng phin bn u tin) to iu kin ngi dng
khai thc c ti a kh nng ca phn cng.
ng tic l ti tn nm 2007 khi th gii i ht chng ng quan trng ny
th chng ta mi c php chnh thc t chn ln mnh t in y du
3/64
4/64
1
1
GARDEN.OUT
2
2
4
V d
PASCAL C/C++
+
*
/
DIV
MOD
+
*
/
/
%
Pascal
a
a
a
a
n
n
+ b
- b
* b
/ b
div m
mod m
C/C++
a
a
a
a
n
n
+
*
/
/
%
b
b
b
b
m
m
PASCAL C/C++
<
<=
=
>=
<>
and
or
not
<
<=
==
>=
!=
&&
||
!
V d
Pascal
a < b
a <= b
a = b
a >= b
a<>b
Pascal s dng cc
php x l bit to
biu thc quan h.
7/64
C/C++
a < b
a <= b
a == b
a >= b
a!=b
PASCAL C/C++
not
and
or
xor
shl
shr
V d
Pascal
C/C++
~
|
&
^
<<
>>
Khai bo
PASCAL
C++
Var i,j,k:integer;
m,n: longint;
a,b:real;
p,d:int64;
x:array[0..20] of longint;
y:array[1..10, 1..5] of longint;
z:array[1..15] of real;
c:char;
s:string;
int i,j,k;
long m,n,x[21], y[10][5];
float a,b,z[15];
long long p,d;
char c;
string s;
Lnh gn
C++
PASCAL
a:=y[i,j];
a:=a+b;
a:=a*z[i];
i:=i+1;
k:=i div j;
k:=i mod j;
a=y[i][j];
a+=b;
// a=a+b;
a*=z[i];
i++;
// ++i;
k=i/j;
k=i%j;
8/64
Lnh IF
Khng c then
PASCAL
t iu kin
trong ngoc
Lu c ;
C++
if (a!=b) c+=5;
if (a==b)z[i]=0;
else
{z[i]+=c;
Z[n-i]-=c;
}
Lu c ;
Cu lnh FOR
C++
PASCAL
for i:=1 to n do
begin
. . . .
end;
for j:= n downto 1 do
begin
. . . .
end;
for (i=1;i<=n;i++)
{
. . . .
}
for (i=n;i>0;i--)
{
. . . .
}
9/64
PASCAL
repeat
. . . . .
until j=i;
do
{
. . . .
} while (i==j);
while i<j do
begin
. . . .
end;
while (i<j)
{
. . . .
}
PASCAL
Khai bo th vin
Program NIM;
Uses crt, matrix;
Khai bo th vin,
tha khng sao!
10/64
T chc vo ra vi file vn bn
Trong phn khai bo.
Flies t ng c m,
C++
PASCAL
ifstream fi (NIM.INP);
ofstream fo (NIM.OUT);
Const tfi=NIM.INP;
Tfo=NIM.OUT;
Var fi,fo:text;
. . . . . . . .
fi>>a>>b>>c;
fi.close();
. . . . . . . .
fo<<a<< <<b<<endl;
fo<<c;
fo.close();
. . . . . . .
Assign(fi,tfi); reset(fi);
Readln(fi,a,b,c);
Close(fi);
. . . . . . .
Assign(fo,tfo);
Rewrite(fo);
Writeln(fo,a, ,b);
Write(fo,c);
Close(fo);
11/64
C++
void p1(int i,int j)
{ . . . . }
void p2()
{ . . . . }
int fact(int i)
{ . . .
return(k);
}
. . . . . . .
int main()
{ . . . .
}
V d:
C++
PASCAL
Program NIM;
Const tfi='NIM.INP';
tfo='NIM.OUT';
Var a:array[1..1000] of longint;
i,n,g,t,k:longint;
fi,fo:text;
BEGIN
assign(fi,tfi);
reset(fi);
readln(fi,n);
for i := 1 to n do readln(fi,a[i]);
close(fi);
g:=0;
for i:=1 to n do g:=g xor a[i];
k:=0;
for i:=1 to n do
begin
t:=a[i]-(a[i] xor g);
if t>0 then
begin
inc(k);
a[i]:=t
end
else a[i]:=0
end;
assign(fo,tfo); rewrite(fo);
writeln(fo,k);
X lifxu
k > 0 then
for i:=1 to n do if a[i]>0 then
writeln(fo,i,' ',a[i]);
close(fo)
END.
12/64
#include <fstream>
#include <conio.h>
#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{long n,g,t,k;
ifstream fi ("NIM.INP");
ofstream fo ("NIM.OUT");
fi >> n;
{long a[n+1];
for (long i = 1; i <=n;i++)
fi>>a[i];
k=0;
g=0;
for (long i=1; i <=n;i++) g^=a[i];
if (g>0)
{ for (long i=1;i<=n;i++)
{ t = a[i] (a[i]^g);
if (t>0)
{k++;a[i]=t;
} else a[i]=0;
}
}
fo<<k<<endl;
if (k>0)
for (long i= 1; i<=n;i++) if
(a[i]>0) fo<<i << " "<<a[i]<<endl;
fi.close(); fo.close();
}
}
// l=s.length(); 19 l
// 1 k (bt u t 0)
// 14 m
// 2 p
// 15 q
// 4 i
// 17 j
13/64
C++
#include <fstream>
#include <string>
#include <iostream>
using namespace std;
int main()
{ string
s=("ABCD12345abcdABCDE2");
string ns=("0123456789");
int l,k,m,p,q,i,j;
ofstream fo ("xl_xau.out");
l=s.size();
k=s.find("BC");
m=s.rfind("BC");
p=s.find_first_of('C');
q=s.find_last_of('C');
i=s.find_first_of(ns);
j=s.find_last_not_of(ns);
XL_XAU.OUT
L
K
M
P
Q
I
J
fo<<"L = "<<l<<endl;
fo<<"K = "<<k<<endl;
fo<<"M = "<<m<<endl;
fo<<"P = "<<p<<endl;
fo<<"Q = "<<q<<endl;
fo<<"I = "<<i<<endl;
fo<<"J = "<<j<<endl;
fo.close();
}
14/64
=
=
=
=
=
=
=
19
1
14
2
15
4
17
S : Char = s ;
begin
Writeln ( P , s t a r t i n g from f i r s t s : ,StrScan (P, s ) ) ;
Writeln ( P , s t a r t i n g from l a s t s : ,StrRScan (P, s ) ) ;
end .
Find_first v Find_last
PASCAL
Program Example14 ;
Uses s t r i n g s ;
{ Program to demonstrate the StrLower and StrUpper f u n c t i o n s . }
Const P1 : PChar = THIS IS AN UPPERCASE PCHAR STRING ;
P2 : PChar = t h i s i s a lowercase s t r i n g ;
begin
Writeln ( Uppercase : ,StrUpper (P2 ) ) ;
StrLower ( P1 ) ;
Writeln ( Lowercase : ,P1 ) ;
end .
Ch hoa v ch thng
(c cng c tng ng trong C)
15/64
PASCAL
Program Example15 ;
Uses s t r i n g s ;
{ Program to demonstrate the StrPos f u n c t i o n . }
Const P : PChar = This i s a PChar s t r i n g . ;
S : Pchar = i s ;
begin
Writeln ( Pos i t ion of i s i n P : , s i z e i n t ( StrPos (P,S)) - s i z e i n t (P ) ) ;
end .
Tm v tr xu con
Nh vy, vic dng PASCAL hay C++ u c th gii quyt mt cch hiu qu
cc bi ton thi hc sinh gii. Tuy vy, trong mi trng hp, hc sinh cn
c:
Trang b thm cc kin thc mi,
C thi quen khai thc cc cng c trong h thng lp trnh,
Bit cch s dng hp l v c hiu qu cc cng c hin c.
l mt cng dy v hc, ti sao ta khng lm vic ngay vi C++? iu ny
l hon ton kh thi v c li v:
V mt php l:
o B Gio dc cho php s dng C/C++ trong k thi,
o Cng c: cng thuc loi Open Sources (min ph),
V thi gian:
o 02 tit cho vic gii thiu cch vit chng trnh trn C++,
o 02 tit cho vic lm quen vi mi trng lp trnh Dev C++,
o Cc kin thc khc, nu cn, trang b dn trong sut qu trnh bi
dng (theo nguyn tc Ma dm thm t),
Li ch:
o Th vin chun STL ca C++ c nhiu dch v cn thit vi hc
sinh (v d nh cc cng c sp xp theo cc gii thut khc nhau,
cc cng c tm kim, t chc stack, heap, x l vector, ma trn v.
v. . .),
o Ti liu: rt phong ph,
16/64
17/64
SP XP
Hm sp xp nhanh trn C++
void quickSort(int arr[], int left, int right) {
int i = left, j = right;
int tmp;
int pivot = arr[(left + right) / 2];
/* partition */
while (i <= j) {
while (arr[i] < pivot)
i++;
while (arr[j] > pivot)
j--;
if (i <= j) {
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
i++;
j--;
}
};
12
26
14
Cha sp xp
12
26
14
Gi tr trc = 7
12
26
14
12 7 2 i ch
i
2
26
14
14
Gi tr trc
j
j
12
26 7 7 i ch
26 12
7 7 3 i ch
26 12
26
i
7
i
1
/* recursion */
if (left < j)
quickSort(arr, left, j);
if (i < right)
quickSort(arr, i, right);
}
j
3
14
1 2 5 7 3
14
12
X l quy cc nhm
12 14 26
{
int val, idx; //val: b[i], idx: cs[i]
}Element;
typedef Element Arr[MAX];
int cmpf(const void *a, const void *b);
int main()
{
Arr a = {{5, 4}, {3, 2}, {5, 3}, {5, 1}, {2, 5}};
int n = 5;
qsort(a, n, sizeof(Element), cmpf);
for (int i=0; i<n; i++)
printf("%d %d\n", a[i].val, a[i].idx);
getchar();
}
int cmpf(const void *a, const void *b)
{
return ((((Element *)a)->val - ((Element *)b)->val>0)||
(((Element *)a)->val == ((Element *)b)->val)&&(((Element
*)a)->idx > ((Element *)b)->idx));
}
Chng trnh trn C++:
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
typedef class CElement
{
int val, idx; //val: b[i], idx: cs[i]
public:
CElement(int v=1, int i=1)
{
val = v; idx = i;
}
int GetValue()
{
return val;
}
};
int cmpf(const void *a, const void *b);
19/64
20/64
BI TP V CHNG TRNH
h tr cho vic trin khai ging dy trn C++ cc bi tp di y s c gii thiu km
theo vi li gii trn ngn ng C/C++ v Pascal (da trn c s h thng lp trnh Free
Pascal). Khi c t tin trong vic lp trnh ta s thy li gii a ra di dng chng
trnh hon thin ch c tc dng ch yu to tests.
Vic trnh by gii thut di dng s khi cng khng gip nhiu cho vic nm bt gii
thut m ch h tr cho vic vit chng trnh (coding) c d dng. Ngoi ra, s khi
cn hn ch tnh sng to trong vic trin khai gii thut.
Vi mi bi ton, quan trng v cn thit hn c l gi v gii thut, bao gm:
Hng trin khai gii thut,
Cn c nhng d liu g, t chc nh th no, gi tr u, cch bin i, dn xut,
Nhng im c bit cn lu ,
nh gi phc tp,
Vi mt vi khu x l quan trng: c th dn xut cu lnh hoc on chng trnh.
Vic dn xut y chng trnh cng rt cn thit, nhng ch giai on u ca qu trnh
o to, bi dng hc sinh.
Vic t chc gi gii thut cn mt nhiu thi gian v cng sc hn c vic trc tip vit
chng trnh gii!
Tuyt i trnh cung cp cho hc sinh chng trnh hon thin ngay t u, khi hc sinh
cha hiu gii thut, cha bt tay t mnh lp trnh.
Li gii cho cc bi tp phn u c a ra dng song ng. na cui ca phn bi
tp, khi c kinh nghim vi C++, cc li gii ch a ra dng chng trnh trn C++
hoc C/C++.
21/64
TP CON
SUBSETS.OUT
3 2
4 9
1 2
22/64
k--
i =n-1 0
k k-1
1 0 1
2 1 1 2
3 2 1 2 3
4 3 1 3
5 4 2
6 5 2 3
7 6 3
i =n-1 0
Bit th i ca
k t
F
t=0
di = 1
T
di=0
di=1
k=0
a ra i
T
F
k--
C++
#include <fstream>
#include <string>
int n,d[61];
long long k,t,t1=1;;
using namespace std;
ifstream fi ("SUBSETS.INP");
ofstream fo ("SUBSETS.OUT");
void xly()
{ k--;
for (int i=n-1; i>=0;i--)
{ t=(t1<<i)&k;
if ( t==0)
{d[n-i]=1; if (k==0) break;k--;}
else d[n-i]=0;
}
for (int i=1;i<=n;i++) if (d[i]==1) fo<<i<<" ";
fo<<endl;
}
int main()
{ while (! fi.eof())
{ fi>>n>>k;
//if (n==0) break;
xly();
}
fi.close();
fo.close();
}
23/64
Exit
PASCAL
Program SUBSETS;
Const tfi='SUBSETS.INP';
tfo='SUBSETS.OUT';
t1:int64=1;
Var n:integer;
k,t:int64;
d:array[1..60] of byte;
fi,fo:text;
Procedure xly;
var i:integer;
Begin
dec(k);
for i:= n-1 downto 0 do
begin
t:=(t1 shl i) and k;
if t=0 then
begin d[n-i]:=1;
if k = 0 then break; dec(k)
end
else begin d[n-i]:=0; end
end;
for i:=1 to n do if d[i]=1 then write(fo,i,' ');
writeln(fo)
End;
BEGIN
assign(fi,tfi); reset(fi);
assign(fo,tfo); rewrite(fo);
while not eof(fi) do
begin
readln(fi, n, k);
xly
end;
close(fi); close(fo)
END.
24/64
INTERNET
Gi truy cp internet ph thuc vo tng thi im trong tun. Cc ngy trong tun c
nh s t 1 n 7 bt u t th 2. Bng tnh gi gm n bn ghi (1 n 100), sp xp theo
th t tng dn ca thi gian, mi bn ghi c dng:
d cc:mm v
trong d l ngy trong tun, cc 2 s ch gi (ch 24 gi), mm 2 s ch pht, v gi
mi pht truy nhp, l s nguyn (1 v 104). Gi truy nhp c gi nguyn cho n khi
gp thi im mi bn ghi tip theo trong bng. Sau bn ghi th n l bng ghi s 1.
Yu cu: Cho bng tnh gi v danh sch xc nh m phin truy nhp mng (1 m 105).
Mi phn t ca danh sch c dng d cc:mm t, trong d, cc, mm c ngha nh trong
bng gi, t thi gian truy nhp tnh theo pht, l s nguyn (1 t 109). Cc phin truy
nhp c th thuc nhng tun khc nhau. Hy tnh chi ph lt mng.
D liu: Vo t file vn bn INTERNET.INP:
Dng u tin cha 2 s nguyn n v m,
Mi dng trong n dng tip theo cha mt bng ghi ca bng gi,
m dng cui cng mi dng cha mt bn ghi v mt phin truy nhp.
Kt qu: a ra file vn bn INTERNET.OUT mt s nguyn chi ph phi tr.
V d:
INTERNET.INP
2 3
1 09:00 500
5 22:00 200
2 22:42 16
5 21:06 57
2 22:50 1
INTERNET.OUT
36100
25/64
C++
#include <fstream>
#include <string>
using namespace std;
long n,m,d,v,t,a[10081];
long long r;
string s;
ifstream fi ("INTERNET.INP");
ofstream fo ("INTERNET.OUT");
void nhap()
{ long i,j;
s.reserve(6);
fi>>d>>s>>v;
i=(s[0]-48)*10+s[1]-48;
j=(s[3]-48)*10+s[4]-48;
t= --d*1440+i*60+j+1;
}
void xd_a()
{
memset(a,0,sizeof(a));
for (int i=1;i<=n; i++)
{ nhap();
a[t]=v;
}
for (int i=1;i<=10080;++i)
{if (a[i]!=0) v=a[i];
a[i]=a[i-1]+v;
}
}
void xd_cf()
{long p,q;
p=v/10080; q=v%10080;
r+=a[10080]*p;
if (t+q>10081){r+= (a[10080]-a[t-1]);q-= (10081-t);t=1;}
r+=(a[t+q-1]-a[t-1]);
}
int main()
{fi>>n>>m;
r=0;
xd_a();
for (int i=1; i <=m; i++)
{nhap();
xd_cf();
}
fo<<r;
fi.close(); fo.close();
}
26/64
PASCAL
Program Internet;
Const tfi = 'INTERNET.INP';
tfo = 'INTERNET.OUT';
Var n,m,d,v,i:integer;
t:longint;
r:int64;
a:array[-1..10079] of longint;
fi,fo:text;
Procedure nhapdl;
var i,j:longint;
s:string[6];
Begin
readln(fi,d,s,v);
dec(d);
i:=(ord(s[2])-48)*10+ord(s[3])-48;
j:=(ord(s[5])-48)*10+ord(s[6])-48;
t:=d*1440+i*60+j
End;
Procedure xd_a;
var i:longint;
Begin
fillchar(a,sizeof(a),0);
for i:=1 to n do
begin
nhapdl;
a[t]:=v;
end;
for i:=0 to 10079 do
begin
if a[i]<>0 then v:=a[i];
a[i]:=a[i-1]+v
end
End;
Procedure xd_cf;
var p,q:longint;
Begin
p:=v div 10080; q:= v mod 10080;
r:=r+a[10079]*p;
if q+t>10080 then
begin r:=r+a[10079]-a[t-1];q:=q-10080+t;t:=0
r:=r+a[q+t-1]-a[t-1]
End;
BEGIN
assign(fi,tfi); reset(fi);
readln(fi,n,m);
xd_a; r:=0;
for i:= 1 to m do
begin
nhapdl;
xd_cf
end;
assign(fo,tfo); rewrite(fo);
write(fo,r);
close(fi); close(fo)
END.
27/64
end;
BNH XE MAY MN
WHEEL.OUT
HONITAVR
8
V
I
T
A
R
N
O
H
28/64
WHEEL.INP
8
4
3
7
7
6
5
1
9
C++
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
string s,s2;
char c;
long n,k,m,fl,v;
ifstream fi ("WHEEL.INP"); ofstream fo ("WHEEL.OUT");
void chbi()
{
fi>>n>>k;
s=""; fl=0;v=n-1;
for (int i = 0; i<n; i++) s+='?';
}
void xldong()
{ fi>>m>>s2;
c=s2[0];
m%=n;
v-=m;
if (v<0) v+=n;
if (s[v]=='?') s[v]=s2[0];
else
if (s[v]!= c) {fl=1; return;}
}
void gnkq()
{ int i;
if (fl==1) s='!';
else
{i=s.find(c); s+=s;
s=s.substr(i,n);
}
fo <<s;
}
int main()
{
chbi();
for (int i=0; i<k; i++) xldong();
gnkq();
fi.close(); fo.close();
}
29/64
PASCAL
Program WHEEL;
Const tfi = 'WHEEL.INP';
tfo = 'WHEEL.OUT';
Var n,k,m,i,err,v:integer;
c0,c:char;
s:string[50];
fi,fo:text;
BEGIN
assign(fi,tfi); reset(fi);
readln(fi,n,k);
s:=''; err:=0; v:=n;
for i:= 1 to n do s:=s+'?';
for i:=1 to k do
begin
readln(fi,m,c0,c);
v:=v-m; if v<=0 then v:=v+n;
if ((s[v]<>'?') and (s[v]<>c)) then
begin err:=1; s:='!'; break end;
s[v]:=c
end;
assign(fo,tfo); rewrite(fo);
if err=0 then
begin s:=s+s; s:=copy(s,v,n) end;
write(fo,s);
close(fi); close(fo)
END.
30/64
GHI TA
Trong cuc thi sng to r bt Steve trnh din r bt chi ghi ta ca mnh, mt r bt,
theo li m t ca tc gi c n hng t ngn tay!
n ghi ta c 6 dy nh s t 1 n 6 v c p phm nh s t
1 n p. Khi chi nhc ngi ta gy dy v bm phm c cc
giai iu khc nhau. Vi mi dy, nu c nhiu phm cng
c bm th m iu s c quyt nh bi phm c s cao
nht. V d, m iu khi gy dy s 3 v bm ng thi cc
phm 5 v 7 s ging nh khi ta gy dy ny v ch bm phm
s 7.
iu kh khn nht khi biu din mt bi nhc l iu khin
cng tc phi gy dy no v bm phm no, cn iu
khin trng (thi gian pht mt nt) khng phi l qu kh
v phng din k thut. Chng trnh iu khin cng
ca Steve c ti u ha theo hng gim s ln chuyn tay
bm phm xung cn t nht.
Xt bn nhc c n nt, nt th i c cho bi cp gi tr
nguyn (si, fi), trong si dy cn gy v fi phm cn
bm (1 si 6, 1 fi p, i = 1 n). Bn nhc cn chi
theo trnh t thc hin ln lt t nt 1 n nt n. V d, vi n
= 5, p = 15 v bn nhc l (2, 8), (2, 10), (2, 12), (2, 10) v
(2,5), thao tc bm phm ca r bt l bm phm 8, bm phm
10, bm phm 12, nh phm 12, nh phm 10, nh phm 8 v
bm phm 5, tt c l 7 thao tc, thc hin trn dy s 2.
Yu cu: Cho n, p v cc si, fi (1 n 5105, 2 p 3105, i = 1 n). Hy xc nh s
cn thc hin.
V d:
5
2
2
2
2
2
GUITAR.INP
15
8
10
12
10
5
GUITAR.OUT
7
e11 Cr7 3
31/64
C++
#include <fstream>
using namespace std;
int main()
{long n,p,s,f,k,r;
ifstream fi ("GUITAR.INP");
ofstream fo ("GUITAR.OUT");
fi>>n>>p;
{long a[6][p];
for (int i=0; i<6;i++) a[i][0]=0;
r=0; k=0;
for (int i=1; i<=n;i++)
{ fi>>s>>f; --s;
while (a[s][k]>f)
{ --k; ++r;}
if (a[s][k]<f) { ++k; a[s][k]=f; ++r;}
}
}
fo<<r;
fi.close(); fo.close();
}
PASCAL
Program GUITAR;
Const tfi='GUITAR.INP';
tfo='GUITAR.OUT';
var m,n,p,k,s,f,i,r:longint;
a:array[1..6,0..300000] of integer;
fi,fo:text;
BEGIN
assign(fi,tfi); reset(fi);
readln(fi,n,p);
r:=0; k:=0;
for i:=1 to 6 do a[i,0]:=0;
for i :=1 to n do
begin
readln(fi,s,f);
while a[s,k]>f do begin dec(k); inc(r) end;
if a[s,k]<f then begin inc(k);a[s,k]:=f; inc(r)
end
end;
assign(fo,tfo); rewrite(fo);
writeln(fo,r);
close(fi); close(fo)
END.
32/64
MU VNG
YELLOW.OUT
3
1
7
1 2 0 1 0
3
3 1 0 0
33/64
C++
#include <fstream>
using namespace std;
ifstream fi ("yellow.inp");
ofstream fo ("yellow.out");
int r;
void xly()
{int m,n,r,t0,t1,d,id,m2,p,q;
fi>>m>>n;
{int a[3*m-2];
t0=0;t1=0;m2=2*m-2;
for (int i=m-1;i<2*m-1;i++)
{fi>>a[i];
switch (a[i])
{case 0: t0++; break;
case 1: t1++; break;
case 2:case 3: d=a[i]; id=i; a[i]=0;break;
}
}
for (int i=0;i<m-1;i++) a[i]=a[2*m-2-i];
for (int i=0;i<m-1;i++) a[i+2*m-1]=a[2*m-3-i];
t1*=2; t-=(a[m-1]+a[2*m-2]);
p=n/m2; q=n%m2;
r=t1*p;
if (d==2) for(int i=1; i<=q;i++) r+=a[i+id];
else for (int i=1;i<=q;i++) r+=a[id-i];
fo<<r<<endl;
}
}
int main()
{int t;
fi>>t;
for (int i =1;i<=t;i++) xly();
fi.close(); fo.close();
}
34/64
PASCAL
Program YELLOW;
Const tfi='YELLOW.INP';
tfo='YELLOW.OUT';
var t,m,n,p,q,t1,m2,i,r,d,id:longint;
a:array[-99..199] of integer;
fi,fo:text;
Procedure xly;
var i:integer;
Begin
readln(fi,m,n); t1:=0; m2:=2*m-2;
for i:=1 to m do
begin
read(fi,a[i]);
case a[i] of
1:inc(t1);
2,3:begin id:=i;d:=a[i];a[i]:=0 end
end
end;
for i:=2 to m do a[2-i]:=a[i];
for i:=2 to m do a[m+i-1]:= a[m-i+1];
t1:=t1*2-a[1]-a[m];
p:=n div m2; q:=n mod m2;
r:=t1*p;
if d=2 then for i:=id to id+q do r:=r+a[i]
else for i:=id downto id-q do r:=r+a[i];
writeln(fo,r)
End;
35/64
TI VI
Ngi ta tin hnh iu tra thng k tnh hnh xem ti vi trong dn chng. Mi ngi trong s
n ngi c hi yu cu cho bit thi im ngi ta bt u xem ti vi v thi im m ht
cui giy ngi ta ri khi ti vi. Cu tr li c ghi li di dng:
Thi im bt u
Thi im cui
HH:MM:SS HH:MM:SS
Trong 0 HH 23, 0 MM, SS 59. Hai thi im u v cui c th khng cng mt
ngy, v d, mt ngi c th bt u xem t 23:45:30 cho n 01:15:00 sng ngy hm sau.
D nhin, khng ai xem ti vi lin tc c mt ngy.
Sau khi c d liu, cc nh thng k bt u phn tch.
ph bin ca mt giy no c tnh bng tng s ngi c xem ti vi giy .
ph bin ca mt khong thi gian c tnh bng tng ph bin ca cc giy trong
khong chia cho tng s giy trong khong.
Hy tnh ph bin ca mi khong trong s q khong cho trc m cc nh thng k quan
tm.
D liu: Vo t file vn bn TV.INP:
TV.OUT
2.0000000000
1.0000000000
2.0000000000
1.2000000000
1.4000000000
e25 Cr11 2
C++
36/64
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
long n,q,b[86400],tb,te;
string s;
ifstream fi ("TV.INP");
ofstream fo ("TV.OUT");
void nhapdl()
{ s.reserve(19);
getline(fi,s);
tb=(((s[0]-48)*10+s[1]-48)*60+(s[3]-48)*10+s[4]-48)*60+(s[6]48)*10+s[7]-48;
te=(((s[11]-48)*10+s[12]-48)*60+(s[14]-48)*10+s[15]-48)*60+(s[17]48)*10+s[18]-48;
}
void gn_be()
{b[tb]++;
if (te==86399) return;
if (te<tb)b[0]++;
b[++te]--;
}
void tichluy()
{long t;
t=b[0];
for (int i=1;i<86400;i++)
{ t+=b[i];
b[i]=(b[i-1]+t);
}
}
float xd_r()
{ float x;
x=b[te];
if (tb==0) return(x);
if (tb<=te) return(x-b[tb-1]);
return(x+b[86399]-b[tb-1]);
}
void xd_kq()
{float r,d;
if (tb>te) d=86400-tb+te+1; else d=te-tb+1;
r= xd_r()/d;
fo<<setprecision(8)<<r<<endl;
}
int main()
{memset(b,0,sizeof(b));
fi>>n; getline(fi,s);
for (int i=1;i<=n;i++)
{nhapdl();
gn_be();
}
tichluy();
fi>>q; getline(fi,s);
37/64
NG GP KHC
V d:
POLYGON.INP
5
0 0
-2 2
-2 -1
2 -2
2 0
POLYGON.OUT
12.5
38/64
Gii thut
a gic c th chia thnh cc hnh thang c cnh song song vi trc Oy, chiu cao bng 1 (c
th c hnh thang suy bin thnh tam gic),
Cc on thng ca li, song song vi trc Oy v nm gn trong a gic, tham gia vo qu
trnh tnh din tch 2 ln: mt ln l
y di v ln khc - l y trn.
Cc on thng ca li, song song
vi trc Oy v nm ng bin
tham gia mt ln.
C nhn xt tng t nh vy vi cc
on ca li, song song vi trc Ox
v nm gn trong a gic.
Gi S l din tch a gic, lv tng
di cc on bin song song vi
Oy, lh tng di cc on bin song song vi Ox, r l kt qu cn tm,ta c:
2r = s
trong s =
lv
lh
+s ,
2
2
1 n
| ( xi yi 1 xi 1 yi ) | , xi+1 = x1, yi+1 = y1.
2 i 1
39/64
C++
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
int n, lh = 0, lv = 0, S = 0, x[100100],
y[100100];
ifstream fi ("POLYGON.INP");
ofstream fo ("POLYGON.OUT");
void input(){
fi>>n;
for (int i = 1; i <= n; i++) fi>>x[i]>>y[i];
x[n+1] = x[1];
y[n+1] = y[1];
}
void cal_area(){
for (int i = 1; i <= n; i++)
S += x[i]*y[i+1] - y[i]*x[i+1];
S = abs(S);
}
void cal_lh_lv(){
for (int i = 1; i <= n; i++){
if (x[i] == x[i+1])
lv += abs(y[i+1] - y[i]);
if (y[i] == y[i+1])
lh += abs(x[i+1] - x[i]);
}
}
void output(){
fo << double(2*abs(S) - lv - lh) / double(2);
}
int main(){
input();
cal_area();
cal_lh_lv();
output();
return 0;
}
40/64
C++
Vo ra theo quy cch
#include <iostream>
#include <cmath>
using namespace std;
int n, lh = 0, lv = 0, S = 0, x[100100], y[100100];
void input(){
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%d %d", &x[i], &y[i]);
x[n+1] = x[1];
y[n+1] = y[1];
}
void cal_area(){
for (int i = 1; i <= n; i++)
S += x[i]*y[i+1] - y[i]*x[i+1];
S = abs(S);
}
void cal_lh_lv(){
for (int i = 1; i <= n; i++){
if (x[i] == x[i+1])
lv += abs(y[i+1] - y[i]);
if (y[i] == y[i+1])
lh += abs(x[i+1] - x[i]);
}
}
void output(){
cout << double(2*abs(S) - lv - lh) / double(2);
}
int main(){
freopen("POLYGON.INP", "r", stdin);
freopen("POLYGON.OUT", "w" ,stdout);
input();
cal_area();
cal_lh_lv();
output();
return 0;
}
41/64
QUAY CP
Th sinh tham d Thi lp trnh th gii np n files li gii f1, f2, . . ., fn. Trc khi chm
Ban Gim kho quyt nh kim tra xem c trng hp chp bi ca nhau khng. Ngi ta
ly tng cp 2 files, so snh ni dung xem chng c qu ging nhau hay khng.
Nhng s lng files l rt ln v khng thi gian so snh tt c cc cp files. V vy,
ngi ta ch so snh cc file c kch thc gn nhau.
C th l, nu vi 2 files, kch thc file b nh hn 90% kch thc file ln th s khng s
snh chng. Ni mt cch khc, gi di l kch thc file fi, ngi ta s so snh 2 files fi v
fj nu di dj, di 0.9dj, i j.
Hy xc nh s lng cp files cn so snh.
D liu: Vo t file vn bn PLAGIARY.INP:
Dng u tin cha s nguyn n (1 n 105),
Dng th 2 cha n s nguyn d1, d2, . . ., dn (1 di 108 , i = 1 n).
Kt qu: a ra file vn bn PLAGIARY.OUT mt s nguyn s lng cp files cn so
snh.
V d:
PLAGIARY.INP
5
1 1 1 1 1
PLAGIARY.OUT
10
Gii thut
42/64
C++
#include <iostream>
#include <ctime>
using namespace std;
int n, up, res = 0, a[100100];
int main(){
freopen("PLAGIARY.INP", "r", stdin);
freopen("PLAGIARY.OUT", "w", stdout);
scanf("%d", &n);
for (int i = 0; i < n; i++)
scanf("%d", &a[i]);
sort(a, a+n);
a[n] = INT_MAX;
for (int i = 0; i < n; i++){
int j = min(n - 1, up);
while (j < n && 9*a[j] <= 10*a[i])
j++;
up = j;
res += j-i-1;
}
printf("%d", res);
return 0;
}
43/64
C T
DUMB_BELL.OUT
4
2 2 1 20
14
44/64
C++
#include <iostream>
using namespace std;
const int b[6] = {1, 2, 5, 10, 15, 20};
int n, w, p, F, res = INT_MAX, s[11], L[201];
int main(){
freopen("DUMB_BELL.INP", "r", stdin);
freopen("DUMB_BELL.OUT", "w", stdout);
scanf("%d", &n);
for (int i = 1; i <= n; i++){
scanf("%d", &p);
s[i] = s[i-1] + p;
}
scanf("%d", &w);
L[1] = L[2] = L[5] = L[10] = L[15] = L[20] = 1;
for (int i = 1; i <= 200; i++){
if (L[i] == 1)
continue;
L[i] = INT_MAX;
for (int j = 0; j < 6 && b[j] < i; j++)
L[i] = min(L[i], L[i - b[j]] + 1);
}
for (int i = 0; i <= n; i++){
F = n-i;
if (s[i] > w)
F = INT_MAX;
else if (s[i] < w)
F += L[w - s[i]];
res = min(res, F);
}
printf("%d", res);
return 0;
}
45/64
TA VNG
Xt bn s hnh vung ta gc di tri l (0, 0), gc trn phi l (2n, 2n) vi 0 < n
1000. Vi im P ta (x,y) (0 < x, y < 2n ta c th ch ra
ng dn ti vng nh nht cha P nh mt im trong
(khng nm trn bin). V d, vi P c ta (5, 3) v n = 3,
tq
tr
vng nh nht cha P c ng dn l tsq.
Yu cu: Cho n, x, y. Hy xc nh ng n ti vng cha
im (x, y).
D liu: Vo t file vn bn REG_COOR.INP gm mt dng
cha 3 s nguyn n, x v y.
Kt qu: a ra file vn bn REG_COOR.OUT xu ng
dn.
V d:
REG_COOR.INP
3 5 3
REG_COOR.OUT
tsq
46/64
tsq
tt
C++
#include <iostream>
#include <string>
using namespace std;
int n;
string x, y, res;
string div(string a, int b){
int hold = 0, s = 0;
string ans = "";
for (int i = 0; i < a.size(); i++){
hold = 10 * hold + (a[i] - '0');
s = hold / b;
hold %= b;
ans += ('0' + s);
}
while (ans[0] == '0')
ans.erase(0, 1);
return ans;
}
string binary_representation(string a){
string ans = "";
while (a.size()){
ans += ('0' + (a[a.size()-1] - '0') %
2);
a = div(a, 2);
}
for (int i = 0; i < (ans.size() >> 1); i++)
swap(ans[i], ans[ans.size()-i-1]);
while (ans.size() < n)
ans.insert(0, "0");
return ans;
}
int main(){
freopen("REG_COOR.INP", "r", stdin);
freopen("REG_COOR.OUT", "w", stdout);
cin >> n >> x >> y;
x = binary_representation(x);
y = binary_representation(y);
res = "";
for (int i = 0; i < n-1; i++){
if (x[i] == '0' && y[i] == '0')
res += 't';
else if (x[i] == '0' && y[i] == '1')
res += 'q';
else if (x[i] == '1' && y[i] == '0')
res += 's';
else if (x[i] == '1' && y[i] == '1')
res += 'r';
if (min(x.rfind("1"), y.rfind("1")) < i-1)
break;
}
cout << "t" << res;
return 0;
}
47/64
B TC
CLAN.OUT
4
C++
#include <fstream>
#include <set>
using namespace std;
int n, ans, res = 0;
set <int> a;
multiset <int> s;
int main(){
ifstream finp ("CLAN.INP");
ofstream fout ("CLAN.OUT");
finp >> n;
while (n--){
finp >> ans;
a.insert(ans);
s.insert(ans);
}
for (set <int>::iterator i = a.begin(); i !=
a.end(); i++)
res += s.count(*i) / (*i);
fout << res;
}
48/64
LY NHIM
Steve pht hin ra mt li trong h iu hnh ni ting ang c s dng ph bin. Li dng
li ny ta c th truy nhp v iu khin my tnh bt k ang kt ni v s dng h iu
hnh ny. Khi kch hot c ch to li, nu mt my tnh ang c kt ni vi ng 8 my
tnh khc ta c mt con ng ring tuy nhp vo n trong ng 8 giy.
Steve vit mt chng trnh virus n gin khai thc li ny. Virus hot ng theo kiu tun
t, ngha l my tnh b nhim virus mi thi im ch tn cng sang ng mt my khc
m thi.
Yu cu: Ban u Steve c 8 my b nhim virus. Hy xc nh sau bao lu virus s lan
c ti tt c n my tnh ca Steve.
D liu: Vo t file vn bn INFECT.INP gm mt dng cha s nguyn n (8 n 108).
Kt qu: a ra file vn bn INFECT.OUT mt s nguyn thi gian cn thit cc my
u b nhim virus.
V d2:
INFECT. INP
INFECT. OUT
20
80
C++
#include <fstream>
using namespace std;
int n, cur = 8, res = 0;
int main(){
ifstream finp ("INFECT.INP");
ofstream fout ("INFECT.OUT");
finp >> n;
while (cur < n){
cur += (cur / 8);
res += 8;
}
fout << res;
return 0;
}
49/64
MT KHU
ng vi kt qu test th i.
V d:
PASSW.INP
PASSW.OUT
4
AliKam123test
AbCdEfG
88syadneerG
Windows7released21october2009
50/64
6
0
10
8
C++
#include <fstream>
#include <string>
using namespace std;
int n;
int main(){
ifstream finp ("PASSW.INP");
ofstream fout ("PASSW.OUT");
finp >> n;
while (n--){
string s;
finp >> s;
if (s.size() < 6){
fout << "0" << endl;
continue;
}
int upper = -1 , lower = -1, num = -1, res = INT_MAX;
for (int i = 0; i <= s.size(); i++){
if ('A' <= s[i] && s[i] <= 'Z')
upper = i;
else if ('a' <= s[i] && s[i] <= 'z')
lower = i;
else if ('0' <= s[i] && s[i] <= '9')
num = i;
if (upper >= 0 && lower >= 0 && num >=0)
res = min(res, i - min(min(upper, lower), num) + 1);
}
if (upper == -1 || lower == -1 || num == -1)
fout << "0" << endl;
else
fout << max(res, 6) << endl;
}
return 0;
}
51/64
TR GIP NG NHP
aaBBccDD
aaBBccDD9
aaBBCCDD
AAbbCCdd
aBcD
AbCd
F1LOGIN.OUT
Login successful
Wrong password.
Wrong password.
Wrong password. Please, check your caps lock key.
Wrong password. Please, check your num lock key.
Wrong password. Please, check your caps lock and num lock key.
52/64
C++
#include <iostream>
#include <string>
using namespace std;
int n;
int main(){
freopen("F1LOGIN.INP", "r", stdin);
freopen("F1LOGIN.OUT", "w", stdout);
cin >> n;
while (n--){
string s,t;
cin >> s >> t;
if (s.compare(t) == 0){
cout << "Login successful. " << endl;
continue;
}
cout << "Wrong password. ";
string ss = s, tt = t;
bool stop;
while (true){
stop = true;
for (int i = 0; i < ss.size(); i++)
if ('0' <= ss[i] && ss[i] <= '9'){
ss.erase(i, 1);
stop = false;
}
if (stop)
break;
}
for (int i = 0; i < tt.size(); i++)
if ('a' <= tt[i] && tt[i] <= 'z')
tt[i] -= ('a' - 'A');
else if ('A' <= tt[i] && tt[i] <= 'Z')
tt[i] += ('a' - 'A');
bool cap = ((s.compare(tt) == 0) || (ss.compare(tt) == 0)),
num = (ss.size() < s.size() && ((ss.compare(t) == 0) ||
(ss.compare(tt) == 0)));
if (cap && num)
cout << "Please, check your caps lock and num lock key."
<< endl;
else if (cap && !num)
cout << "Please, check your caps lock key." << endl;
else if (!cap && num)
cout << "Please, check your num lock key." << endl;
else
cout << endl;
}
return 0;
}
53/64
XA 7
V d:
DROP7.INP
6
2 2
3 2
6 2
4 3
1 1
6 7
11
5 3
5 3
2 3
2 3
2 3
3 3
4 3
5 3
6 3
7 3
1 3
0
DROP7.OUT
#######
#######
#######
#######
#######
#######
#64###6
Game Over!
55/64
C++
#include <iostream>
using namespace std;
int n, a[10][10], top[10];
bool find(){
for (int col = 1; col <= 7; col++)
for (int i = 1; i <= 7; i++)
if (top[col] > 0 && a[col][i] == top[col])
return true;
for (int row = 1; row <= 7; row++){
int i = 1;
while (i <= 7){
while (i <= 7 && a[i][row] == 0)
i++;
int j = i;
while (a[j+1][row])
j++;
for (int k = i; k <= j; k++)
if (j > i && a[k][row] == j-i+1)
return true;
i = j+1;
}
}
return false;
}
void del_column(int col){
if (top[col] == 0)
return;
int num_del = 0;
for (int i = 1; i <= 7; i++)
if (a[col][i] == top[col]){
a[col][i] = 0;
num_del++;
}
top[col] -= num_del;
}
void del_row(int row){
int i = 1;
while (i <= 7){
while (i <= 7 && a[i][row] == 0)
i++;
int j = i;
while (a[j+1][row])
j++;
for (int k = i; k <= j; k++)
if (j > i && a[k][row] == j-i+1){
a[k][row] = 0;
top[k]--;
}
i = j+1;
}
}
56/64
57/64
BNG GIY
STRIP.OUT
9
58/64
C++
#include <fstream>
#include <string>
using namespace std;
int n, k, kk, p = 1, q;
string s;
ifstream fi ("STRIP.INP");
ofstream fo ("STRIP.OUT");
inline void input(){fi >> k >> n;}
inline void initial(){k--; n--; s = "";}
inline void generate_s(){
while (k>=0){
if (((n >> k) & 1) == 0)
s += 'D';
else{
s += 'U';
n = ~n;
}
k--;
}
}
inline void process(){
for (int i = 0; i < s.size(); i++){
if (s[i] == 'U'){
int temp = p;
p = p+(q << 1)+1;
q = temp-1;
}
else
q += p+q;
}
}
inline void output(){
fo << p;
}
int main(){
input();
initial();
generate_s();
process();
output();
return 0;
}
59/64
NG VIN
CONTOUR.OUT
6 1
5 -1
5 5
2 5
2 2
-2 2
-2 -1
1 6
1 3
3 -3
-3 -2
6 -2
6 6
60/64
C++
#include <iostream>
using namespace std;
int n, d, first = 100009, x[100010], y[100010], res_x[100010], res_y[100010];
bool clockwise;
void input(){
cin >> n >> d;
for (int i = 1; i <= n; i++)
cin >> x[i] >> y[i];
x[n+1] = x[1];
y[n+1] = y[1];
x[0] = x[n];
y[0] = y[n];
}
void define_direction(){
int S = 0;
for (int i = 1; i <= n; i++)
S += x[i]*y[i+1] - y[i]*x[i+1];
clockwise = (S < 0);
}
void process(){
y[first] = -INT_MAX;
x[first] = INT_MAX;
for (int i = 1; i <= n; i++){
if (x[i] == x[i-1]){
if (y[i] > y[i-1])
res_y[i] = y[i] + d;
else
res_y[i] = y[i] - d;
if (x[i] < x[i+1])
res_x[i] = x[i] - d;
else
res_x[i] = x[i] + d;
}
else{// x[i] == x[i+1]
if (y[i] > y[i+1])
res_y[i] = y[i] + d;
else
res_y[i] = y[i] - d;
if (x[i] < x[i-1])
res_x[i] = x[i] - d;
else
res_x[i] = x[i] + d;
}
if (y[i] > y[first] || (y[i] == y[first] && x[i] < x[first]))
first = i;
}
}
void output(){
if (!clockwise){
for (int i = first; i <= n; i++)
cout << res_x[i] << " " << res_y[i] << endl;
for (int i = 1; i < first; i++)
cout << res_x[i] << " " << res_y[i] << endl;
}
else{
for (int i = first; i >= 1; i--)
cout << res_x[i] << " " << res_y[i] << endl;
for (int i = n; i > first; i--)
cout << res_x[i] << " " << res_y[i] << endl;
}
}
int main(){
freopen("CONTOUR.INP", "r", stdin);
freopen("CONTOUR.OUT", "w", stdout);
input();
define_direction();
process();
output();
return 0;
}
61/64
MUA KEM
V d:
5
1
3
1
CREAM.INP
3
2
4
3
CREAM.OUT
3
C++
#include <iostream>
using namespace std;
int n, m, u, v, res = 0;
bool contrast[201][201];
int main(){
freopen("CREAM.INP", "r", stdin);
freopen("CREAM.OUT", "w", stdout);
scanf("%d %d", &n, &m);
while (m--){
scanf("%d %d", &u, &v);
contrast[u][v] = contrast[v][u] = true;
}
for (int i = 1; i <= n-2; i++)
for (int j = i+1; j <= n-1; j++)
for (int k = j+1; k <= n; k++)
res += !(contrast[i][j] || contrast[i][k] ||
contrast[j][k]);
printf("%d", res);
return 0;
}
62/64
C++
#include <iostream>
#include <fstream>
using namespace std;
int n, m, u, v, res = 0;
int c[201][201];
int main()
{
ifstream fi ("CREAM.INP");
ofstream fo ("CREAM.OUT");
fi>>n>>m;
{int c[n+1][n+1],t;
memset(c,0,sizeof(c));
for (int i=1;i<=m;i++)
{fi>>u>>v;
c[u][v]=c[v][u]=1;
}
for (int i = 1; i <= n-2; i++)
for (int j = i+1; j <= n-1; j++)
for (int k = j+1; k <= n; k++)
{t=c[i][j]+c[i][k]+c[j][k];
if (t==0) res++;
}
}
fo<<res;
fo.close(); fi.close();
}
HNH KHI
Hnh khi
bc 0
Hnh khi
bc 1
Hnh khi
bc 2
hnh khi bc i ngi ta to ra hnh khi bc i+1 bng cch dn cc hnh lp phng vo cc
mt trng ca n.
Yu cu: Cho s nguyn n ( 0 n 100 000). Hy xc nh s khi lp phng to ra hnh
khi bc n.
63/64
7
25
C++
#include <fstream>
using namespace std;
ifstream fi ("CUBES.INP");
ofstream fo ("CUBES.OUT");
int n,ho,hn,co,cn,r;
int main()
{while (!fi.eof())
{hn=1;cn=1;
fi>>n;
for (int i = 1; i<n;i++)
{ho=hn;co=cn;
cn=co+i*4;
hn=ho+cn;
}
r=cn+2*ho;
fo<<r<<endl;
}
fo.close(); fi.close();
}
64/64