上善若水

  C++博客 :: 首页 :: 联系 :: 聚合  :: 管理
  2 Posts :: 32 Stories :: 2 Comments :: 0 Trackbacks

常用链接

留言簿

我参与的团队

最新随笔

搜索

  •  

积分与排名

  • 积分 - 10174
  • 排名 - 1166

最新评论

阅读排行榜

评论排行榜

Cow Counting

时间限制(普通/Java):1000MS/10000MS          运行内存限制:65536KByte
总提交:823            测试通过:288

描述

Farmer John wants to label his N (1 <= N <= 1,000,000) cows, but cows don't like having the digit L (0 <= L <= 9) written on them, so he avoids that.

If the cows are labeled with the smallest set of N positive integers that don't have the digit L, what is the largest number that Farmer John has to write on any cow?

输入

* Line 1: Two space-separated integers: N and L.

输出

* Line 1: A single integer that is the largest number that Farmer John has to write on any cow.

样例输入

10 1

样例输出

22

题目来源

Elite 2007 US Open Competition

分析:十进制变九进制,唯一挠头的就是L为0的时候,因为数数遇九不进1。太恶心了写的挺傻。
#include <stdio.h>
#include 
<stack>
using namespace std;
int main()
{
    
int n,k,l,m,i,j,v[10]={0,1,2,3,4,5,6,7,8,9};
    stack
<int>s;
    scanf(
"%d%d",&n,&l);
    
if (l!=0)
    
{
        
for (i=l;i<9;i++)
        
{
            v[i]
=v[i+1];
        }

        
while (n)
        
{
            s.push(n
%9);
            n
/=9;
        }

    }

    
else
    
{
        v[
0]=9;
        
while (n)
        
{
            
if (n%9!=0)
            
{
                s.push(n
%9);
                n
/=9;
            }

            
else if (n%9==0)
            
{
                s.push(n
%9);
                n
=(n-1)/9;
            }
        
        }

    }
    
    
while (!s.empty())
    
{
        printf(
"%d",v[s.top()]);
        s.pop();
    }

    printf(
"\n");
}
posted on 2009-11-24 22:23 上善若水 阅读(315) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理