7.19 解题报告

A - Lucky Division
Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
Petya calls a number almost lucky if it could be evenly divided by some lucky number. Help him find out if the given number n is almost lucky.

Input
The single line contains an integer n (1 ≤ n ≤ 1000) — the number that needs to be checked.

Output
In the only line print "YES" (without the quotes), if number n is almost lucky. Otherwise, print "NO" (without the quotes).

Example
Input
47
Output
YES
Input
16
Output
YES
Input
78
Output
NO

Note
Note that all lucky numbers are almost lucky as any number is evenly divisible by itself.
In the first sample 47 is a lucky number. In the second sample 16 is divisible by 4.

题意:
给出一个数字,如果所有数位上的数字都是4或7,那么称这个数为“幸运数”。如果一个数能被“幸运数”整除,那么称这个数为“近似幸运数”。现在给一个数,请判断它是不是“幸运数”或者是“近似幸运数”。

思路:已经很明确了,水题一枚。但是注意:“能被“幸运数”整除”指一个数可以被4,7,47,74,444,447,474,477,744,747,774……等数整除的。

代码:

 
  1. #include<iostream>
  2. using namespace std;
  3. int main(){
  4. int n,a[30]={0},i=1,flag=0;
  5. cin>>n;
  6. if(((n%4==0)||(n%7==0))||((n%74==0)||(n%47==0))||((n%774==0)||(n%447==0))||((n%474==0)||(n%747==0))||((n%744==0)||(n%477==0)))cout<<"YES"<<endl;
  7. else{
  8. while(n){
  9. a[i]=n%10;
  10. n/=10;
  11. i++;
  12. if(a[i]!=7&&a[i]!=4){
  13. cout<<"NO"<<endl;
  14. flag=1;
  15. break;
  16. }
  17. }
  18. if(flag==0)cout<<"YES"<<endl;
  19. }
  20. return 0;
  21. }

B - Lucky Substring
Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya was delivered a string s, containing only digits. He needs to find a string that
represents a lucky number without leading zeroes,
is not empty,
is contained in s as a substring the maximum number of times.
Among all the strings for which the three conditions given above are fulfilled, Petya only needs the lexicographically minimum one. Find this string for Petya.

Input
The single line contains a non-empty string s whose length can range from 1 to 50, inclusive. The string only contains digits. The string can contain leading zeroes.

Output
In the only line print the answer to Petya's problem. If the sought string does not exist, print "-1" (without quotes).

Example
Input
047
Output
4
Input
16
Output
-1
Input
472747
Output
7

Note
The lexicographical comparison of strings is performed by the < operator in the modern programming languages. String x is lexicographically less than string y either if x is a prefix of y, or exists such i (1 ≤ i ≤ min(|x|, |y|)), that xi < yi and for any j (1 ≤ j < i) xj = yj. Here |a| denotes the length of string a.
In the first sample three conditions are fulfilled for strings "4", "7" and "47". The lexicographically minimum one is "4".
In the second sample s has no substrings which are lucky numbers.
In the third sample the three conditions are only fulfilled for string "7".

题意:
Petya喜欢幸运数字。大家都知道幸运数字是正整数,其十进制表示只包含幸运数字4和7.例如,数字47,744,4是幸运的,5,17,467不是。
有一天,Petya被送了一个字符串,只包含数字。他需要找到一个字符串
代表一个没有前导零的幸运数字,
不是空的,
在s中包含最大次数的子串。
在满足上述三个条件的所有字符串中,Petya只需要字典上最小的字符串。找到这个Petya的字符串。

思路:
直接找、判断就是了

代码:

 
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cmath>
  5. #include <cstring>
  6. #include <string>
  7. #include <algorithm>
  8. using namespace std;
  9. int main()
  10. {
  11. char a[51];
  12. int i,count1,count2;
  13. scanf("%s",&a);
  14. count1=count2=0;
  15. for(i=0;a[i]!='\0';i++)
  16. {
  17. if(a[i]=='4')
  18. {
  19. count1++;
  20. }
  21. if(a[i]=='7')
  22. {
  23. count2++;
  24. }
  25. }
  26. if(count1==0&&count2==0)
  27. {
  28. printf("-1\n");
  29. }
  30. else if(count1>=count2)
  31. {
  32. printf("4\n");
  33. }
  34. else if(count1<count2)
  35. {
  36. printf("7\n");
  37. }
  38. return 0;
  39. }

