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"); }
|