关于521
时间限制:1000 ms | 内存限制:65535 KB 难度:2- 描述
-
Acm队的流年对数学的研究不是很透彻,但是固执的他还是想一头扎进去。
浏览网页的流年忽然看到了网上有人用玫瑰花瓣拼成了521三个数字,顿时觉得好浪漫,因为每个男生都会不经意的成为浪漫的制造者。此后,流年走到哪里都能看到5、2、1三个数字,他怒了,现在他想知道在连续的数中有多少数全部包含了这三个数字。例如12356就算一个,而5111就不算。特别的,如果他看到了521三个数连续出现,会特别的愤怒。例如35210。
- 输入
-
多组测试数据:
一行给定两个数a,b(0 输出 - 一行显示他想要知道的数有几个及显示有多少个数字令他特别的愤怒。用空格隔开。
- 样例输入
-
200 500 300 900 1 600
- 样例输出
-
Case 1:2 0 Case 2:2 1 Case 3:6 1
开始一直TLE,后来看了别人的代码,才发现应该先范围内的数都判断一遍,这样就不用每次都判断,直接在存储的数组中找就可以了哈!
超时代码如下
#includeusing std::endl; using std::cout; using std::cin; int main() { int a , b; int cnt = 0; while(cin >> a >> b) { cnt++; int s[6]; int cnt1 = 0 , cnt2 = 0; for(int j=a; j<=b;++j) { int five=0 , two = 0, one = 0; s[0] = j % 10; s[1] = j / 10 % 10; s[2] = j / 100 % 10; s[3] = j / 1000 % 10; s[4] = j / 10000 % 10; s[5] = j / 100000 % 10; for(int i=0 ; i<4;++i) { if(s[i] == 1 && s[i+1] == 2 && s[i+2] == 5) { cnt2++; break; } } for(int i=0 ; i<6; ++i) { if(s[i] == 5) { five++; continue; } if(s[i] == 2) { two++; continue; } if(s[i] == 1) { one++; continue; } } if(five && two && one) cnt1++; } cout << "Case " < 修改后AC的代码
#include#include using std::endl; using std::cout; using std::cin; int main() { int data1[1000000] , data2[1000000]; int s[6]; //把范围之内的数全都模拟一次,累积符合条件的次数放到数组中 for(int j=125; j<1000000; ++j) { int five = 0 , two =0 , one = 0; s[0] = j % 10; s[1] = j / 10 % 10; s[2] = j / 100 % 10; s[3] = j / 1000 % 10; s[4] = j / 10000 % 10; s[5] = j / 100000 % 10; int temp; for(int i=0; i<4;++i) { if(s[i] == 1 && s[i+1] == 2 && s[i+2] == 5) { data2[j] = data2[j-1] +1; break; } //如果不符合条件 if(i == 3) { data2[j] = data2[j-1]; } } for(int i=0; i<6;++i) { if(s[i] == 5) { five++; }else if(s[i] == 2) { two++; }else if(s[i] == 1) one++; } //判断是否符合条件 if(five && two && one) data1[j] = data1[j-1]+1; else data1[j] = data1[j-1]; } int a,b; int cnt = 0; while(cin >> a >> b) { cnt++; if(a <2) a=2; //直接在数组中取值,二者相减就是两个数之间符合条件的个数,注意一定是第一位数的前一位 cout << "Case " << cnt <<":" << data1[b] - data1[a-1] <<" " < NYOJ上最优代码(短小精悍,果然牛逼哈)
#includestruct AC { int x,y; }a[1000004]; int main() { int i,j,k=0;a[125].x=1,a[521].y=1; for(i=0; i<1000003; i++) { int c[3]={0}; if(i%10==5||i%100/10==5||i%1000/100==5||i%10000/1000==5||i%100000/10000==5||i%1000000/100000==5) c[2]=1; if(i%10==2||i%100/10==2||i%1000/100==2||i%10000/1000==2||i%100000/10000==2||i%1000000/100000==2) c[1]=1; if(i%10==1||i%100/10==1||i%1000/100==1||i%10000/1000==1||i%100000/10000==1||i%1000000/100000==1) c[0]=1; if(c[0]&&c[1]&&c[2]) a[i].x=a[i-1].x+1; else a[i].x=a[i-1].x; if(i%1000==521||i%10000/10==521||i%100000/100==521||i%1000000/1000==521) a[i].y=a[i-1].y+1; else a[i].y=a[i-1].y; }//printf("%d",l); while(~scanf("%d %d",&i,&j)) {int m; k++; printf("Case %d:%d %d\n",k,a[j].x-a[i-1].x,a[j].y-a[i-1].y); } }
- <script type="text/java script">BAIDU_CLB_fillSlot("771048");
- 点击复制链接 与好友分享! 回本站首页 <script> function copyToClipBoard(){ var clipBoardContent=document.title + '\r\n' + document.location; clipBoardContent+='\r\n'; window.clipboardData.setData("Text",clipBoardContent); alert("恭喜您!复制成功"); }
<script type="text/java script" id="bdshare_js" data="type=tools&uid=12732"> <script type="text/java script" id="bdshell_js"> <script type="text/java script"> var bds_config = {'snsKey':{'tsina':'2386826374','tqq':'5e544a8fdea646c5a5f3967871346eb8'}}; document.getElementById("bdshell_js").src = "http://bdimg.share.baidu.com/static/js/shell_v2.js cdnversion=" + Math.ceil(new Date()/3600000) - 您对本文章有什么意见或着疑问吗?请到 论坛讨论您的关注和建议是我们前行的参考和动力
- 上一篇: [Leetcode]Roman to Integer
- 下一篇: 最后一页
- 相关文章
- <script type="text/java script">BAIDU_CLB_fillSlot("182716");
- <script type="text/java script">BAIDU_CLB_fillSlot("517916");
- 图文推荐
<iframe src="http://www.2cto.com/uapi.php tid=288243&catid=339&title=TllPSi0tudjT2jUyMQ==&forward=http://www.2cto.com/kf/201403/288243.html" width="100%" height="100%" id="comment_iframe" name="comment_iframe" frameborder="0" scrolling="no">
- <script type="text/java script">BAIDU_CLB_fillSlot("771057");



