链接:https://ac.nowcoder.com/acm/contest/19859/B
来源:牛客网
题目描述
输入描述:
多组输入
每组输入一行,有3个数字y,m,x(1000<=y<=3000,1<=m<=12,0<=x<=9),分别代表年份,月份,和他想知道哪个数字出现的次数。
输出描述:
每组输出一个整数,表示数字x在这个月的日期里出现了多少次。
说明
第一组样例中,日中有数字4的为2017-04-04,2017-04-14,2017-04-24,4月一共有30天,因为月份中有4,所以数字4一共出现了30 + 3 = 33次
初试
#include<bits/stdc++.h>
using namespace std;
int FigN(int pre,int N){
int a,ans=0;
while(pre){
if( pre%10==N)
{ans++;}
pre=pre/10;
}
return ans;
}
int fig_day(int year,int month){
switch(month){
case 1: return 31;
case 3: return 31;
case 4: return 30;
case 5: return 31;
case 6: return 30;
case 7: return 31;
case 8: return 31;
case 9: return 30;
case 10: return 31;
case 11:return 30;
case 12: return 31;
break;
}
if(month==2){
if(year%400!=0||year%4==0&&year%100!=0)
return 28;
else return 29;
}
return 0;
}
int main(){
int year,month,N;
int count,day;
while(scanf("%d %d %d",&year,&month,&N)){
count=0;
day=fig_day(year,month);
count=(FigN(year,N)+FigN(month,N))*day;
for(int i=1;i<=day;i++){
count+=FigN(i,N);
}
cout<<count<<endl;
}
return 0;
}
尝试解决2000 1 0输出错误原因:忽视0的特殊性:
再试
#include<bits/stdc++.h>
using namespace std;
int FigN(int pre,int N){
int a,ans=0;
while(pre){
if( pre%10==N)
{ans++;}
pre=pre/10;
}
return ans;
}
int fig_day(int year,int month){
switch(month){
case 1: return 31;
case 3: return 31;
case 4: return 30;
case 5: return 31;
case 6: return 30;
case 7: return 31;
case 8: return 31;
case 9: return 30;
case 10: return 31;
case 11:return 30;
case 12: return 31;
break;
}
if(month==2){
if(year%400!=0||year%4==0&&year%100!=0)
return 28;
else return 29;
}
}
int main(){
int year,month,N;
int count,day;
while(scanf("%d %d %d",&year,&month,&N)){
count=0;
day=fig_day(year,month);
count=(FigN(year,N)+FigN(month,N))*day;
for(int i=1;i<=day;i++){
count+=FigN(i,N);
}
if(N==0){
if(month<10){
count+=day+9;
}
else{
count+=9;
}
}
cout<<count<<endl;
}
return 0;
}
问题1:有报错原因在于,忽略了函数figure_day可能出现无法返回的情况导致编译失败。
问题2:再有为什么之前的输出会出现第二次通过scanf输入之后会一直输出第二次输入的结果?
这里牵扯到一个我比较薄弱的知识点,那就是scanf的详细用法和性质(这里欠下一篇文章,专门写C语言的输入输出),在此暂时略去,而且已找到替代方法,是否有解决方法我们将在文章中探寻。
终版
#include<bits/stdc++.h>
using namespace std;
int FigN(int pre,int N){
int a,ans=0;
while(pre){
if( pre%10==N)
{ans++;}
pre=pre/10;
}
return ans;
}
int fig_day(int year,int month){
switch(month){
case 1: return 31;
case 3: return 31;
case 4: return 30;
case 5: return 31;
case 6: return 30;
case 7: return 31;
case 8: return 31;
case 9: return 30;
case 10: return 31;
case 11:return 30;
case 12: return 31;
break;
}
if(month==2){
if(year%400==0||(year%4==0&&year%100!=0))
return 29;
else return 28;
}
return 0;
}
int main(){
int year,month,N;
int count,day;
while(cin>>year>>month>>N){
count=0;
day=fig_day(year,month);
count=(FigN(year,N)+FigN(month,N))*day;
for(int i=1;i<=day;i++){
count+=FigN(i,N);
}
if(N==0){
if(month<10){
count+=day+9;
}
else{
count+=9;
}
}
cout<<count<<endl;
}
return 0;
}
总结错误:
代码整体思路没错,但是在之前的版本中将闰年和平年的区别弄反,导致陷入谬误,闰年02月应该是29天而平年02月是28天;
在尝试一些测试案例之后,倘若与自己的人工思路测算结果一致,那么应该是代码和你的思路是一致的,因此还无法通过最终提交则应该是在自己的思路哪一环出现了逻辑上不符合事实的地方或者漏洞。漏洞还好一些,对测试集是敏感的,但是前者更加难以检查,一般情况下可以通过参考别人代码思路快速检查。
参考:https://blog.csdn.net/qq_43811879/article/details/104202117
原文链接: https://www.cnblogs.com/walter-mitty/p/17054438.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/312242
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!