【C语言】求s=a+aa+aaa+aaaa+aa...a的值

Doomsday_singer 2017-11-06 11:06:49
题目描述
求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个一位的整数。
例如2+22+222+2222+22222(此时共有5个数相加)
输入
整数a和n(n个数相加,1<= n, a<=9)
输出
s的值
样例输入
2 2
样例输出
24



我用一个for会写,试着用两个for写了下
写出来的是
int main(void)
{
int a,n,s,p,i,d;
scanf("%d %d",&a,&n);
s=0;
p=0;
for(i=1; i<=n; i=i+1)
{
for(d=1; d<=i; d=d+1)
{
s=s*10+a;
}
p=p+s;


}
printf("%d\n",p);
}

这样输入2 2的话得出的是224
实在是不知道哪里有问题,希望能有人指正一下,谢谢QAQ。
...全文
4729 13 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
拥抱Linux 2019-03-15
  • 打赏
  • 举报
回复

2 楼 正解。

2 楼 正解。
手工壁纸 2019-03-15
  • 打赏
  • 举报
回复
int a, b,c;

scanf_s("%d%d",&a,&c);
for (b = 0; b < c; b++)
{
a=(a * 10+a);
}

printf("%d", a);
赫凯 2019-03-15
  • 打赏
  • 举报
回复
//求和
int sum(int a, int n){
    int sum = 0, i = 1;
    //个位相加,十位相加,百位相加
    while (n) {
        sum += a*i*n;
        i *= 10;
        n--;
    } 
    return sum;
}
JackyRao 2017-11-11
  • 打赏
  • 举报
回复
s=0, 放到循环里面
赵4老师 2017-11-08
  • 打赏
  • 举报
回复
Format Specification Fields: scanf and wscanf Functions A format specification has the following form: %
  • [width] [{h | l | I64 | L}]type The format argument specifies the interpretation of the input and can contain one or more of the following: White-space characters: blank (' '); tab ('\t'); or newline ('\n'). A white-space character causes scanf to read, but not store, all consecutive white-space characters in the input up to the next non–white-space character. One white-space character in the format matches any number (including 0) and combination of white-space characters in the input. Non–white-space characters, except for the percent sign (%). A non–white-space character causes scanf to read, but not store, a matching non–white-space character. If the next character in stdin does not match, scanf terminates. Format specifications, introduced by the percent sign (%). A format specification causes scanf to read and convert characters in the input into values of a specified type. The value is assigned to an argument in the argument list. The format is read from left to right. Characters outside format specifications are expected to match the sequence of characters in stdin; the matching characters in stdin are scanned but not stored. If a character in stdin conflicts with the format specification, scanf terminates, and the character is left in stdin as if it had not been read. When the first format specification is encountered, the value of the first input field is converted according to this specification and stored in the location that is specified by the first argument. The second format specification causes the second input field to be converted and stored in the second argument, and so on through the end of the format string. An input field is defined as all characters up to the first white-space character (space, tab, or newline), or up to the first character that cannot be converted according to the format specification, or until the field width (if specified) is reached. If there are too many arguments for the given specifications, the extra arguments are evaluated but ignored. The results are unpredictable if there are not enough arguments for the format specification. Each field of the format specification is a single character or a number signifying a particular format option. The type character, which appears after the last optional format field, determines whether the input field is interpreted as a character, a string, or a number. The simplest format specification contains only the percent sign and a type character (for example, %s). If a percent sign (%) is followed by a character that has no meaning as a format-control character, that character and the following characters (up to the next percent sign) are treated as an ordinary sequence of characters, that is, a sequence of characters that must match the input. For example, to specify that a percent-sign character is to be input, use %%. An asterisk (*) following the percent sign suppresses assignment of the next input field, which is interpreted as a field of the specified type. The field is scanned but not stored.
Bambi_C 2017-11-08
  • 打赏
  • 举报
回复
空白字符会使scanf()函数在读操作中略去输入中的一个或多个空白字符,空白符可以是space,tab,newline等等,直到第一个非空白符出现为止
Bambi_C 2017-11-08
  • 打赏
  • 举报
回复
你的代码有问题 scanf 使用错误 两个%d之间不能有空格
自信男孩 2017-11-07
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <math.h>


int main(void)
{
#if 1

    int digit, num, s = 0, total = 0;
    int i, k = 1;
    scanf("%d%d", &digit, &num);

    if (num >= 10)
        return 0;

    while (k <= num) {
        for (i = 1; i < k; i++)
            s += digit * pow(10, i);
        s += digit;
        printf("s = %d\n", s);
        total += s;
        s = 0;
        k++;
    }
    printf("total = %d\n", total);

#else
    int a,n,s,p,i,d;
    scanf("%d %d",&a,&n);
    s=0;
    p=0;
    for(i=1; i<=n; i=i+1)
    {
        for(d=1; d<=i; d=d+1)
        {
            s=s*10+a;
        }
        p=p+s;


    }
    printf("%d\n",p);
#endif

    return 0;
}
参考一下吧
zhaozhejubenyan 2017-11-07
  • 打赏
  • 举报
回复
当i = 1时,内循环执行1次,s = 2,p = 2;当i = 2时,内循环执行2次,内循环第一次s = 2 * 10 + 2,s变为 22,内循环执行第二次s = 22 * 10 +2,s变为220。两次的和相加就是224. 如果在p = p + s后面写一句:s = 0,可以解决。
赵4老师 2017-11-07
  • 打赏
  • 举报
回复
“多一少一”问题占程序员常犯错误的10%以上! 避免“多一少一”问题的方法之一是将比如<10甚至<5的数代入程序片断,掰手指头心算验证一下程序到底应该写为 x、x-1、x+1中的哪个? <、<=、==、>、>=中的哪个?
destory27 2017-11-07
  • 打赏
  • 举报
回复
#include <stdio.h> #include <math.h> int main() { int a,n,sum,i; sum=0; i=1; scanf("%d,%d",&a,&n); while(i<=n) { sum=sum+a; a=a+a*(pow(10,i)); i++; } printf("%d\n",sum); return 0; }
zy35 2017-11-07
  • 打赏
  • 举报
回复
在for(d=1;d<=i;d++)s中,i=1执行一次,i=2是会执行两次,总的就会执行三次,故输出结果为224.
das白 2017-11-06
  • 打赏
  • 举报
回复
多循环了一下*10 d的循环有问题 建议调试

70,023

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