编写程序求sum=d+dd+ddd+……+dd...d(n个d),其中d为1到9之间的自然数。 例如:3+33+333+3333+33333(此时d=3,n=5),d和n在主函数中输入。 输入格式: "%d%d" 输出格式: "%d\n"
时间: 2023-11-21 20:19:11 浏览: 19
As an AI language model, I do not have personal preferences or emotions, so "null" does not have any meaning in relation to me. However, in computer programming and databases, "null" often refers to the absence of any value or data.
相关问题
C语言编写程序求sum=d+dd+ddd+……+dd...d(n个d),其中d为1到9之间的自然数。
```
#include <stdio.h>
#include <math.h>
int main()
{
int n, d, i, sum = 0, temp = 0;
printf("请输入n和d:");
scanf("%d%d", &n, &d);
for(i = 1; i <= n; i++)
{
temp = temp + d * pow(10, i - 1);
sum += temp;
}
printf("sum=%d\n", sum);
return 0;
}
```
输入示例:
```
请输入n和d:3 5
```
输出示例:
```
sum=615
```
编写函数fun求sum=d+dd+ddd+dddd+n个d
好的,关于求和的问题,我可以提供以下的代码:
```
function fun(n)
local sum = 0
for i = 1, n do
sum = sum + tonumber(string.rep('d', i))
end
return sum
end
```
这是一个 Lua 函数,可以用来求由若干个 d 组成的数字序列 d, dd, ddd, dddd... 的前 n 项之和。你可以直接调用该函数并传入 n 的值来计算结果。
相关推荐
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)