C - The number of positions
Petr stands in line of n people, but he doesn't know exactly which position he occupies. He can say that there are no less than a people standing in front of him and no more than b people standing behind him. Find the number of different positions Petr can occupy.

Input
The only line contains three integers n, a and b (0 ≤ a, b < n ≤ 100).

Output
Print the single number — the number of the sought positions.

Example
Input
3 1 1
Output
2
Input
5 2 3
Output
3

Note
The possible positions in the first sample are: 2 and 3 (if we number the positions starting with 1).
In the second sample they are 3, 4 and 5.

题意:
Petr站在n个人的位置,但他不知道他占据什么位置。他可以说,站在他面前的人不少于一个站立在他身后的人。查找Petr可以占据的不同位置的数量。

思路:
一道很简单的模拟水题,因为前边不少于a个人,所以我们位子从a+1开始扫,一直扫到n,如果当前位子的后边的人数小于等于b个,那么就output++
代码:

 
  1. #include<stdio.h>
  2. #include<string.h>
  3. using namespace std;
  4. int main()
  5. {
  6. int n,a,b;
  7. while(~scanf("%d%d%d",&n,&a,&b))
  8. {
  9. int output=0;
  10. int pos=a+1;
  11. for(int i=pos;i<=n;i++)
  12. {
  13. if(n-i<=b)
  14. output++;
  15. }
  16. printf("%d\n",output);
  17. }
  18. }

D - Permutations
You are given n k-digit integers. You have to rearrange the digits in the integers so that the difference between the largest and the smallest number was minimum. Digits should be rearranged by the same rule in all integers.

Input
The first line contains integers n and k — the number and digit capacity of numbers correspondingly (1 ≤ n, k ≤ 8). Next n lines contain k-digit positive integers. Leading zeroes are allowed both in the initial integers and the integers resulting from the rearranging of digits.

Output
Print a single number: the minimally possible difference between the largest and the smallest number after the digits are rearranged in all integers by the same rule.

Example
Input
6 4
5237
2753
7523
5723
5327
2537
Output
2700
Input
3 3
010
909
012
Output
3
Input
7 5
50808
36603
37198
44911
29994
42543
50156
Output
20522

Note
In the first sample, if we rearrange the digits in numbers as (3,1,4,2), then the 2-nd and the 4-th numbers will equal 5237 and 2537 correspondingly (they will be maximum and minimum for such order of digits).
In the second sample, if we swap the second digits and the first ones, we get integers 100, 99 and 102.

