1.Alex and broken contest
Description
One day Alex was creating a contest about his friends, but accidentally deleted it. Fortunately, all the problems were saved, but now he needs to find them among other problems.
But there are too many problems, to do it manually. Alex asks you to write a program, which will determine if a problem is from this contest by its name.
It is known, that problem is from this contest if and only if its name contains one of Alex's friends' name exactly once. His friends' names are "Danil", "Olya", "Slava", "Ann" and "Nikita".
Names are case sensitive.
Input
The only line contains string from lowercase(小写字母) and uppercase(大写字母) letters and "_" symbols of length, not more than 100 — the name of the problem.
Output
Print "YES", if problem is from this contest, and "NO" otherwise.
Sample Input
Alex_and_broken_contest
NO
NikitaAndString
YES
Danil_and_Olya
NO
题目意思:所给的字符串中只能包含他朋友的一个名字。
解题思路:当时比赛的时候没多想直接用了笨办法,反正数据量很小,直接一个个的比对。
之后看到有大神用string中的find做,确实很简单,之前find也学的不清楚,这次贴出代码。
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 char s[110]; 6 int main() 7 { 8 int i,len,count; 9 while(scanf("%s",s)!=EOF) 10 { 11 len=strlen(s); 12 count=0; 13 for(i=0; i<len; i++) 14 { 15 if(s[i]=='D') 16 { 17 if(s[i+1]=='a'&&s[i+2]=='n'&&s[i+3]=='i'&&s[i+4]=='l') 18 { 19 count++; 20 } 21 } 22 else if(s[i]=='O') 23 { 24 if(s[i+1]=='l'&&s[i+2]=='y'&&s[i+3]=='a') 25 { 26 count++; 27 } 28 } 29 else if(s[i]=='S') 30 { 31 if(s[i+1]=='l'&&s[i+2]=='a'&&s[i+3]=='v'&&s[i+4]=='a') 32 { 33 count++; 34 } 35 } 36 else if(s[i]=='A') 37 { 38 if(s[i+1]=='n'&&s[i+2]=='n') 39 { 40 count++; 41 } 42 } 43 else if(s[i]=='N') 44 { 45 if(s[i+1]=='i'&&s[i+2]=='k'&&s[i+3]=='i'&&s[i+4]=='t'&&s[i+5]=='a') 46 { 47 count++; 48 } 49 } 50 } 51 if(count==1) 52 { 53 printf("YES\n"); 54 } 55 else 56 { 57 printf("NO\n"); 58 } 59 } 60 return 0; 61 }
c++,使用string中的find
#include <bits/stdc++.h> using namespace std; vector<string> s; int main() { s.push_back("Danil"); s.push_back("Olya"); s.push_back("Slava"); s.push_back("Ann"); s.push_back("Nikita");///建立动态数组 string a; cin>>a; int res = 0; for(int i = 0; i < 5; i++) { if(a.find(s[i]) != a.npos) { res++; if(a.rfind(s[i]) != a.find(s[i]))///一个字符中出现多个一样的名字 { res++; } } } if(res == 1) { cout<<"YES"<<endl; } else { cout<<"NO"<<endl; } return 0; }
2.The Eternal Immortality
Description
Even if the world is full of counterfeits, I still regard it as wonderful.
Pile up herbs and incense, and arise again from the flames and ashes of its predecessor — as is known to many, the phoenix does it like this.
The phoenix has a rather long lifespan, and reincarnates itself once every a! years. Here a! denotes the factorial of integer a, that is, a! = 1 × 2 × ... × a. Specifically, 0! = 1.
Koyomi doesn't care much about this, but before he gets into another mess with oddities, he is interested in the number of times the phoenix will reincarnate in a timespan of b! years, that is, . Note that when b ≥ a this value is always integer.
As the answer can be quite large, it would be enough for Koyomi just to know the last digit of the answer in decimal representation. And you're here to provide Koyomi with this knowledge.
Input
The first and only line of input contains two space-separated integers a and b (0 ≤ a ≤ b ≤ 1018).
Output
Output one line containing a single decimal digit — the last digit of the value that interests Koyomi.
Sample Input
2 4
2
0 10
0
107 109
2
Hint
In the first example, the last digit of is 2;
In the second example, the last digit of is 0;
In the third example, the last digit of is 2.
解题思路:这道题本身就是一个同余定理的应用a*b%c= (a%c) * (b%c) % c
这道题的坑点在于不能直接去暴力做,要求的只是最后一位,我们知道只要乘上一个最后一位是0的数,结果的最后一位一定是0!
所以当b-a>=10的时候一定是0,这样就缩小了范围。
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #define ll long long int 5 using namespace std; 6 int main() 7 { 8 ll a,b,ans,i; 9 scanf("%lld%lld",&a,&b); 10 ans=1; 11 if((b-a)>=10) 12 { 13 ans=0; 14 } 15 else 16 { 17 for(i=a+1;i<=b;i++) 18 { 19 ans=(ans*(i%10))%10; 20 } 21 ans=ans%10; 22 } 23 printf("%lld\n",ans); 24 return 0; 25 }