给定正整数N,函数F(N)表示小于等于N的自然数中1和2的个数之和,例如:1,2,3,4,5,6,7,8,9,10序列中1和2的个数之和为3,因此 F(10)=3。输入N,求F(N)的值,1=<N<=

发布于:2022-11-16 ⋅ 阅读:(21) ⋅ 点赞:(0) ⋅ 评论:(0)

函数求值:给定正整数N,函数F(N)表示小于等于N的自然数中1和2的个数之和,例如:1,2,3,4,5,6,7,8,9,10序列中1和2的个数之和为3,因此 F(10)=3。输入N,求F(N)的值,1=<N<=10^100(10的100次方)若F(N)很大,则求F(N)mod20123的值。

http://t.csdn.cn/omEZ3《===这个作者详细介绍了该题的一种做法,我也是在看了思路后才写出自己的程序。建议先进入该作者文章中去理解思路然后再看代码。作为一个编程小白,我的代码完全基于目前所学,所以可能不会很简便。

代码如下:

#include<stdio.h>
#include<math.h>
        int ge[1000],count[1000],ge1[1000],count1[1000];
        int main()
{
        int i,i1,n1,n;
        int N,M,N1,M1;
        scanf("%d",&N);
        scanf("%d",&N1);
            M = N;
            n = 0;
            while (M / 10 != 0)/*计算这个数有几次方*/
            {
                M = M / 10;
                n += 1;
            }
            for (i = 0; i <= n; i++)/*从低位到高位把个位数存入数组*/
            {
                ge[i] = (N % (int) (pow(10.0, (double) (i + 1)))) / (int) (pow(10.0, (double) i));
            }
            for (i = 0; i <= n; i++)/*将N中的每一位数从低位到高位存入count数组,方便后面判断N中1和2有多少个*/
            {
                count[i] = (N / (int) (pow(10.0, (double) (i)))) % 10;
            }
            int number, tem, b, num;
            tem = 0;
            number = count[n];
            if (count[n] == 1 || count[n] == 2) {
                tem += 1;
            }
            num = tem;
            for (i = 1; i <= n; i++) {
                number = number * (int) pow(10.0, (double) i) + ge[n - i];
                if (count[n - i] == 1 || count[n - i] == 2) {
                    tem += 1;
                }
                if (count[n - i] > 2) {
                    b = 2;
                } else {
                    b = count[n - i];
                }
                num = ((num - tem) * 10 + number / 10 * 2 + tem * (count[n - i] + 1) + b);
            }

    printf("%d\n", num);
            M1 = N1;
            n1 = 0;
            while (M1 / 10 != 0)/*计算这个数有几次方*/
            {
                M1 = M1 / 10;
                n1 += 1;
            }
            for (i1 = 0; i1 <= n1; i1++)/*从低位到高位把个位数存入数组*/
            {
                ge[i1] = (N1 % (int) (pow(10.0, (double) (i1 + 1)))) / (int) (pow(10.0, (double) i1));
            }
            for (i1 = 0; i1 <= n1; i1++)/*将N中的每一位数从低位到高位存入count数组,方便后面判断N中1和2有多少个*/
            {
                count1[i1] = (N1 / (int) (pow(10.0, (double) (i1)))) % 10;
            }
            int number1, tem1, b1, num1;
            tem1 = 0;
            number1 = count1[n1];
            if (count1[n1] == 1 || count1[n1] == 2) {
                tem1 += 1;
            }
            num1 = tem1;
            for (i1 = 1; i1 <= n1; i1++) {
                number1 = number1 * (int) pow(10.0, (double) i1) + ge1[n1 - i1];
                if (count1[n1 - i1] == 1 || count1[n1 - i1] == 2) {
                    tem1 += 1;
                }
                if (count1[n1 - i1] > 2) {
                    b1 = 2;
                } else {
                    b1 = count1[n1 - i1];
                }
                num1 = ((num1 - tem1) * 10 + number1 / 10 * 2 + tem1 * (count1[n1 - i1] + 1) + b1) % 20123;
            }
            printf("%d", num1);
return 0;
}