题意:
给出n个k位的数字的序列,现在可以对每一个数字进行相同的操作,比如,你把第1个数字的第1,2位互换了,那么剩余的n-1个数字你也必须把第1,2位互换。现在对这n个数字一系列操作后,会产生许多不同的序列,输出min{序列的max-序列的min
思路:
只要把所有情况搜出来就行了,刚开始还想找规律。。

代码:

 
  1. #include<iostream>
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #include<string.h>
  5. #include<math.h>
  6. #include<vector>
  7. #include<set>
  8. #include<queue>
  9. #include<stack>
  10. #include<string>
  11. #include<algorithm>
  12. using namespace std;
  13. char s[100][100],str[100];
  14. int vis[100],k,cha,b[100],n;
  15. #define inf 99999999
  16. void dfs(int wei)
  17. {
  18. int i,j,minx=inf,maxx=-inf,num;
  19. if(wei==k+1){
  20. num=0;
  21. for(i=1;i<=n;i++){
  22. num=0;
  23. for(j=1;j<=k;j++){
  24. num=num*10+s[i][b[j]]-'0';
  25. }
  26. maxx=max(maxx,num);
  27. minx=min(minx,num);
  28. }
  29. cha=min(cha,maxx-minx);//return;
  30. }
  31. else{
  32. for(i=1;i<=k;i++){
  33. if(!vis[i]){
  34. vis[i]=1;
  35. b[wei]=i;
  36. dfs(wei+1);
  37. vis[i]=0;
  38. }
  39. }
  40. }
  41. }
  42. int main()
  43. {
  44. int m,i,j,t,num;
  45. char c;
  46. while(scanf("%d%d",&n,&k)!=EOF)
  47. {
  48. for(i=1;i<=n;i++){
  49. scanf("%s",s[i]+1);
  50. }
  51. cha=inf;
  52. if(n==1){
  53. printf("0\n");continue;
  54. }
  55. memset(vis,0,sizeof(vis));
  56. dfs(1);
  57. printf("%d\n",cha);
  58. }
  59. return 0;
  60. }

E - Wasted Time
Mr. Scrooge, a very busy man, decided to count the time he wastes on all sorts of useless stuff to evaluate the lost profit. He has already counted the time he wastes sleeping and eating. And now Mr. Scrooge wants to count the time he has wasted signing papers.
Mr. Scrooge's signature can be represented as a polyline A1A2... An. Scrooge signs like that: first it places a pen at the point A1, then draws a segment from point A1 to point A2, then he draws a segment from point A2 to point A3 and so on to point An, where he stops signing and takes the pen off the paper. At that the resulting line can intersect with itself and partially repeat itself but Scrooge pays no attention to it and never changes his signing style. As Scrooge makes the signature, he never takes the pen off the paper and his writing speed is constant — 50 millimeters per second.
Scrooge signed exactly k papers throughout his life and all those signatures look the same.
Find the total time Scrooge wasted signing the papers.

Input
The first line contains two integers n and k (2 ≤ n ≤ 100, 1 ≤ k ≤ 1000). Each of the following n lines contains the coordinates of the polyline's endpoints. The i-th one contains coordinates of the point Ai — integers xi and yi, separated by a space.
All points Ai are different. The absolute value of all coordinates does not exceed 20. The coordinates are measured in millimeters.

Output
Print one real number — the total time Scrooges wastes on signing the papers in seconds. The absolute or relative error should not exceed 10 - 6.

Example
Input
2 1
0 0
10 0
Output
0.200000000
Input
5 10
3 1
-5 6
-2 -1
3 2
10 0
Output
6.032163204
Input
6 10
5 0
4 0
6 0
3 0
7 0
2 0
Output
3.000000000

题意:
给n个点的坐标,然后在纸上用折线将这n个点连起来,一共有k张纸,连线速度为50/second ,求所用时间。求出在一张纸上的连线的总长l,l*k/50 即所用时间。

思路:
此题就是求N段线段距离

代码:

 
  1. #include<cstdio>
  2. #include<iostream>
  3. #include<cstring>
  4. #include<cmath>
  5. using namespace std;
  6. int main()
  7. {
  8. int n,k;
  9. int i;
  10. double sum;
  11. double x[101],y[101];
  12. scanf("%d %d",&n,&k);
  13. sum=0;
  14. scanf("%lf %lf",&x[0],&y[0]);
  15. for(i=1;i<n;i++)
  16. {
  17. scanf("%lf %lf",&x[i],&y[i]);
  18. sum+=sqrt((x[i]-x[i-1])*(x[i]-x[i-1])+(y[i]-y[i-1])*(y[i]-y[i-1]));
  19. }
  20. sum=(sum/50)*k;
  21. printf("%lf\n",sum);
  22. return 0;
  23. }

F - Canvas Frames
Nicholas, a painter is going to paint several new canvases. Nicholas is sure that the canvases will turn out so great that each one will need framing and being hung on the wall. Frames are what Nicholas decided to begin with.
Nicholas has n sticks whose lengths equal a1, a2, ... an. Nicholas does not want to break the sticks or glue them together. To make a h × w-sized frame, he needs two sticks whose lengths equal h and two sticks whose lengths equal w. Specifically, to make a square frame (when h = w), he needs four sticks of the same length.
Now Nicholas wants to make from the sticks that he has as many frames as possible; to be able to paint as many canvases as possible to fill the frames. Help him in this uneasy task. Note that it is not necessary to use all the sticks Nicholas has.

Input
The first line contains an integer n (1 ≤ n ≤ 100) — the number of sticks. The second line contains n space-separated integers. The i-th integer equals the length of the i-th stick ai (1 ≤ ai ≤ 100).

Output
Print the single number — the maximum number of frames Nicholas can make for his future canvases.

Example
Input
5
2 4 3 2 3
Output
1
Input
13
2 2 4 4 4 4 6 6 6 7 7 9 9
Output
3
Input
4
3 3 3 5
Output
0

题意:
画家尼古拉斯将画几幅新的画布。尼古拉斯肯定画布会变得如此之大,每个人都会需要框架,挂在墙上。框架是尼古拉斯决定开始的。
尼古拉斯有n个长条等于a1,a2,...的棒。尼古拉斯不想打破棍子或粘在一起。要制作一个h×w尺寸的框架,他需要两根长度等于h的长条和长度等于w的两根棒。具体来说,要制作一个方框(当h = w)时,他需要四根长度相同的棒。
现在,尼古拉斯想从棍棒上做出尽可能多的框架;能够画尽可能多的画布来填充画框。帮助他在这个不安的任务。请注意,没有必要使用Nicholas所有的棒。

代码:

 
  1. #include<cstdio>
  2. #include<iostream>
  3. #include<algorithm>
  4. #include<cstring>
  5. #include<cmath>
  6. using namespace std;
  7. int main(){
  8. int n,i,temp;
  9. int count;
  10. int flag;
  11. int a[101];
  12. for(i=0;i<101;i++)
  13. a[i]=0;
  14. scanf("%d",&n);
  15. for(i=0;i<n;i++){
  16. scanf("%d",&temp);
  17. a[temp]++;
  18. }
  19. flag=0;
  20. count=0;
  21. for(i=0;i<101;i++){
  22. if(a[i]>=2){
  23. a[i]-=2;
  24. if(flag<2)
  25. flag++;
  26. if(flag==2){
  27. count++;
  28. flag=0;
  29. }
  30. if(a[i]>=2)
  31. i--;
  32. }
  33. }
  34. printf("%d\n",count);
  35. return 0;
  36. }

G - Hot Bath
Bob is about to take a hot bath.
There are two taps to fill the bath: a hot water tap and a cold water tap. The cold water's temperature is t1, and the hot water's temperature is t2. The cold water tap can transmit any integer number of water units per second from 0 to x1, inclusive. Similarly, the hot water tap can transmit from 0 to x2 water units per second.
If y1 water units per second flow through the first tap and y2 water units per second flow through the second tap, then the resulting bath water temperature will be:
https://odzkskevi.qnssl.com/53f5a0419be743602c90b2166625a643?v=1500222875![此处输入图片的描述][1]

Bob wants to open both taps so that the bath water temperature was not less than t0. However, the temperature should be as close as possible to this value. If there are several optimal variants, Bob chooses the one that lets fill the bath in the quickest way possible.
Determine how much each tap should be opened so that Bob was pleased with the result in the end.

Input
You are given five integers t1, t2, x1, x2 and t0 (1 ≤ t1 ≤ t0 ≤ t2 ≤ 106, 1 ≤ x1, x2 ≤ 106

Output
Print two space-separated integers y1 and y2 (0 ≤ y1 ≤ x1, 0 ≤ y2 ≤ x2).

Example
Input
10 70 100 100 25
Output
99 33
Input
300 500 1000 1000 300
Output
1000 0
Input
143 456 110 117 273
Output
76 54
Note
In the second sample the hot water tap shouldn't be opened, but the cold water tap should be opened at full capacity in order to fill the bath in the quickest way possible.

题意:
给出整数t1,t2,x1,x2,t0,和公式,(1 ≤ t1 ≤ t0 ≤ t2 ≤ 106, 1 ≤ x1, x2 ≤ 106).求满足(0 ≤ y1 ≤ x1, 0 ≤ y2 ≤ x2)此条件的y1,y2使得t最接近t0,但要保证t>t0.输出y1,y2.多种方案时输出最大的y1,y2.

思路:
不4个特判必WA。不过这4个特判不太容易想全。
case 1:t1==t0 && t2!=t0 => y1=x1,y2=0
case 2:t2==t0 && t1!=t0 => y1=0,y2=x2
case 3:t1==t2 =>y1=x1,y2=x2
做法:
枚举y1,然后t2可以解出来。在选最小的,不过也有坑!见程序吧
case 4:非常难想到的是,当y1==0,方程解出y2=0此时显然不对,所以此种情况仍需处理。

代码:

 
  1. #include<iostream>
  2. #include<cstring>
  3. using namespace std;
  4. long long t1,t2,x1,x2,t0,f1,f2;
  5. long long cal(long long t1,long long y1,long long t2,long long y2)
  6. {
  7. return t1*y1+t2*y2-t0*(y1+y2);
  8. }
  9. void solve(long long y1,long long y2)
  10. {
  11. if(y2<0)y2=0;else if(y2>x2)y2=x2;//溢出
  12. if(y1==0 && y2==0)return;//WA
  13. if(t1*y1+t2*y2<t0*(y1+y2))return;//不满足t>=t0的条件
  14. if(cal(t1,y1,t2,y2)*(f1+f2)<cal(t1,f1,t2,f2)*(y1+y2)||(f1<0 && f2<0))
  15. {
  16. f1=y1;f2=y2;
  17. }
  18. else
  19. if(cal(t1,y1,t2,y2)*(f1+f2)==cal(t1,f1,t2,f2)*(y1+y2)&&y1+y2>f1+f2)
  20. {
  21. f1=y1;f2=y2;
  22. }
  23. }
  24. int main()
  25. {
  26. long long y2;
  27. while(cin>>t1>>t2>>x1>>x2>>t0)
  28. {
  29. f1=-1,f2=-1;
  30. if(t1==t2)//case
  31. {
  32. f1=x1;f2=x2;
  33. }
  34. else if(t1==t0)//case
  35. {
  36. f1=x1;f2=0;
  37. }
  38. else if(t2==t0)//case
  39. {
  40. f1=0;f2=x2;
  41. }
  42. else
  43. for(long long y1=1;y1<=x1;y1++)
  44. {
  45. if(t0>t2)
  46. {
  47. y2=(t1*y1-t0*y1)/(t0-t2);
  48. solve(y1,y2);
  49. }
  50. else
  51. if(t0<t2)
  52. {
  53. y2=(t1*y1-t0*y1)%(t0-t2)==0?(t1*y1-t0*y1)/(t0-t2):1+(t1*y1-t0*y1)/(t0-t2);
  54. solve(y1,y2);
  55. }
  56. }
  57. //y1=0 case 4!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  58. if(t2>=t0)solve(0,x2);
  59. cout<<f1<<" "<<f2<<endl;
  60. }
  61. return 0;
  62. }

H - Cookies
Olga came to visit the twins Anna and Maria and saw that they have many cookies. The cookies are distributed into bags. As there are many cookies, Olga decided that it's no big deal if she steals a bag. However, she doesn't want the sisters to quarrel because of nothing when they divide the cookies. That's why Olga wants to steal a bag with cookies so that the number of cookies in the remaining bags was even, that is, so that Anna and Maria could evenly divide it into two (even 0 remaining cookies will do, just as any other even number). How many ways there are to steal exactly one cookie bag so that the total number of cookies in the remaining bags was even?

Input
The first line contains the only integer n (1 ≤ n ≤ 100) — the number of cookie bags Anna and Maria have. The second line contains n integers ai (1 ≤ ai ≤ 100) — the number of cookies in the i-th bag.

Output
Print in the only line the only number — the sought number of ways. If there are no such ways print 0.

Example
Input
1
1
Output
1
Input
10
1 2 2 3 4 4 4 2 2 2
Output
8
Input
11
2 2 2 2 2 2 2 2 2 2 99
Output
1

Note
In the first sample Olga should take the only bag so that the twins ended up with the even number of cookies.
In the second sample Olga can take any of five bags with two cookies or any of three bags with four cookies — 5 + 3 = 8 ways in total.
In the third sample, no matter which bag with two cookies Olga chooses, the twins are left with 2 * 9 + 99 = 117 cookies. Thus, Olga has only one option: to take the bag with 99 cookies.

题意:
从一个数列中任取出一个数,使剩下的数的和是偶数,问共有多少种取法。

思路:
暴力

代码:

 
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cstdio>
  4. #include <cstring>
  5. #include <string>
  6. #include <vector>
  7. using namespace std;
  8. #define LL long long
  9. int main()
  10. {
  11. int a[110],i,j,n,sum;
  12. while(scanf("%d",&n)!=EOF)
  13. {
  14. int cnt=0;
  15. for(i=0;i<n;i++)
  16. scanf("%d",a+i);
  17. for(i=0;i<n;i++)
  18. {
  19. sum=0;
  20. for(j=0;j<n;j++)
  21. if(j!=i)
  22. sum+=a[j];
  23. if(sum%2==0)
  24. cnt++;
  25. }
  26. printf("%d\n",cnt);
  27. }
  28. return 0;
  29. }

I - Students and Shoelaces
Anna and Maria are in charge of the math club for junior students. When the club gathers together, the students behave badly. They've brought lots of shoe laces to the club and got tied with each other. Specifically, each string ties together two students. Besides, if two students are tied, then the lace connects the first student with the second one as well as the second student with the first one.
To restore order, Anna and Maria do the following. First, for each student Anna finds out what other students he is tied to. If a student is tied to exactly one other student, Anna reprimands him. Then Maria gathers in a single group all the students who have been just reprimanded. She kicks them out from the club. This group of students immediately leaves the club. These students takes with them the laces that used to tie them. Then again for every student Anna finds out how many other students he is tied to and so on. And they do so until Anna can reprimand at least one student.
Determine how many groups of students will be kicked out of the club.

Input
The first line contains two integers n and m — the initial number of students and laces (). The students are numbered from 1 to n, and the laces are numbered from 1 to m. Next m lines each contain two integers a and b — the numbers of students tied by the i-th lace (1 ≤ a, b ≤ n, a ≠ b). It is guaranteed that no two students are tied with more than one lace. No lace ties a student to himself.

Output
Print the single number — the number of groups of students that will be kicked out from the club.

Example
Input
3 3
1 2
2 3
3 1
Output
0
Input
6 3
1 2
2 3
3 4
Output
2
Input
6 5
1 4
2 4
3 4
5 4
6 4
Output
1

Note
In the first sample Anna and Maria won't kick out any group of students — in the initial position every student is tied to two other students and Anna won't be able to reprimand anyone.
In the second sample four students are tied in a chain and two more are running by themselves. First Anna and Maria kick out the two students from both ends of the chain (1 and 4), then — two other students from the chain (2 and 3). At that the students who are running by themselves will stay in the club.
In the third sample Anna and Maria will momentarily kick out all students except for the fourth one and the process stops at that point. The correct answer is one.

解题说明:n个人,m对关系。每次能删除当前所有只跟一个人有关系的人,删到不能删要搞多少次,可以用暴力来做,用数组保存节点,循环判断。

 
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cmath>
  4. #include<algorithm>
  5. using namespace std;
  6. int main()
  7. {
  8. int n, m, i, j;
  9. int a[100][2], b[100] = {0};
  10. scanf("%d %d", &n, &m);
  11. for (i = 0; i < m; i++)
  12. {
  13. scanf("%d %d", &a[i][0], &a[i][1]);
  14. a[i][0]--;
  15. a[i][1]--;
  16. }
  17. for (i = 0; ; i++)
  18. {
  19. int f = 0;
  20. int c[100] = {0};
  21. for (j = 0; j < m; j++)
  22. {
  23. if (b[a[j][0]] == 0 && b[a[j][1]] == 0)
  24. {
  25. c[a[j][0]]++;
  26. c[a[j][1]]++;
  27. }
  28. }
  29. for (j = 0; j < n; j++)
  30. {
  31. if (c[j] == 1)
  32. {
  33. f = 1;
  34. b[j] = 1;
  35. }
  36. }
  37. if (f == 0)
  38. {
  39. break;
  40. }
  41. }
  42. printf("%d\n", i);
  43. return 0;
  44. }

J - cAPS lOCK

wHAT DO WE NEED cAPS LOCK FOR?
Caps lock is a computer keyboard key. Pressing it sets an input mode in which typed letters are capital by default. If it is pressed by accident, it leads to accidents like the one we had in the first passage.
Let's consider that a word has been typed with the Caps lock key accidentally switched on, if:
either it only contains uppercase letters;
or all letters except for the first one are uppercase.
In this case we should automatically change the case of all letters. For example, the case of the letters that form words "hELLO", "HTTP", "z" should be changed.
Write a program that applies the rule mentioned above. If the rule cannot be applied, the program should leave the word unchanged.

Input
The first line of the input data contains a word consisting of uppercase and lowercase Latin letters. The word's length is from 1 to 100 characters, inclusive.

Output
Print the result of the given word's processing.

Example
Input
cAPS
Output
Caps
Input
Lock
Output
Lock

题目大意:仅需考虑两种情况(1)全部为大写字母 (2)除了第一个字符,其他都为大写字母。对于第一种情况,将其改为全部小写输出。对于第二种情况,将其改为仅有第一个字符大写,其他均为小写输出。

解题思路:题目大意已经很明确地指示出所需要考虑的两种情况,是一道水题。

 
  1. #include <iostream>
  2. #include<string.h>
  3. using namespace std;
  4. int main()
  5. {
  6. char c[101];
  7. int i,k,ans,s;
  8. while(cin>>c)
  9. {
  10. k=strlen(c);
  11. ans=s=0;
  12. for(i=1; i<k; i++)
  13. {
  14. if(c[i]>='A'&&c[i]<='Z')
  15. s++;
  16. else if(c[i]>='a'&&c[i]<='z')
  17. ans++;
  18. }
  19. if((ans==0&&s!=0)||k==1)
  20. {
  21. if(c[0]>='a'&&c[0]<='z')
  22. c[0]-=32;
  23. else if(c[0]>='A'&&c[0]<='Z')
  24. c[0]+=32;
  25. for(i=1; i<k; i++)
  26. {
  27. c[i]+=32;
  28. }
  29. cout<<c<<endl;
  30. }
  31. else
  32. cout<<c<<endl;
  33. memset(c,'\0',sizeof(c));
  34. }
  35. return 0;
  36. }

K - Opposites Attract
Everybody knows that opposites attract. That is the key principle of the "Perfect Matching" dating agency. The "Perfect Matching" matchmakers have classified each registered customer by his interests and assigned to the i-th client number ti ( - 10 ≤ ti ≤ 10). Of course, one number can be assigned to any number of customers.
"Perfect Matching" wants to advertise its services and publish the number of opposite couples, that is, the couples who have opposite values of t. Each couple consists of exactly two clients. The customer can be included in a couple an arbitrary number of times. Help the agency and write the program that will find the sought number by the given sequence t1, t2, ..., tn. For example, if t = (1,  - 1, 1,  - 1), then any two elements ti and tj form a couple if i and j have different parity. Consequently, in this case the sought number equals 4.
Of course, a client can't form a couple with him/herself.

Input
The first line of the input data contains an integer n (1 ≤ n ≤ 105) which represents the number of registered clients of the "Couple Matching". The second line contains a sequence of integers t1, t2, ..., tn ( - 10 ≤ ti ≤ 10), ti — is the parameter of the i-th customer that has been assigned to the customer by the result of the analysis of his interests.

Output
Print the number of couples of customs with opposite t. The opposite number for x is number  - x (0 is opposite to itself). Couples that only differ in the clients' order are considered the same.
Note that the answer to the problem can be large enough, so you must use the 64-bit integer type for calculations. Please, do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specificator.

Example
Input
5
-3 3 0 0 3
Output
3
Input
3
0 0 0
Output
3

Note
In the first sample the couples of opposite clients are: (1,2), (1,5) и (3,4).
In the second sample any couple of clients is opposite.

 
  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. int n, num;
  6. cin >> n;
  7. __int64 part[40] = {0};
  8. while(n--)
  9. {
  10. cin >> num;
  11. num += 10;
  12. part[num]++;
  13. }
  14. __int64 sum = 0;
  15. for ( int i = 0; i < 10; i++ )
  16. {
  17. sum += part[i]*part[20-i];
  18. }
  19. if ( part[10] != 0 )
  20. sum += part[10] * (part[10]-1) / 2;
  21. cout << sum << endl;
  22. }

L - HQ9+
HQ9+ is a joke programming language which has only four one-character instructions:
"H" prints "Hello, World!",
"Q" prints the source code of the program itself,
"9" prints the lyrics of "99 Bottles of Beer" song,
"+" increments the value stored in the internal accumulator.
Instructions "H" and "Q" are case-sensitive and must be uppercase. The characters of the program which are not instructions are ignored.
You are given a program written in HQ9+. You have to figure out whether executing this program will produce any output.

Input
The input will consist of a single line p which will give a program in HQ9+. String p will contain between 1 and 100 characters, inclusive. ASCII-code of each character of p will be between 33 (exclamation mark) and 126 (tilde), inclusive.

Output
Output "YES", if executing the program will produce any output, and "NO" otherwise.

Example
Input
Hi!
Output
YES
Input
Codeforces
Output
NO
Note
In the first case the program contains only one instruction — "H", which prints "Hello, World!".
In the second case none of the program characters are language instructions.

字符串水题

 
  1. #include <iostream>
  2. #include<string.h>
  3. using namespace std;
  4. int main()
  5. {
  6. char s[101];
  7. bool flag;
  8. int i,k;
  9. while(cin>>s)
  10. {
  11. flag=true;
  12. k=strlen(s);
  13. for(i=0;i<k;i++)
  14. {
  15. if(s[i]=='H'||s[i]=='Q'||s[i]=='9')
  16. {
  17. cout<<"YES"<<endl;
  18. flag=false;
  19. break;
  20. }
  21. }
  22. if(flag)
  23. cout<<"NO"<<endl;
  24. }
  25. return 0;
  26. }

M - Unary
Unary is a minimalistic Brainfuck dialect in which programs are written using only one token.
Brainfuck programs use 8 commands: "+", "-", "[", "]", "<", ">", "." and "," (their meaning is not important for the purposes of this problem). Unary programs are created from Brainfuck programs using the following algorithm. First, replace each command with a corresponding binary code, using the following conversion table:
">"  →  1000,
"<"  →  1001,
"+"  →  1010,
"-"  →  1011,
"."  →  1100,
","  →  1101,
"["  →  1110,
"]"  →  1111.
Next, concatenate the resulting binary codes into one binary number in the same order as in the program. Finally, write this number using unary numeral system — this is the Unary program equivalent to the original Brainfuck one.
You are given a Brainfuck program. Your task is to calculate the size of the equivalent Unary program, and print it modulo 1000003 (106 + 3).

Input
The input will consist of a single line p which gives a Brainfuck program. String p will contain between 1 and 100 characters, inclusive. Each character of p will be "+", "-", "[", "]", "<", ">", "." or ",".

Output
Output the size of the equivalent Unary program modulo 1000003 (106 + 3).

Example
Input
,.
Output
220
Input
++++[>,.<-]
Output
61425

Note
To write a number n in unary numeral system, one simply has to write 1 n times. For example, 5 written in unary system will be 11111.
In the first example replacing Brainfuck commands with binary code will give us 1101 1100. After we concatenate the codes, we'll get 11011100 in binary system, or 220 in decimal. That's exactly the number of tokens in the equivalent Unary program.

此题是一道模拟题,把符合转换为数字,注意到这里从上到下列出的符号正好和数字中的8-15相对应。所以我们只需要记住某个符号在这个符合表中的位置加上8即可。两个符号连续出现,前一个符号所代表的值要乘以16,再求和。

 
    1. #include <iostream>
    2. #include <cstdio>
    3. #include <cstdlib>
    4. #include <cmath>
    5. #include <cstring>
    6. #include <string>
    7. #include <algorithm>
    8. using namespace std;
    9. char s[110];
    10. char c[]="><+-.,[]";
    11. int main()
    12. {
    13. int i,j,a=0;
    14. scanf("%s",s);
    15. for(i=0;s[i]!='\0';i++)
    16. {
    17. for(j=0;c[j]!=s[i];j++);
    18. {
    19. a=(a*16+8+j)%1000003;
    20. }
    21. }
    22. printf("%d\n",a);
    23. return 0;
    24. }