NOIP2012

DAY1

Vigenère 密码

模拟

可以先把密钥全部转化为大写或小写

 1 var
 2   i,asc:longint;
 3   k,c:ansistring;
 4 begin
 5   readln(k);
 6   for i:=1 to length(k) do k[i]:=upcase(k[i]);
 7   readln(c);
 8   while length(k)<length(c) do k:=k+k;
 9   for i:=1 to length(c) do
10   begin
11     asc:=ord(c[i])-(ord(k[i])-ord('A'));
12     if (ord(c[i])>=65) and (ord(c[i])<=90) then
13       if asc<65 then asc:=ord('Z')-(65-asc)+1;
14     if (ord(c[i])>=97) and (ord(c[i])<=122) then
15       if asc<97 then asc:=ord('z')-(97-asc)+1;
16     write(chr(asc));
17   end;
18 end.
我去年的程序
 1 var
 2   k,m,c:ansistring;
 3   i,l,s:longint;
 4   dax1,xix1,dax2,xix2:boolean;
 5 procedure pd(x,y:char);
 6 begin
 7   dax1:=false; dax2:=false;
 8   xix1:=false; xix2:=false;
 9   if (ord(x)>=ord('a'))and(ord(x)<=ord('z')) then xix1:=true
10    else if (ord(x)>=ord('A'))and(ord(x)<=ord('Z')) then dax1:=true;
11   if (ord(y)>=ord('a'))and(ord(y)<=ord('z')) then xix2:=true
12    else if (ord(y)>=ord('A'))and(ord(y)<=ord('Z')) then dax2:=true;
13 end;
14 procedure doit1(var k,c:char);
15 var cha:longint;
16 begin
17   cha:=ord(c)-(ord(k)-ord('a'));
18   if cha<ord('a') then
19    cha:=ord('z')-((ord('a')-cha))+1;
20   c:=chr(cha);
21 end;
22 procedure doit2(var k,c:char);
23 var cha:longint;
24 begin
25   cha:=ord(c)-(ord(k)-ord('A'));
26   if cha<ord('A') then
27    cha:=ord('Z')-((ord('A')-cha))+1;
28   c:=chr(cha);
29 end;
30 procedure doit3(var k,c:char);
31 var cha:longint;
32 begin
33   cha:=ord(c)-(ord(k)-ord('a'));
34   if cha<ord('A') then
35    cha:=ord('Z')-((ord('A')-cha))+1;
36   c:=chr(cha);
37 end;
38 procedure doit4(var k,c:char);
39 var cha:longint;
40 begin
41   cha:=ord(c)-(ord(k)-ord('A'));
42   if cha<ord('a') then
43    cha:=ord('z')-((ord('a')-cha))+1;
44   c:=chr(cha);
45 end;
46 begin
47 assign(input,'vigenere.in');
48 reset(input);
49 assign(output,'vigenere.out');
50 rewrite(output);
51   readln(k);
52   readln(c);
53   while l<length(c) do
54    begin
55      k:=k+k;
56      l:=length(k);
57    end;
58   for i:=1 to length(c) do
59   begin
60     pd(k[i],c[i]);
61     if (xix1)and(xix2) then doit1(k[i],c[i]);
62     if (dax1)and(dax2) then doit2(k[i],c[i]);
63     if (xix1)and(dax2) then doit3(k[i],c[i]);
64     if (dax1)and(xix2) then doit4(k[i],c[i]);
65   end;
66   writeln(c);
67 close(input);
68 close(output);
69 end.
我今年的程序
 1 注意大小写
 2 var
 3 i,l,t:longint;
 4 a:Array[0..1000] of longint;
 5 s:string;
 6 ch:char;
 7 begin
 8   readln(s);
 9   l:=length(s);
10   for i:=1 to l do
11     if ord(s[i])>=97 then a[i]:=ord(s[i])-96
12     else a[i]:=ord(s[i])-64;
13   a[0]:=a[l];
14   read(ch);
15   i:=1;
16   t:=ord(ch);
17   while (t>64)and(t<124) do
18    begin
19     if t<97 then
20      begin
21       t:=t-64;
22       t:=(t-a[i mod l]+27) mod 26;
23       if t=0 then t:=26;
24       t:=t+64;
25       write(chr(t));
26      end
27     else
28      begin
29       t:=t-96;
30       t:=(t-a[i mod l]+27) mod 26;
31       if t=0 then t:=26;
32       t:=t+96;
33       write(chr(t));
34      end;
35     read(ch);
36     inc(i);
37     t:=ord(ch);
38    end;
39 end.
老师给的题解

智商在流失。。

国王游戏

快排+高精乘+高精除

[转]

正解= 贪心+高精.

贪心证明 :

设存在 相邻大臣A(l1,r1),B(l2,r2),A,B前左手乘积为Sum :

   当A在B前时: 

        则Ans=max(Sum/r1,Sum*l1/r2) ;

  当B在A前时: 

        则Ans=max(Sum/r2,Sum*l2/r1) ;

  显然 Sum*l2/r1>Sum/r1  ; 

         Sum*l1/r2>Sum/r2 ;

  所以当 Sum*l2/r1>Sum*l1/r2

        即   l2*r2>l1*r1 时  A应在B前

  同理

        即   l2*r2<l1*r1 时  B应在A前

  所以按 l*r 从小到大排序在进行模拟即为正解.

 1 type
 2   arr=array[1..10000] of longint;
 3 var
 4   ans:arr;
 5   n,i,j,a1,b1,la:longint;
 6   a,b,c:array[1..10000] of longint;
 7 procedure sort(l,r:longint);
 8 var
 9   i,j,tmp,mid:longint;
