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

目录
相关文章
|
3月前
|
索引
leetcode每日一题刷题打卡1700
leetcode每日一题刷题打卡1700
23 0
|
3月前
每日一题——移动零
每日一题——移动零
|
9月前
【LeetCode】每日一题(3)
【LeetCode】每日一题(3)
33 0
|
4月前
|
算法 C语言 索引
每日一题:LeetCode-283. 移动零
每日一题:LeetCode-283. 移动零
|
9月前
【LeetCode】每日一题(2)
【LeetCode】每日一题(2)
41 0
|
9月前
|
算法
【LeetCode】每日一题(1)
【LeetCode】每日一题(1)
49 0
|
9月前
【LeetCode】每日一题(5)
【LeetCode】每日一题(5)
30 0
每日一题——后继者
每日一题——后继者
58 0
每日一题——后继者
|
算法
LeetCode每日一题(24)——后继者
后继者 1.题目 2.示例 3.思路 4.代码
LeetCode每日一题——944. 删列造序
给你由 n 个小写字母字符串组成的数组 strs,其中每个字符串长度相等。
70 0

热门文章

最新文章