每日一题<求1+2+3+...+n>

简介: 每日打卡

image.png

要求不用循环实现n的阶乘,这题目未免显得有些刁钻,但是突然想到可以使用类中的静态变量进行实现,问题就迎刃而解了。

class add
{
public:
    add()
    {
        ans+=i;
        i++;
    }
    static int i,ans;
};
int add::i = 1;      //外部对静态变量定义
int add::ans = 0;
class Solution {
public:
    int Sum_Solution(int n) {
     add A[n];   //调用n次构造函数
     return add::ans;
    }
};

image.gif

目录
相关文章
|
2月前
|
Python
每日一题 1447. 最简分数
每日一题 1447. 最简分数
|
3月前
每日一题——移动零
每日一题——移动零
【LeetCode】每日一题(3)
【LeetCode】每日一题(3)
49 0
【LeetCode】每日一题(2)
【LeetCode】每日一题(2)
51 0
|
存储 人工智能 BI
AcWing - 寒假每日一题2023(DAY 11——DAY 15)
AcWing - 寒假每日一题2023(DAY 11——DAY 15)
|
机器学习/深度学习 测试技术
AcWing - 寒假每日一题2023(DAY 16——DAY 20)
AcWing - 寒假每日一题2023(DAY 16——DAY 20)
|
人工智能 Java C++
AcWing - 寒假每日一题2023(DAY 1——DAY 5)
AcWing - 寒假每日一题2023(DAY 1——DAY 5)
|
存储 人工智能 算法
AcWing - 寒假每日一题2023(DAY 6——DAY 10)
AcWing - 寒假每日一题2023(DAY 6——DAY 10)
每日一题:Leetcode283 移动零
每日一题:Leetcode283 移动零
每日一题——后继者
每日一题——后继者
69 0
每日一题——后继者