10 begin
11   i:=l;
12   j:=r;
13   mid:=c[(i+j)div 2];
14   repeat
15     while c[i]<mid do inc(i);
16     while c[j]>mid do dec(j);
17     if i<=j then
18      begin
19        tmp:=c[i]; c[i]:=c[j]; c[j]:=tmp;
20        tmp:=a[i]; a[i]:=a[j]; a[j]:=tmp;
21        tmp:=b[i]; b[i]:=b[j]; b[j]:=tmp;
22        inc(i);
23        dec(j);
24      end;
25   until i>j;
26   if l<j then sort(l,j);
27   if i<r then sort(i,r);
28 end;
29 procedure mul(var a:arr;c:longint;var la:longint);
30   var i:longint;
31   begin
32     a[1]:=a[1]*c;
33     for i:=2 to la do
34       begin
35         a[i]:=a[i]*c;
36         a[i]:=a[i]+a[i-1] div 10;
37         a[i-1]:=a[i-1] mod 10;
38       end;
39     while a[la]>=10 do
40       begin
41         inc(la);
42         a[la]:=a[la-1] div 10;
43         a[la-1]:=a[la-1] mod 10;
44       end;
45   end;
46 procedure chufa(var d:arr; b:longint; var la:longint);
47 var c,a:arr;
48     x,j,tmp:longint;
49 begin
50   fillchar(c,sizeof(c),0);
51   for i:=la downto 1 do a[la-i+1]:=d[i];
52   //for i:=1 to la do write(a[i]); writeln;
53   x:=0;
54   for i:=1 to la do begin
55     c[i]:=(x*10+a[i]) div b;
56     x:=(x*10+a[i]) mod b; end;
57    j:=1;
58    while (c[j]=0) and (j<la) do inc(j);
59    if (j=la) and (c[la]=0) then begin writeln(1); halt; end;
60   for i:=j to la do
61    write(c[i]);
62 end;
63 begin
64 assign(input,'kinggame.in');
65 reset(input);
66 assign(output,'kinggame.out');
67 rewrite(output);
68   readln(n);
69   readln(a1,b1);
70   for i:=1 to n do
71    begin
72      readln(a[i],b[i]);
73      c[i]:=a[i]*b[i];
74    end;
75   sort(1,n);
76   fillchar(ans,sizeof(ans),0);
77   ans[1]:=1; la:=1;
78   for i:=1 to n-1 do mul(ans,a[i],la);
79   mul(ans,a1,la);
80   //for i:=la downto 1 do write(ans[i]); writeln(b[n]);
81   chufa(ans,b[n],la);
82   //for i:=la downto 1 do write(ans[i]);
83   //for i:=1 to n do writeln(a[i],' ',b[i],' ',c[i]);
84   close(input);
85   close(output);
86 end.
我的程序
 1 可以证明最优序列是按照左手上的数*右手上的数递增排序,所以
 2 1.    按照左手上的数*右手上的数递增排序
 3 2.    前n-1个左手上的数累乘(高精乘)
 4 3.    除以n右手上的数(高精除)
 5 var
 6 i,n,l:longint;
 7 a,b,c:array[0..10000] of longint;
 8 g,g2:array[0..100000] of longint;
 9 procedure gj1;
10 var
11 j:longint;
12 begin
13  for j:=1 to l do g[j]:=g[j]*b[i];
14  for j:=1 to l do
15   begin
16    g[j+1]:=g[j+1]+g[j] div 10;
17    g[j]:=g[j] mod 10;
18   end;
19  inc(l);
20  while g[l]>9 do
21   begin
22    g[l+1]:=g[l+1]+g[l] div 10;
23    g[l]:=g[l] mod 10;
24    inc(l);
25   end;
26  if g[l]=0 then dec(l);
27 end;
28 procedure gj2;
29 var
30 j:longint;
31 begin
32  for j:=l downto 1 do
33   begin
34    g[j-1]:=g[j-1]+(g[j] mod c[n])*10;
35    g[j]:=g[j] div c[n];
36   end;
37  while g[l]=0 do dec(l);
38 end;
39 procedure sort(l,r:longint);
40 var
41 i,j,x,y:longint;
42 begin
43   i:=l;
44   j:=r;
45   x:=a[(l+r) div 2];
46   repeat
47     while a[i]<x do inc(i);
48     while x<a[j] do dec(j);
49     if not (i>j) then
50      begin
51       y:=a[i];
52       a[i]:=a[j];
53       a[j]:=y;
54       y:=b[i];
55       b[i]:=b[j];
56       b[j]:=y;
57       y:=c[i];
58       c[i]:=c[j];
59       c[j]:=y;
60       inc(i);
61       dec(j);
62      end;
63   until (i>j);
64   if l<j then sort(l,j);
65   if i<r then sort(i,r);
66 end;
67 begin
68   readln(n);
69   readln(b[0],c[0]);
70   for i:=1 to n do
71    begin
72     read(b[i],c[i]);
73     a[i]:=b[i]*c[i];
74    end;
75   sort(1,n);
76   l:=1;
77   g[1]:=b[0];
78   for i:=1 to n-1 do gj1;
79   gj2;
80   for i:=l downto 1 do write(g[i]);
81   writeln;
82 end.
老师给的题解

 

posted @ 2016-11-08 19:46  ZJQCation  阅读(189)  评论(0编辑  收藏  举报