user-0 提问: 【C语言】求s=a+aa+aaa+aaaa+aa...a的值

回复:
user: user-1
content: 2 楼  正解。  2 楼  正解。

user: user-2
content: int a, b,c;  	scanf_s("%d%d",&a,&c); 	for (b = 0; b < c; b++) 	{ 		a=(a * 10+a); 	}  	printf("%d", a);

user: user-3
content: //求和 int sum(int a, int n){     int sum = 0, i = 1;     //个位相加，十位相加，百位相加     while (n) {         sum += a*i*n;         i *= 10;         n--;     }      return sum; }

user: user-4
content: s=0, 放到循环里面

user: user-5
content: 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.

user: user-6
content: 空白字符会使scanf()函数在读操作中略去输入中的一个或多个空白字符，空白符可以是space,tab,newline等等，直到第一个非空白符出现为止

user: user-6
content: 你的代码有问题 scanf 使用错误 两个%d之间不能有空格

user: user-7
content: #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; }  参考一下吧

user: user-8
content: 当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，可以解决。

user: user-5
content: “多一少一”问题占程序员常犯错误的10%以上！ 避免“多一少一”问题的方法之一是将比如<10甚至<5的数代入程序片断，掰手指头心算验证一下程序到底应该写为 x、x-1、x+1中的哪个？ <、<=、==、>、>=中的哪个？

user: user-9
content: #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; }

user: user-10
content: 在for(d=1;d<=i;d++)s中，i=1执行一次，i=2是会执行两次，总的就会执行三次，故输出结果为224.

user: user-11
content: 多循环了一下*10  d的循环有问题 建议调试