数列求和-加强版(20 分) C

习题2.3 数列求和-加强版 (20 分)

给定某数字A(1≤A≤9)以及非负整数N(0≤N≤100000),求数列之和S=A+AA+AAA+?+AA?A(N个A)。例如A=1, N=3时,S=1+11+111=123。

输入格式:

输入数字A与非负整数N。

输出格式:

输出其N项数列之和S的值。

输入样例:

1 3

输出样例:

123

#include <stdio.h>

typedef int ElementType;
typedef int Position;
typedef int Count;

struct Int{
    ElementType Digit[100000];
    Position Last;
};

typedef struct Int Integer;

int main(void)
{
    ElementType num;
    Count n;
    Integer a = {0};

    a.Last=0;
    scanf("%d %d", &num, &n);

    ElementType temp;
    for(Count i=1;i<=n;i++){
        if(i-1>a.Last)
            a.Last++;
        a.Digit[i-1] += (n-i+1)*num+temp;
        if(n!=i){
            temp = a.Digit[i-1]/10;
            a.Digit[i-1] %= 10;
        }
    }

    for(Position i=a.Last;i>=0;i--)
        printf("%d", a.Digit[i]);
    printf("\n");

    return 0;
}

原文地址:https://www.cnblogs.com/nonlinearthink/p/10901659.html

时间: 2024-07-31 04:56:41

数列求和-加强版(20 分) C的相关文章

PAT 数列求和-加强版&#160;&#160;&#160;(20分)(简单模拟)

给定某数字A(1≤A≤9)以及非负整数N(0≤N≤100000),求数列之和S=A+AA+AAA+?+AA?A(N个A).例如A=1, N=3时,S=1+11+111=123 输入格式: 输入数字A与非负整数N. 输出格式: 输出其N项数列之和S的值. 输入样例: 1 3 输出样例: 123 1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<cmath> 5 #i

习题4-4 特殊a串数列求和(20 分)

给定两个均不超过9的正整数a和n,要求编写程序求a+aa+aaa++?+aa?a(n个a)之和. 输入格式: 输入在一行中给出不超过9的正整数a和n. 输出格式: 在一行中按照"s = 对应的和"的格式输出. 输入样例: 2 3 输出样例: s = 246 #include <stdio.h> int main() { int i,a,n,sum=0,t=0; scanf("%d %d", &a, &n); for(i=1;i<=n

7-38 数列求和-加强版

7-38 数列求和-加强版(20 分) 给定某数字A(1≤A≤9)以及非负整数N(0≤N≤100000),求数列之和S=A+AA+AAA+?+AA?A(N个A).例如A=1, N=3时,S=1+11+111=123. 输入格式: 输入数字A与非负整数N. 输出格式: 输出其N项数列之和S的值. 输入样例: 1 3 输出样例: 123 思路:直接用长整形累加代码简单,但是超范围了(:′⌒`),本来还试了下使用数组来进行发现还是没AC.然后换了种思路横着一个个加不行,那纵着加呢?(横纵参考下图)很完

2-06. 数列求和(20)(ZJUPAT 数学)

题目链接:http://pat.zju.edu.cn/contests/ds/2-06 给定某数字A(1<=A<=9)以及非负整数N(0<=N<=100000),求数列之和S = A + AA + AAA + - + AA-A(N个A).例如A=1, N=3时,S = 1 + 11 + 111 = 123. 输入格式说明: 输入数字A与非负整数N. 输出格式说明: 输出其N项数列之和S的值. 样例输入与输出: 序号 输入 输出 1 1 3 123 2 6 100 740740740

PAT 2-06. 数列求和(20)

题目意思:给定某数字A(1<=A<=9)以及非负整数N(0<=N<=100000),求数列之和S = A + AA + AAA + … + AA…A(N个A) 最开始一想还以为是大数相加,但是想了一下,感觉有规律. 解题思路:n个a相加除以10的余数为当前位的结果,商为新的进位 ,由此可以得到结果的每一位 代码如下(带注释): #include<iostream> using namespace std; int a,n; int sum[100005],k=0;//s

[PAT]数列求和(20)

#include "stdio.h" #include "malloc.h" #include "math.h" void calc(int num,int N,long *data); int main() { int N,num; long *result; scanf("%d",&num); scanf("%d",&N); result=(long *)malloc(N*sizeof(

1049 数列的片段和 (20 分)

1049 数列的片段和 (20 分) 给定一个正数数列,我们可以从中截取任意的连续的几个数,称为片段.例如,给定数列 { 0.1, 0.2, 0.3, 0.4 },我们有 (0.1) (0.1, 0.2) (0.1, 0.2, 0.3) (0.1, 0.2, 0.3, 0.4) (0.2) (0.2, 0.3) (0.2, 0.3, 0.4) (0.3) (0.3, 0.4) (0.4) 这 10 个片段. 给定正整数数列,求出全部片段包含的所有的数之和.如本例中 10 个片段总和是 0.1 +

PTA乙级 (1049 数列的片段和 (20分))

1049 数列的片段和 (20分) https://pintia.cn/problem-sets/994805260223102976/problems/994805275792359424 第一次提交: 代码: #include <cstdio> #include <iostream> #include <cstring> #include <string> #include <cmath> #include <algorithm>

UVa 1363 (数论 数列求和) Joseph&#39;s Problem

题意: 给出n, k,求 分析: 假设,则k mod (i+1) = k - (i+1)*p = k - i*p - p = k mod i - p 则对于某个区间,i∈[l, r],k/i的整数部分p相同,则其余数成等差数列,公差为-p 然后我想到了做莫比乌斯反演时候有个分块加速,在区间[i, n / (n / i)],n/i的整数部分相同,于是有了这份代码. 1 #include <cstdio> 2 #include <algorithm> 3 using namespace