题damn传送门
题解:
第一题闰年求和:
分析:
很简单判断是不是闰年,是闰年就加起来。
code:
#include<bits/stdc++.h>
using namespace std;
int main(){
int a,b,sum=0;
cin>>a>>b;
for(int i=a+1;i<b;i++){
if((i%4==0&&i%100!=0)||(i%400==0)){
sum+=i;
}
}
cout<<sum;
return 0;
}
第二题当天的第几秒:
分析:
换成24小时制,12小时=42300秒,1小时=3600秒,1分钟=60秒
code:
#include<bits/stdc++.h>
using namespace std;
int main(){
int a,b,c;
char d;
cin>>a>>b>>c>>d;
if(d=='P') cout<<(a+12)*3600+b*60+c;
else cout<<a*3600+b*60+c;
}
第三题长方形面积:
分析:
用暴力枚举就完了。
code:
#include<bits/stdc++.h>
using namespace std;
int main(){
int a,ans=0;
cin>>a;
for(int i=1;i*i<=a;++i){
if(a%i==0){
++ans;
}
}
cout<<ans;
}
第四题每月天数:
分析:
建个数组,判断是否是闰年,如果是,2月加1,最后输出数组名[月份-1](这是格式)。
code:
#include<bits/stdc++.h>
using namespace std;
int a[13]={0,31,28,31,30,31,30,31,31,30,31,30,31},n=0,s=0;
int main(){
cin>>s>>n;
if(n!=2)cout<<a[n]<<endl;
else if(s%400==0||(s%4==0&&s%100!=0))cout<<a[n]+1;
else cout<<a[n]<<endl;
return 0;
}
第五题时间规划:
分析:
建4个变量a,b,c,d。d一直加1,如果d>=60就取余60,c++,知道a=c,b=d,最后输出循环次数,公式:(c-a)*60+d-b。
code:
#include<bits/stdc++.h>
using namespace std;
int main(){
int a,b,c,d;
cin>>a>>b>>c>>d;
cout<<(c-a)*60+d-b;
}
第六题累计相加:
分析:
用双重循环一个一个相加。
code:
#include<bits/stdc++.h>
using namespace std;
int main(){
unsigned long long n,sum=0;
cin>>n;
for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++){
sum+=j;
}
}
cout<<sum;
}
第七题买文具:
分析:
if(x*2+y*5+z*3>q){cout<<"yes"<<endl<<x*2+y*5+z*3-q;}else{cout<<"no"<<endl<<q-x*2+y*5+z*3;}
code:
#include<bits/stdc++.h>
using namespace std;
int main(){
int a,b,c,d;
cin>>a>>b>>c>>d;
if(2*a+b*5+c*3>d){
cout<<"No"<<endl<<(2*a+b*5+c*3)-d;
}else{
cout<<"Yes"<<endl<<d-(2*a+b*5+c*3);
}
}