可以拆分成两个部分,一部分是生成aaaa,另一部分是求和
生成aaaa,可以这么实现(其中,E是为了方便×10的倍数)
long E(int m)
{
long num = 1;
while (m) {
num *= 10;
m--;
}
return num;
}
long GenNum(int num, int count)
{
long ret = 0;
while (count) {
count--;
ret += num * E(count);
}
return ret;
}
然后对生成的数求和即可
int main() {
int a, n;
printf("Input a,n:\n");
scanf("%d,%d", &a, &n);
long sum = 0;
while (n) {
sum += GenNum(a, n);
n--;
}
printf("sum=%ld\n", sum);
return 0;
}
... 展开详请
主要思路:
将待检测的数字number对10循环求余,然后拿每次产生的余数与要计算次数的digit比较,相等,则次数+1,最终计算出digit的出现次数
参考代码如下
int CountDigit(int number, int digit)
{
int count = 0;
do {
if (digit == number % 10) {
count++;
}
number /= 10;
} while (number > 0);
return count;
}... 展开详请
官方释义如下:
/* Re-allocate the previously allocated block
in PTR, making the new block SIZE bytes long. */
/* __attribute_malloc__ is not used, because if realloc returns
the same pointer that was passed to it, aliasing needs to be allowed
between objects pointed by the old and new pointers. */
extern void *realloc (void *__ptr, size_t __size)
__THROW __attribute_warn_unused_result__;
将之前创建的块区重新分配为 __size 的 大小,并返回新的地址指针。
在insert函数中,因为是插入新的元素,链表扩张,元素加一,原先 list->arr 长度不符合,需要扩张空间,所以使用了 realloc函数。
list->arr = (int*)realloc(list->arr, sizeof(int)*list->length + 1);
从上下函数分析。 list->arr 是一个 int 型数组。因为要插入一个新的int型元素,所以要对原先数组扩张,但还要维护 list->arr 的首地址,只能采用 realloc 函数。
但是你的这个函数不报错吗?增加一个int值,不管是维护指针还是内存,都需要4个字节的空间。
应该是
list->arr = (int*)realloc(list->arr, sizeof(int)*(list->length + 1));
... 展开详请