问题1:顺序查找
大概题意:
从输入的n个整数中查找给定的SearchNum(若存在必唯一)。如果找到,输出SearchNum的位置(从0开始数);如果没有找到,输出“Not Found”。
要求:
有3行。第1行是1个正整数n(n≤20)。第2行是n个整数,数字均不超过基本整型,其间以空格分隔。第3行是SearchNum。
输出
仅一行。如果找到,输出SearchNum的位置(从0开始数);如果没有找到,输出“Not Found”。
方法1:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,i,m,flag=0;
int a[10001];
cin>>n;
for(i=0;i<n;i++){
cin>>a[i];
}
cin>>m;
for(i=0;i<n;i++)
{
if(a[i]==m)
{
cout<<i;
flag=1;/用flag标记一下,flag=1证明真。
}
}
if(flag==0)
cout<<"Not Found";
return 0;
}
方法2:
int main()
{
int i, n, x, index;
int a[N];
scanf("%d", &n);
scanf("%d", &x);
for (i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
for (i = 0; i < n; i++)
{
if (a[i] == x)
{
index = i;/用index来标记查找元素的下标
printf("%d", index);
break;
}
}
if (i == n)
{
printf("Not Found\n");
}
return 0;
}
问题2:2的n次方
大概题意:
对于任意给定的n,计算2的n次方。
要求:
输入一个整数,输出2的n次方。
方法1:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
int power=1;/必须要初始成1
for(int i=1;i<=n;i++)/此处的for循环代表的意思是n次
{
power=power*2;
}
cout<<power<<endl;
return 0;
相当于:1*2;
1*2*2;
1*2*2*2;
方法2:
直接用pow(2,n)即可,但是建议不要使用,数越大误差越大。
}插入代码片
问题3:小x与三角形
大概题意:
给出三角形的两条边,求满足条件的三角形有几个。
#pragma GCC optimize(2)
#pragma G++ optimize(2)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+9;
int main()
{
int a,b,sum=0,left,right;
int i;
cin>>a>>b;
left=abs(a-b)-1;
right=a+b-1;
sum=right-left-1;
cout<<sum<<endl;
return 0;
}代码片
本道题用枚举法会超时,所以要改变思路,利用技巧解答,根据两边之和大于第三边,两边之差小于第三边,两种情况可以作为两种边界条件。
问题3:幼儿园小朋友们的难题
大概题意:输入多组字符串,找到字符串最大的非负整数。
要求:输入
输入有多组数据。每组输入一个句子(一定包含数字字符,可能包含空格),长度小于256,占一行
输出
输出对应有多行,每行输出所找出的最大的那个非负整数
#include<bits/stdc++.h>
using namespace std;
int main()
{
string s;//定义一个字符串
while(getline(cin,s))
{
string max="0";//因为题目中要求的是最大负整数,所以假设最大为0即可
for(int i=0;i<s.size();i++){
if(isdigit(s[i])&&s[i]!='0')//判断字符串是否为数字并且不含有前导零
{
string a;
while(isdigit(s[i]))
{
a+=s[i++];//如果为数字那么将字符储存到字符串a中即可
}
if(a.size() >max.size() ||(a.size() ==max.size() &&a>max))
{
max=a;
}
}
}
cout<<max<<endl;
}
return 0;
}
补充知识点:
对于string的输入需要注意,当想读取一整行只能用getline(),在用getline读取时可以包括空格。
比较字符串出现的数字的大小的方法:
(1)可以直接比较连续数字的长度,长度长的自然大于长度短的。
(2)如果长度相等时,可以利用string类的比较大小,因为string 类型对于字符的比较是按照字典序排序的,从前往后进行比较。
所以对于本题可以直接用string类储存答案,将最大值初始化成0,遇到非0时再进行记录,这样可以避免出现前导零的情况。
问题4:特殊的数字
题目描述
153是一个非常特殊的数,它等于它的每位数字的立方和,即153=111+555+333。编程求所有满足这种条件的三位十进制数。
输出
输出格式
按从小到大的顺序输出满足条件的三位十进制数,每个数占一行。
#pragma GCC optimize(2)
#pragma G++ optimize(2)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+9;
int main()
{
int a,b,c,sum=0;
for(int i=100;i<=999;i++)
{
int t=i;
a=t%10;
b=t/10%10;
c=t/100;
sum=pow(a,3)+pow(b,3)+pow(c,3);
if(t==sum)
cout<<t<<endl;
}
return 0;
}
问题5:分苹果
把一堆苹果分给 n 个小朋友,要使每个人都能拿到苹果,而且每个人拿到的苹果数都不同的话,这堆苹果至少应该有多少个?
输入
一个不大于 1000 的正整数 n,代表小朋友人数。
输出
一个整数,表示满足条件的最少苹果个数。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
ll n,sum=0,i;
cin>>n;
for(i=1;i<=n;i++)
{
sum+=i;/从第一个开始后一个比前一个多一即可:1,2,3,4,5....
}
cout<<sum<<endl;
return 0;
}
问题6:Mad Scientist(中石油16L)
题目描述
Farmer John’s cousin Ben happens to be a mad scientist. Normally, this creates a good bit of friction at family gatherings, but it can occasionally be helpful, especially when Farmer John finds himself facing unique and unusual problems with his cows.
Farmer John is currently facing a unique and unusual problem with his cows. He recently ordered N cows (1≤N≤1000) consisting of two different breeds: Holsteins and Guernseys. He specified the cows in his order in terms of a string of N characters, each either H (for Holstein) or G (for Guernsey). Unfortunately, when the cows arrived at his farm and he lined them up, their breeds formed a different string from this original string.
Let us call these two strings A and B, where A is the string of breed identifiers Farmer John originally wanted, and B is the string he sees when his cows arrive. Rather than simply check if re-arranging the cows in B is sufficient to obtain A, Farmer John asks his cousin Ben to help him solve the problem with his scientific ingenuity.
After several months of work, Ben creates a remarkable machine, the multi-cow-breed-flipinator 3000, that is capable of taking any substring of cows and toggling their breeds: all Hs become Gs and all Gs become Hs in the substring. Farmer John wants to figure out the minimum number of times he needs to apply this machine to transform his current ordering B into his original desired ordering A. Sadly, Ben’s mad scientist skills don’t extend beyond creating ingenious devices, so you need to help Farmer John solve this computational conundrum.
输入
The first line of input contains N, and the next two lines contain the strings A and B. Each string has N characters that are either H or G.
输出
Print the minimum number of times the machine needs to be applied to transform BB into AA.
样例输入 Copy
7
GHHHGHH
HHGGGHH
样例输出 Copy
2
提示
First, FJ can transform the substring that corresponds to the first character alone, transforming B into GHGGGHH. Next, he can transform the substring consisting of the third and fourth characters, giving A. Of course, there are other combinations of two applications of the machine that also work.
#include<bits/stdc++.h>
using namespace std;
int k,n,sum=0;
string a,b;
int flag[10001];
int main()
{
cin>>n;
cin>>a;
cin>>b;
for(int i=0;i<n;i++)
{
if(a[i]!=b[i])
flag[i]=1;/不相等记为1
else
flag[i]=0;相等记为0
}
for(int i=0;i<n;i++)
{
if(flag[i]==1&&flag[i+1]!=1)
sum++;
}
cout<<sum<<endl;
return 0;
}
问题7: Word Processor
题目描述
Bessie the cow is working on an essay for her writing class. Since her handwriting is quite bad, she decides to type the essay using a word processor.
The essay contains N words (1≤N≤100), separated by spaces. Each word is between 1 and 15 characters long, inclusive, and consists only of uppercase or lowercase letters. According to the instructions for the assignment, the essay has to be formatted in a very specific way: each line should contain no more than K (1≤K≤80) characters, not counting spaces. Fortunately, Bessie’s word processor can handle this requirement, using the following strategy:
·If Bessie types a word, and that word can fit on the current line, put it on that line.
·Otherwise, put the word on the next line and continue adding to that line.
Of course, consecutive words on the same line should still be separated by a single space. There should be no space at the end of any line.
Unfortunately, Bessie’s word processor just broke. Please help her format her essay properly!
输入
The first line of input contains two space-separated integers N and K.
The next line contains N words separated by single spaces. No word will ever be larger than K characters, the maximum number of characters on a line.
输出
Bessie’s essay formatted correctly.
样例输入 Copy
10 7
hello my name is Bessie and this is my essay
样例输出 Copy
hello my
name is
Bessie
and this
is my
essay
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+9;
int main()
{
int n,k;
cin>>n>>k;
string s[n];
for(int i=0 ; i<n ;i++) cin>>s[i];
int cnt=0;/cnt代表当前的长度
for(int i=0;i<n;i++)
{
cout<<s[i]<<" ";
cnt+=s[i].size();
if(cnt+s[i+1].size()>k)
{
cout<<endl;
cnt=0;
}
}
return 0;
}
问题8:Sleepy Kaguya
Description
Houraisan☆Kaguya is the princess who lives in Literally House of Eternity. However, she is very playful and often stays up late. This morning, her tutor, Eirin Yagokoro was going to teach her some knowledge about the Fibonacci sequence. Unfortunately, the poor princess was so sleepy in class that she fell asleep. Angry Eirin asked her to complete the following task:
This sequence can be described by following equations:
1.F[1]=F[2]=1
2.F[n]=F[n−1]+F[n−2] (n>2)
Now, Kaguya is required to calculate F[k+1]∗F[k+1]−F[k]∗F[k+2] for each integer k that does
not exceed 1018. Kaguya is so pathetic. You have an obligation to help her. (I love Houraisan Kaguya forever!!!)
Input
Only one integer k
Output
Only one integer as the result which is equal to F[k+1]∗F[k+1]−F[k]∗F[k+2]
Samples
Input Copy
2
Output
1
Hint
F[2]=1,F[3]=2,F[4]=3
22-13
0<k≤1018
If necessary, please use %I64d instead of %lld when you use “scanf”, or just use “cin” to get the cases.
The online judge of HNU has the above feature, thank you for your cooperation.
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+9;
int main()
{
ll n;
cin>>n;
if(n%2==0)
cout<<"1"<<endl;
else
cout<<"-1"<<endl;
return 0;
}
找规律判断奇偶即可。
问题9:B. The Right-angled Triangleswith Sides of Integral Length
Description
Consider the right-angled triangles with sides of integral length. Give you the integral length of the hypotenuse of a right-angled triangle. Can it construct a right triangle with given hypotenuse c such that the two legs of the triangle are all integral length
Input
There are several test cases. The first line contains an integer T(1≤T≤1,000), T is the number of test cases. The following T lines contain T test cases, each line contains one test case. For each test case, there is an integer : c, the length of hypotenuse.(1≤c≤45,00)
Output
For each case, output Yes if it can construct a right triangle with given hypotenuse c and sides of integral length , No otherwise
Samples
Input Copy
4
5
6
15
13
Output
Yes
No
Yes
Yes
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+9;
bool fac(ll c)
{
ll c1=c*c;
for(int i=1;i*i<=c1/2;i++)
{
ll d=sqrt(c1-i*i);
if(d*d==c1-i*i)
return 1;
}
return 0;
}
int main()
{
ll n,a;
cin>>n;
while(n--)
{
cin>>a;
if(fac(a)){
cout<<"Yes"<<endl;
}
else
cout<<"No"<<endl;
}
return 0;
}
如果三角形三边满足如下关系,则是直角三角形。
• a=m2-n2
• b=2mn
• c=m2+n2
解释:这是本原勾股数 因为
a2+b2
=(m^4 + n^4)- 2×m2×n2+(2mn)^2
=(m2+n2)2=c2
• 所以如果斜边长度能够表示成2个正整数的平方和,则能使得三边都是正整数。这样枚举的复杂度是O( c)。
• 另外,如果斜边长度是一个合数,其有一个因子能表示为2个正整数的平方和,那么也能使得三边都是正整数。比如c=15,有因
子5=12+22,那么也是可以构成三边全是整数的直角三角形,每边长度乘以3即可。就是( 9, 12, 15)。
问题10:记数问题
试计算在区间 1 到 n 的所有整数中,数字 x (0 ≤ x ≤ 9)共出现了多少次?例如,在 1到 11 中,即在 1、2、3、4、5、6、7、8、9、10、11 中,数字 1 出现了 4 次。
输入
输入共 1 行,包含 2 个整数 n、x,之间用一个空格隔开。1≤n≤1,000,000 ,0≤x≤9
输出
输出共 1 行,包含一个整数,表示 x 出现的次数。
样例输入 Copy
11 1
样例输出 Copy
4
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+9;
int main()
{
ll n,sum=0,x,ans=0,b;
cin>>n>>x;
for(int i=1;i<=n;i++)
{
ll b=i;
while(b!=0)
{
sum=b%10;
b/=10;
if(sum==x)
ans++;
}
}
cout<<ans<<'\n';
return 0;
}
问题11: Teleportation II
题目描述
One of the farming chores Farmer John dislikes the most is hauling around lots of cow manure. In order to streamline this process, he comes up with a brilliant invention: the manure teleporter! Instead of hauling manure between two points in a cart behind his tractor, he can use the manure teleporter to instantly transport manure from one location to another.
Farmer John’s farm is built along a single long straight road, so any location on his farm can be described simply using its position along this road (effectively a point on the number line). A teleporter is described by two numbers x and y, where manure brought to location x can be instantly transported to location y, or vice versa.
Farmer John wants to transport manure from location a to location b, and he has built a teleporter that might be helpful during this process (of course, he doesn’t need to use the teleporter if it doesn’t help). Please help him determine the minimum amount of total distance he needs to haul the manure using his tractor.
输入
The first and only line of input contains four space-separated integers: a and b, describing the start and end locations, followed by x and y, describing the teleporter. All positions are integers in the range 0…100, and they are not necessarily distinct from each-other.
输出
Print a single integer giving the minimum distance Farmer John needs to haul manure in his tractor.
样例输入 Copy
3 10 8 2
样例输出 Copy
3
提示
In this example, the best strategy is to haul the manure from position 3 to position 2, teleport it to position 8, then haul it to position 10. The total distance requiring the tractor is therefore 1 + 2 = 3.
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+5;
const int N=1e9+7;
int main()
{
int a,b,x,y,sum=0;
cin>>a>>b>>x>>y;
int tem=abs(a-b);
if(abs(a-x)<abs(a-y))
{
sum+=abs(a-x)+abs(b-y);
}
else
{
sum+=abs(b-x) +=abs(a-y);
}
int ans=min(sum,tem);
cout<<ans<<'\n';
return 0;
}
问题12:Training Camp
题目描述
Snuke loves working out. He is now exercising N times.
Before he starts exercising, his power is 1. After he exercises for the i-th time, his power gets multiplied by i.
Find Snuke’s power after he exercises N times. Since the answer can be extremely large, print the answer modulo 109+7.
Constraints
1≤N≤105
输入
The input is given from Standard Input in the following format:
N
输出
Print the answer modulo 109+7.
样例输入 Copy
3
样例输出 Copy
6
提示
After Snuke exercises for the first time, his power gets multiplied by 1 and becomes 1.
After Snuke exercises for the second time, his power gets multiplied by 2 and becomes 2.
After Snuke exercises for the third time, his power gets multiplied by 3 and becomes 6.
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+5;
const int N=1e9+7;
ll a[maxn];
int main()
{
ll n;
a[0]=1;
for(int i=1;i<=maxn;i++)//打表
{
a[i]=a[i-1]*i%N;
}
cin>>n;
cout<<a[n]<<'\n';
return 0;
}
//第二种方法:
{
ll n,sum=1;
cin>>n;
for(int i=1;i<=n;i++)
{
sum=sum*i%N;
}
cout<<sum%N<<'\n';
}
求1到n的阶乘对1e9+7取余即可
问题13:Scc Puzzle
题目描述
Snuke loves puzzles.
Today, he is working on a puzzle using S- and c-shaped pieces. In this puzzle, you can combine two c-shaped pieces into one S-shaped piece, as shown in the figure below:
Snuke decided to create as many Scc groups as possible by putting together one S-shaped piece and two c-shaped pieces.
Find the maximum number of Scc groups that can be created when Snuke has N S-shaped pieces and M c-shaped pieces.
Constraints
1≤N,M≤1012
输入
The input is given from Standard Input in the following format:
N M
输出
Print the answer.
样例输入 Copy
1 6
样例输出 Copy
2
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+5;
const int N=1e9+7;
ll a[maxn];
int main()
{
ll n,m;
cin>>n>>m;
if(m/2<n) cout<<m/2;
else cout<<n+((m-n*2)/4)<<'\n';
return 0;
}
问题14:表达式求值
题目描述
给定一个只包含加法和乘法的算术表达式,请你编程计算表达式的值。
输入
输入仅有一行,为需要你计算的表达式,表达式中只包含数字、加法运算符“+”和乘法运算符“*”,且没有括号,所有参与运算的数字均为 0 到 2^31-1 之间的整数。输入数据保证这一行只有 0~ 9、+、这 12 种字符。
输出
输出只有一行,包含一个整数, 表示这个表达式的值。 注意: 当答案长度多于 4 位时,请只输出最后 4 位,前导 0 不输出。
样例输入 Copy
1+10000000031
样例输出 Copy
4
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+9;
stack<int> x;
int main()
{
int a,b;
char c;
cin>>a;
int m=10000;
a=a%m;
x.push(a);
while(cin>>c>>b)
{
if(c=='*')
{
a=x.top();
x.pop();
x.push(a*b%m);
}
else
x.push(b);
}
a=0;
while(x.size())
{
a+=x.top();
a%=m;
x.pop();
}
cout<<a<<'\n';
return 0;
}
问题15:小朋友的数字
题目描述
有n个小朋友排成一列。每个小朋友手上都有一个数字,这个数字可正可负。规定每个小朋友的特征值等于排在他前面(包括他本人)的小朋友中连续若干个(最少有一个)小朋友手上的数字之和的最大值。
作为这些小朋友的老师,你需要给每个小朋友一个分数,分数是这样规定的:第一个小朋友的分数是他的特征值,其它小朋友的分数为排在他前面的所有小朋友中(不包括他本人),小朋友分数加上其特征值的最大值。
请计算所有小朋友分数的最大值,输出时保持最大值的符号,将其绝对值对p取模后输出。
输入
第一行包含两个正整数n、p,之间用一个空格隔开。
第二行包含n个数,每两个整数之间用一个空格隔开,表示每个小朋友手上的数字。
输出
输出只有一行,包含一个整数,表示最大分数对p取模的结果。
样例输入 Copy
5 997
1 2 3 4 5
样例输出 Copy
21
提示
样例1小朋友的特征值分别为1、3、6、10、15,分数分别为1、2、5、11、21,最大值21对997的模是21。
对于 100%的数据,1 ≤ n ≤ 1,000,000,1 ≤ p ≤ 10^9,其他数字的绝对值均不超过 10^9。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[1000005][3],n,p,x,maxn;
bool bo=0;
int main()
{
ll n,p,x,i;
cin>>n>>p;
p=abs(p);
cin>>x;
a[1][1]=a[1][0]=a[1][2]=x;
maxn=x;
for(i=2;i<=n;i++)
{
cin>>x;
if(a[i-1][1]>0) a[i][1]=a[i-1][1]+x;
else a[i][1]=x;
a[i][0]=max(maxn,a[i][1]);
maxn=a[i][0];
}
a[2][2]=a[1][0]*2;
if(a[2][2]>=a[1][2]) bo=1;
for(i=3;i<=n;i++)
{
if(a[i-1][0]<0) a[i][2]=a[i-1][2];
else {
a[i][2]=a[i-1][2]+a[i-1][0];
if(a[i][2]>a[1][2]) bo=1;
if(a[i][2]>1000000000)a[i][2]%=p;
}
}
if(bo) cout<<a[n][2]%p<<'\n';
else cout<<a[1][2]%p<<'\n';
return 0;
}
问题16:Three-letter acronym
题目描述
You are given three words s1, s2 and s3, each composed of lowercase English letters, with spaces in between. Print the acronym formed from the uppercased initial letters of the words.
Constraints
s1, s2 and s3 are composed of lowercase English letters.
1≤|si|≤10(1≤i≤3)
输入
Input is given from Standard Input in the following format:
s1 s2 s3
输出
Print the answer.
样例输入 Copy
atcoder beginner contest
样例输出 Copy
ABC
提示
The initial letters of atcoder, beginner and contest are a, b and c. Uppercase and concatenate them to obtain ABC.
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+9;
int main()
{
char a[200],ch1;
while(cin>>a)
{
int len1=strlen(a);
for(int i=0;i<len1;i++)
{
ch1=a[0]-32;
}
cout<<ch1;
}
return 0;
}
问题17:买铅笔
题目描述
P老师需要去商店买n支铅笔作为小朋友们参加NOIP的礼物。她发现商店一共有3种包装的铅笔,不同包装内的铅笔数量有可能不同,价格也有可能不同。为了公平起见,P老师决定只买同一种包装的铅笔。
商店不允许将铅笔的包装拆开,因此P老师可能需要购买超过n支铅笔才够给小朋友们发礼物。
现在P老师想知道,在商店每种包装的数量都足够的情况下,要买够至少n支铅笔最少需要花费多少钱。
输入
输入的第一行包含一个正整数n,表示需要的铅笔数量。接下来三行,每行用两个正整数描述一种包装的铅笔:其中第一个整数表示这种包装内铅笔的数量,第二个整数表示这种包装的价格。保证所有的7个数都是不超过10000的正整数。
输出
输出一行一个整数,表示P老师最少需要花费的钱。
样例输入 Copy
57
2 2
50 30
30 27
样例输出 Copy
54
提示
铅笔的三种包装分别是:
·2支装,价格为2;
·50支装,价格为30;
·30支装,价格为27。
P老师需要购买至少_57支铅笔。
如果她选择购买第一种包装,那么她需要购买29份,共计2×29=5 8支,需要花费的钱为2×29=58。
实际上,P老师会选择购买第三种包装,这样需要买2份。虽然最后买到的铅笔数量更多了,为30×2=60支,但花费却减少为27×2=_54,比第一种少。
对于第二种包装,虽然每支铅笔的价格是最低的,但要够发必须买2份,实际的花费达到了30×2=60,因此P老师也不会选择。
所以最后输出的答案是54。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+9;
int main()
{
int n,a,b,sum=0;
int max1=10000000;
cin>>n;
for(int i=0;i<3;i++)
{
cin>>a>>b;
if(n%a!=0)
{
sum=(n/a+1)*b;
if(sum<max1)
max1=sum;
}
else
{
sum=(n/a)*b;
if(sum<max1)
max1=sum;
}
}
cout<<max1<<'\n';
return 0;
}
注意本题不是贪心题哦!
问题18:Comparison
题目描述
You are given two positive integers A and B. Compare the magnitudes of these numbers.
Constraints
1≤A,B≤10100
Neither A nor B begins with a 0.
输入
Input is given from Standard Input in the following format:
A
B
输出
Print GREATER if A>B, LESS if A<B and EQUAL if A=B.
样例输入 Copy
36
24
样例输出 Copy
GREATER
提示
Since 36>24, print GREATER.
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5;
char a[N],b[N];
int main()
{
scanf("%s%s",a,b);/数太大要用char数组做
int len1=strlen(a);
int len2=strlen(b);
if(len1<len2) cout<<"LESS";
else if(len1>len2) cout<<"GREATER";
else if(len1==len2)
{
for(int i=0;i<len1;i++)
{
if(a[i]>b[i]){
cout<<"GREATER";break;
}
else if(a[i]<b[i]){
cout<<"LESS";break;
}
else if(a[i]==b[i]){
cout<<"EQUAL";break;
}
}
}
return 0;
}
问题19:回文日期
题目描述
在日常生活中,通过年、月、日这三个要素可以表示出一个唯一确定的日期。
牛牛习惯用8位数字表示一个日期,其中,前4位代表年份,接下来2位代表月份,最后2位代表日期。显然:一个日期只有一种表示方法,而两个不同的日期的表示方法不会相同。
牛牛认为,一个日期是回文的,当且仅当表示这个日期的8位数字是回文的。现在,牛牛想知道:在他指定的两个日期之间(包含这两个日期本身),有多少个真实存在的日期是回文的。
【提示】
一个8位数字是回文的,当且仅当对于所有的i(1<i<8)从左向右数的第i个数字和第9-i个数字(即从右向左数的第i个数字)是相同的。
例如:
·对于2016年11月19日,用8位数字20161119表示,它不是回文的。
·对于2010年1月2日,用8位数字20100102表示,它是回文的。
·对于2010年10月2日,用8位数字20101002表示,它不是回文的。
每一年中都有12个月份: 其中,1, 3, 5, 7, 8, 10, 12月每个月有31天;4, 6, 9, 11月每个月有30天;而对于2月,闰年时有29天,平年时有28天。 一个年份是闰年当且仅当它满足下列两种情况其中的一种: 1.这个年份是4的整数倍,但不是100的整数倍; 2.这个年份是400的整数倍。
例如:
·以下几个年份都是闰年:2000 , 2012 , 2016。
·以下几个年份是平年:1900, 2011、2014。
输入
输入包括两行,每行包括一个8位数字。
第一行表示牛牛指定的起始日期dates,第二行表示牛牛指定的终止日期date2。保证dates和date2都是真实存在的日期,且年份部分一定为4位数字,且首位数字不为0;保证dates一定不晚于date2。
输出
输出一行,包含一个整数,表示在date1和date2之间,有多少个日期是回文的。
样例输入 Copy
20110101
20111231
样例输出 Copy
1
提示
对于样例1,符合条件的日期是20111102。
#pragma GCC optimize(2)
#pragma G++ optimize(2)
#pragma GCC optimize(3)
#pragma G++ optimize(3)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+9;
int s[13]={0,31,30,31,30,31,30,31,31,30,31,30,31};
int main()
{
int n,m,i,j,ans=0,sum;
scanf("%d%d",&n,&m);
for(i=1;i<=12;i++)
{
for(j=1;j<=s[i];j++)
{
int tem=(j%10)*1000+(j/10)*100+(i%10)*10+(i/10);
sum=tem*10000+i*100+j;
if(sum<n||sum>m)continue;
ans++;
}
}
cout<<ans<<'\n';
return 0;
}