每日一题<求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

目录
相关文章
|
4月前
|
索引
leetcode每日一题刷题打卡1700
leetcode每日一题刷题打卡1700
35 0
|
3月前
|
Python
每日一题 1447. 最简分数
每日一题 1447. 最简分数
|
4月前
每日一题——移动零
每日一题——移动零
|
9月前
|
算法 C语言 索引
每日一题:LeetCode-283. 移动零
每日一题:LeetCode-283. 移动零
【LeetCode】每日一题(2)
【LeetCode】每日一题(2)
56 0
【LeetCode】每日一题(5)
【LeetCode】每日一题(5)
45 0
每日一题:Leetcode283 移动零
每日一题:Leetcode283 移动零
每日一题——后继者
每日一题——后继者
74 0
每日一题——后继者
LeetCode每日一题(21)——删列造序
删列造序 1.题目 2.示例 3.思路 4.代码
|
人工智能 算法 物联网
每日一题(day1)
每日一题(day1)
163 2
每日一题(day1)