non-random numbers

探讨一种特殊规则下的随机数生成问题,即n位数中任意一位不能等于其位置值。文章介绍了一个程序解决方案,能够计算符合该规则的不同n位数的数量。
Vasya is a schoolboy who was playing around with a random number generator and 
noticed that it never generates numbers with the value of a specific digit equal to the 
position of that digit in the number. 
Vasya became curious and he came to discover the following:
? The input accepted by the generator is one positive integer n – the 
number of digits in the generated random number.
? The output is a positive integer number consisting of n digits without 
leading zeroes.
? In the generated number at i position (from the left-hand side) cannot be 
digit i.
For example, if we want the generator to produce a single-digit number, it will 
generate any single-digit number except 0 or 1. In case with a double-digit number, 
the output will be anything except 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 22, 32, 42, 
52, 62, 72, 82 or 92.
The schoolboy decided to find out how many different numbers can be generated for 
any given n-digit number. 
Your task is to write a program that will help the schoolboy to solve this problem.
Limitations
1 ≤ n ≤ 100.
Input
The input file contains single integer n – the number of digits in the generated 
random number.
Output
The output file must contain one single integer – the number of possible random n-
digit numbers. The output must have no leading zeroes.
Examples
Input.txt Output.txt
1 8
2 72

12 344373768000


问n位数中第x位不等于x的数的个数


思路:

当n前10时直接相乘

例如8*9*9*9*9....

当n大于9时直接乘10直接加0就可以了

ac代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

int main()
{
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    long long x;
    long long y;
    while(scanf("%lld",&y)!=-1)
    {
        if(y<10)
        {
            x=8;
            for(int i=1;i<y;i++)
            x=x*9;
            printf("%lld\n",x);
        }
        else
        {
            printf("344373768");
            for(int i=1;i<=y-9;i++)
            printf("0");
            printf("\n");
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值