Chủ Nhật, 12 tháng 8, 2012

Lời giải đề thi học sinh giỏi tỉnh Khánh Hòa năm 2010

Bài 1:
program HSGKhH_Bai1_2010;
type
    arr = Array[1..100000] of double;
var
    N:longint;
    a:arr;
    i:integer;
    isTang,isGiam:boolean;
    x:double;
begin
    write('N=');
    readln(N);
    isTang:=true;
    isGiam:=true;
    for i:=1 to N do
        begin
            write('a[',i,']=');
            readln(a[i]);
            if(i=1) then
                begin
                    x := a[i];
                    continue;
                end;
            if(isTang) then
                begin
                    if(x>a[i]) then
                        isTang:=false
                    else
                        x:=a[i];
                end;

            if(isGiam) then
                begin
                    if(x<a[i]) then
                        isGiam:=false
                    else
                        x:=a[i];
                end;
        end;
    if(isTang) then
        write('day tang')
    else
        if(isGiam) then
            write('day giam')
        else
            write('khong tang khong giam');
    readln;
end.

Bài 2:

program HSGKhH_Bai2_2010;
type
    arr = array[1..100000] of double;
var
    N,K,dem,i:integer;
    x:double;
    isok:boolean;
    a:arr;
begin
    dem:=0;
    isok := false;
    write('N=');readln(N);
    for i:=1 to N do
        begin
            write('a[',i,']=');
            readln(a[i]);
        end;
    write('K=');readln(K);
    for i:=1 to N do
        begin
            if(a[i]>0) then inc(dem);
            if(a[i]<0) then
                begin
                    if(dem=K) then
                        begin
                            isok := true;
                            break;
                        end;
                    dem :=0;
                end;
        end;
    if isok then
        write('co')
    else
        write('khong');
    readln;
end.

Bài 3: nhận xét rất đơn giản,

  • dòng và cột đều lẻ điền số 1, dòng và cột đều lẻ điền số -1, còn lại là số 0. 
  • nếu là ma trận vuông thì tổng giá trị luôn là 0.
solution (free pascal):
program HSGKhH_Bai3_2010;
var
    N,i,j:longint;
    a:array[1..201,1..201]of longint;
begin
    write('N='); readln(N);
    fillchar(a,sizeof(a),0);
    for i:=1 to N do
    for j:=1 to N do
        if(odd(i) and odd(j)) then
            a[i,j]:=1
        else if(not odd(i) and not odd(j)) then
            a[i,j]:=-1;
    for i:=1 to N do
        begin
            for j:=1 to N do write(a[i,j],' ');
            writeln;
        end;

    readln;
end.

Bài 4:
Solution bằng ngôn ngữ C++ cài trên IDE DevC:



#include<iostream>
#include<string>
#include<algorithm>

using namespace std;

string s;
int f[256];
int trace[256];
bool IsPalin(int jj,int ii);
int main()
{
freopen("TEST.INP","r",stdin);
freopen("TEST.OUT","w",stdout);
cin >> s;
//swap
char c;
for(int i=0,j=s.size()-1; i<j;)
{
c=s[i];
s[i]=s[j];
s[j]=c;
i++,j--;
}
//cout << s;
f[0]=1;
for(int i=1; i<s.size(); i++)
{
f[i]=i+1;
trace[i]=i;
for(int j=0;j<=i;j++)
if(IsPalin(j,i))
{
if(j==0)
{
f[i]=1;
trace[i]=0;
continue;
}
if(f[i]>f[j-1]+1)
{
f[i] = f[j-1]+1;
trace[i]=j;
}
}
}
cout << f[s.size()-1] << endl;
for(int i=s.size()-1; i>=0;)
{
for(int j=trace[i];j<=i;j++)
cout << s[j];
cout << endl;
i = trace[i]-1;
}
return 0;
}

bool IsPalin(int jj,int ii)
{
int jjj=jj,iii=ii;
while(jjj<iii)
{
if(s[jjj]!=s[iii]) return false;
jjj++;
iii--;
}
return true;
}

1 nhận xét:

  1. trong lời giải trên vừa kết hợp việc nhập liệu vừa kiểm tra có phải là dãy tăng không, vừa kiểm tra có là dãy giảm không, nếu không thỏa mãn hai trường hợp kia thì là trường hợp còn lại.

    Trả lờiXóa