Pascal & C++——USACO Section 1.2 题解
来源:互联网 发布:cc攻击防御 php 编辑:程序博客网 时间:2024/05/18 00:32
PS:部分代码为转载
Section 1.2
Milking Cows
Transformations
Name That Number
Palindromic Squares
Dual Palindromes
Three farmers rise at 5 am each morning and head for the barn to milk three cows. The first farmer begins milking his cow at time 300 (measured in seconds after 5 am) and ends at time 1000. The second farmer begins at time 700 and ends at time 1200. The third farmer begins at time 1500 and ends at time 2100. The longest continuous time during which at least one farmer was milking a cow was 900 seconds (from 300 to 1200). The longest time no milking was done, between the beginning and the ending of all milking, was 300 seconds (1500 minus 1200).
Your job is to write a program that will examine a list of beginning and ending times for N (1 <= N <= 5000) farmers milking N cows and compute (in seconds):
- The longest time interval at least one cow was milked.
- The longest time interval (after milking starts) during which no cows were being milked.
PROGRAM NAME: milk2
INPUT FORMAT
Line 1:The single integer, NLines 2..N+1:Two non-negative integers less than 1,000,000, respectively the starting and ending time in seconds after 0500SAMPLE INPUT (file milk2.in)
3300 1000700 12001500 2100
OUTPUT FORMAT
A single line with two integers that represent the longest continuous time of milking and the longest idle time.SAMPLE OUTPUT (file milk2.out)
900 300
{ID: mcdonne1PROG: milk2LANG: PASCAL} program milk2;const maxn=5000;var n:integer;mem:array[1..maxn,1..2]of longint;line:array[0..maxn*2+2]of longint;dis:array[0..maxn*2+2]of longint;used,caled:array[0..maxn*2+2]of boolean;i,j,k:longint;x1,x2:longint;maxbusy,maxidle,tempans:longint; function search(a:longint):longint;var i:integer;begin for i:=1 to 2*n do if a=line[i] then begin search:=i; exit; end;end; procedure qsort(a,b:integer);var l,r,mid,temp:longint;begin if a=b then exit; l:=a; r:=b; mid:=line[(l+r)div 2]; repeat while line[l]<mid do l:=l+1; while line[r]>mid do r:=r-1; if l<=r then begin temp:=line[l]; line[l]:=line[r]; line[r]:=temp; l:=l+1; r:=r-1; end; until l>r; if a<r then qsort(a,r); if l<b then qsort(l,b);end; begin assign(input,'milk2.in'); assign(output,'milk2.out'); reset(input); rewrite(output); readln(n); for i:=1 to n do begin readln(mem[i,1],mem[i,2]); line[i*2-1]:=mem[i,1]; line[i*2]:=mem[i,2]; end; qsort(1,n*2); for i:=1 to 2*n do dis[i]:=line[i]-line[i-1]; for i:=1 to n do begin x1:=search(mem[i,1]); x2:=search(mem[i,2]); for j:=x1+1 to x2 do used[j]:=true; end; for i:=2 to 2*n do if (caled[i]=false) then if (used[i]=true) then begin for j:=i to 2*n do if used[j]=false then begin tempans:=0; for k:=i to j-1 do begin tempans:=tempans+dis[k]; caled[k]:=true; end; if tempans>maxbusy then maxbusy:=tempans; break; end; if j<>2*n then continue else begin tempans:=0; for k:=i to 2*n do begin tempans:=tempans+dis[k]; caled[k]:=true; end; if tempans>maxbusy then maxbusy:=tempans; end; end else begin for j:=i to 2*n do if used[j]=true then begin tempans:=0; for k:=i to j-1 do begin tempans:=tempans+dis[k]; caled[k]:=true; end; if tempans>maxidle then maxidle:=tempans; break; end; if j<>2*n then continue else begin tempans:=0; for k:=i to j-1 do begin tempans:=tempans+dis[k]; caled[k]:=true; end; if tempans>maxidle then maxidle:=tempans; end; end; writeln(maxbusy,' ',maxidle); close(output);end.
A square pattern of size N x N (1 <= N <= 10) black and white square tiles is transformed into another square pattern. Write a program that will recognize the minimum transformation that has been applied to the original pattern given the following list of possible transformations:
- #1: 90 Degree Rotation: The pattern was rotated clockwise 90 degrees.
- #2: 180 Degree Rotation: The pattern was rotated clockwise 180 degrees.
- #3: 270 Degree Rotation: The pattern was rotated clockwise 270 degrees.
- #4: Reflection: The pattern was reflected horizontally (turned into a mirror image of itself by reflecting around a vertical line in the middle of the image).
- #5: Combination: The pattern was reflected horizontally and then subjected to one of the rotations (#1-#3).
- #6: No Change: The original pattern was not changed.
- #7: Invalid Transformation: The new pattern was not obtained by any of the above methods.
In the case that more than one transform could have been used, choose the one with the minimum number above.
PROGRAM NAME: transform
INPUT FORMAT
Line 1:A single integer, NLine 2..N+1:N lines of N characters (each either `@' or `-'); this is the square before transformationLine N+2..2*N+1:N lines of N characters (each either `@' or `-'); this is the square after transformationSAMPLE INPUT (file transform.in)
3@-@---@@-@-@@----@
OUTPUT FORMAT
A single line containing the the number from 1 through 7 (described above) that categorizes the transformation required to change from the `before' representation to the `after' representation.SAMPLE OUTPUT (file transform.out)
1{ID: mcdonne1PROG: transformLANG: PASCAL}program transform;var i,j,n:integer; a,b:array[1..10,1..10] of char; ans:array[1..8] of integer; procedure main; var i,j:integer;begin fillchar(ans,sizeof(ans),0); for i:=1 to n do for j:=1 to n do begin if a[i,j]=b[j,n-i+1] then inc(ans[1]); if a[i,j]=b[n-i+1,n-j+1] then inc(ans[2]); if a[i,j]=b[n-j+1,i] then inc(ans[3]); if a[i,j]=b[i,n-j+1] then inc(ans[4]); if a[i,j]=b[i,j] then inc(ans[6]); if a[i,n-j+1]=b[j,n-i+1] then inc(ans[5]); if a[i,n-j+1]=b[n-i+1,n-j+1] then inc(ans[7]); if a[i,n-j+1]=b[n-j+1,i] then inc(ans[8]); end; for i:=1 to 8 do if ans[i]=n*n then begin if i<7 then writeln(i) else writeln('5'); exit; end; writeln('7');end; begin assign(input,'transform.in'); reset(input); assign(output,'transform.out');rewrite(output); readln(n); for i:=1 to n do begin for j:=1 to n do read(a[i,j]); readln; end; for i:=1 to n do begin for j:=1 to n do read(b[i,j]); readln; end; main; close(input);close(output);end.
Among the large Wisconsin cattle ranchers, it is customary to brand cows with serial numbers to please the Accounting Department. The cow hands don't appreciate the advantage of this filing system, though, and wish to call the members of their herd by a pleasing name rather than saying, "C'mon, #4734, get along."
Help the poor cowhands out by writing a program that will translate the brand serial number of a cow into possible names uniquely associated with that serial number. Since the cow hands all have cellular saddle phones these days, use the standard Touch-Tone(R) telephone keypad mapping to get from numbers to letters (except for "Q" and "Z"):
2: A,B,C 5: J,K,L 8: T,U,V 3: D,E,F 6: M,N,O 9: W,X,Y 4: G,H,I 7: P,R,S
Acceptable names for cattle are provided to you in a file named "dict.txt", which contains a list of fewer than 5,000 acceptable cattle names (all letters capitalized). Take a cow's brand number and report which of all the possible words to which that number maps are in the given dictionary which is supplied as dict.txt in the grading environment (and is sorted into ascending order).
For instance, the brand number 4734 produces all the following names:
GPDG GPDH GPDI GPEG GPEH GPEI GPFG GPFH GPFI GRDG GRDH GRDIGREG GREH GREI GRFG GRFH GRFI GSDG GSDH GSDI GSEG GSEH GSEIGSFG GSFH GSFI HPDG HPDH HPDI HPEG HPEH HPEI HPFG HPFH HPFIHRDG HRDH HRDI HREG HREH HREI HRFG HRFH HRFI HSDG HSDH HSDIHSEG HSEH HSEI HSFG HSFH HSFI IPDG IPDH IPDI IPEG IPEH IPEIIPFG IPFH IPFI IRDG IRDH IRDI IREG IREH IREI IRFG IRFH IRFIISDG ISDH ISDI ISEG ISEH ISEI ISFG ISFH ISFI
As it happens, the only one of these 81 names that is in the list of valid names is "GREG".
Write a program that is given the brand number of a cow and prints all the valid names that can be generated from that brand number or ``NONE'' if there are no valid names. Serial numbers can be as many as a dozen digits long.
PROGRAM NAME: namenum
INPUT FORMAT
A single line with a number from 1 through 12 digits in length.SAMPLE INPUT (file namenum.in)
4734
OUTPUT FORMAT
A list of valid names that can be generated from the input, one per line, in ascending alphabetical order.SAMPLE OUTPUT (file namenum.out)
GREG
/*ID: mcdonne1PROG: namenumLANG: C++*/#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <iostream>#include <algorithm>using namespace std;const int num[26]={2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,0,7,7,8,8,8,9,9,9};const int MAX_N=15;bool none=true;int a[MAX_N];int len=0;void getx(){char c;for(c=getchar();c<'0'||c>'9';c=getchar());for(;c>='0'&&c<='9';c=getchar()) a[++len]=c-'0';}bool check(char c[]){int t=strlen(c+1);if(t!=len) return false;for(int i=1;i<=len;++i) if('a'<=c[i]&&c[i]<='z') c[i]+='A'-'a'; for(int i=1;i<=len;++i) if (num[c[i]-'A']!=a[i]) return false;return true;}void in(){freopen("dict.txt","r",stdin);char t[MAX_N];while(scanf("%s",t+1)!=EOF){ if(check(t)){ none=false; printf("%s\n",t+1);}}}int main(){freopen("namenum.in","r",stdin);freopen("namenum.out","w",stdout);getx();in();if(none) printf("NONE\n");}
Rob Kolstad
Palindromes are numbers that read the same forwards as backwards. The number 12321 is a typical palindrome.
Given a number base B (2 <= B <= 20 base 10), print all the integers N (1 <= N <= 300 base 10) such that the square of N is palindromic when expressed in base B; also print the value of that palindromic square. Use the letters 'A', 'B', and so on to represent the digits 10, 11, and so on.
Print both the number and its square in base B.
PROGRAM NAME: palsquare
INPUT FORMAT
A single line with B, the base (specified in base 10).SAMPLE INPUT (file palsquare.in)
10
OUTPUT FORMAT
Lines with two integers represented in base B. The first integer is the number whose square is palindromic; the second integer is the square itself. NOTE WELL THAT BOTH INTEGERS ARE IN BASE B!SAMPLE OUTPUT (file palsquare.out)
1 12 43 911 12122 48426 676101 10201111 12321121 14641202 40804212 44944264 69696
{ID: mcdonne1PROG: palsquareLANG: PASCAL}var i,n:longint;function tentok(n,m:longint):string; var i:longint; function nts(n:longint):string; begin str(n,nts); end; begin tentok:=''; repeat if (m>10) and (n mod m>9) then tentok:=chr((n mod m)+55)+tentok else tentok:=nts(n mod m)+tentok; n:=n div m; until n=0; end;function ispalind(s:string):boolean; var i:longint; begin ispalind:=true; for i:=1 to length(s) div 2 do if s[i]<>s[length(s)-i+1] then begin ispalind:=false; break; end; end;begin assign(input,'palsquare.in');reset(input); assign(output,'palsquare.out');rewrite(output); readln(n); for i:=1 to 300 do if ispalind(tentok(sqr(i),n)) then writeln(tentok(i,n),' ',tentok(sqr(i),n)); close(input);close(output);end.
Mario Cruz (Colombia) & Hugo Rickeboer (Argentina)
A number that reads the same from right to left as when read from left to right is called a palindrome. The number 12321 is a palindrome; the number 77778 is not. Of course, palindromes have neither leading nor trailing zeroes, so 0220 is not a palindrome.
The number 21 (base 10) is not palindrome in base 10, but the number 21 (base 10) is, in fact, a palindrome in base 2 (10101).
Write a program that reads two numbers (expressed in base 10):
- N (1 <= N <= 15)
- S (0 < S < 10000)
Solutions to this problem do not require manipulating integers larger than the standard 32 bits.
PROGRAM NAME: dualpal
INPUT FORMAT
A single line with space separated integers N and S.
SAMPLE INPUT (file dualpal.in)
3 25
OUTPUT FORMAT
N lines, each with a base 10 number that is palindromic when expressed in at least two of the bases 2..10. The numbers should be listed in order from smallest to largest.SAMPLE OUTPUT (file dualpal.out)
262728
{ID: mcdonne1PROG: dualpalLANG: PASCAL}var n,m,c,i,j:longint;function tentok(n,m:longint):string; var i:longint; function nts(n:longint):string; begin str(n,nts); end; begin tentok:=''; repeat if (m>10) and (n mod m>9) then tentok:=chr((n mod m)+55)+tentok else tentok:=nts(n mod m)+tentok; n:=n div m; until n=0; end;function ispalind(s:string):boolean; var i:longint; begin ispalind:=true; for i:=1 to length(s) div 2 do if s[i]<>s[length(s)-i+1] then begin ispalind:=false; break; end; end;begin assign(input,'dualpal.in');reset(input); assign(output,'dualpal.out');rewrite(output); readln(n,m); j:=0; repeat inc(m); c:=0; for i:=2 to 10 do begin if ispalind(tentok(m,i)) then inc(c); //ispalind=是不是回文;tentok=十进制转K进制 if c>1 then break; end; if c>1 then begin writeln(m); inc(j); end; until j=n; close(input); close(output);end.
- Pascal & C++——USACO Section 1.2 题解
- C++——USACO Section 1.1 题解
- C++——USACO Section 1.3 题解
- C++——USACO Section 1.4 题解
- C++——USACO Section 1.5 题解
- C++——USACO Section 2.1 题解
- C++——USACO Section 2.2 题解
- C++——USACO Section 2.3 题解
- C++——USACO Section 2.4 题解
- C++——USACO Section 3.1 题解
- C++——USACO Section 3.2 题解
- USACO Section 1.2 题解
- USACO Section 1.1 题解
- USACO Section 1.3 题解
- USACO Section 1.4 题解
- USACO Section 1.5 题解
- USACO Section 2.1 题解
- [USACO] Section 1.1 ride 题解
- 验证码
- C 空格替换
- 组合-lintcode
- 微架构与SOA的区别
- HDU 2222 Keywords Search AC自动机
- Pascal & C++——USACO Section 1.2 题解
- 文件上传
- 设计模式之策略模式
- linux设置IP
- RxJava框架
- [BZOJ2111][ZJOI2010]Perm 排列计数(组合数学+lucas定理)
- Python学习
- Servlet(1)
- Caffe新手教程傻瓜系列(9):训练和测试自己的图片