题目:
假设第一天量产1台,接下来2天(即第二、三天)每天量产2件,接下来3天(即第四、五、六天)每天量产3件 … …
以此类推,请编程计算出第n天总共可以量产的手机数量。
输入例子1:
11
输出例子1:
35
思路:
用一层for循环遍历天数,一层for循环遍历能够增加的次数,当i超出了规定天数,就返回对应的总和。
代码:
int solution(int n) {
// write code here
int sum=0,t=1;
for(int i=1;i<=n;)
{
for(int j=1;j<=t;j++)
{
sum+=t;
i++;
if(i>n)
{
return sum;
}
}
t++;
}
return sum;
}
};
笔试题8-数位之积
https://blog.csdn.net/weixin_44370808/article/details/106525886