当前位置:网站首页>C#求完全数,输出水仙花以及类的使用
C#求完全数,输出水仙花以及类的使用
2022-07-16 13:18:00 【南蓬幽】
完全数
如果一个正整数等于除它本身之外其他所有除数之和,就称之为完全数。
完全数
for(int i = 1; i < 1000; i++)
{
int sum = 0;
for(int j = 1; j < i; j++)
{
if (i % j == 0)
{
sum += j;
}
}
if (sum == i)
{
Console.WriteLine(i);
}
代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp3
{
class Program
{
static void Main(string[] args)
{
for(int i = 1; i < 1000; i++)
{
int sum = 0;
for(int j = 1; j < i; j++)
{
if (i % j == 0)
{
sum += j;
}
}
if (sum == i)
{
Console.WriteLine(i);
}
}
Console.ReadKey();
}
}
}
结果

以前以内的完全数 6 28 496
输出所有水仙花数
水仙花数是三位数的数,其各位数字的立方和等于该数的本身
水仙花
int a, b, c;
for(int i = 100; i < 1000; i++)
{
a = i / 100;
b = i / 10 %10;
c = i % 10;
if (a * a * a + b * b * b + c * c * c == i)
{
Console.WriteLine(i);
}
代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp4
{
class Program
{
static void Main(string[] args)
{
int a, b, c;
for(int i = 100; i < 1000; i++)
{
a = i / 100;
b = i / 10 %10;
c = i % 10;
if (a * a * a + b * b * b + c * c * c == i)
{
Console.WriteLine(i);
}
}
Console.ReadKey();
}
}
}
结果

水仙花数为 153 370 371 407
类的使用
代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp6
{
class Student
{
public string name;
public string sex;
public int age;
public Student(string name, string sex)
{
}
public Student(string name, string sex,int age)
{
this.name = name;
this.sex = sex;
this.age = age;
}
public void Inderduce()
{
Console.WriteLine("我叫"+this.name+"我是一个"+this.sex+"孩,我今年"+this.age+"岁");
}
}
class Program
{
static void Main(string[] args)
{
Student s1 = new Student("小华","男",20);
Student s2 = new Student("小花","女",18);
s1.Inderduce();
s2.Inderduce();
Console.ReadKey();
}
}
}
输出结果

边栏推荐
猜你喜欢

【开发教程7】疯壳·开源蓝牙智能健康手表-充电

Qt Creator调试模式断点不起作用 MinCW可以

才22岁!这位“00后”博士拟任职985高校!

【技术碎片】基于指数扩散二分搜索的重名文件重命名后缀

【白盒测试】逻辑覆盖和路径测试的设计方法

This article takes you to graph transformers

ECCV 2022 | open source for generative knowledge distillation of classification, detection and segmentation

Export data with navicatpremium

(pytorch进阶之路五)RNN/LSTM/LSTMP/GRU

New exploration of Ali mother's display advertising engine: towards the overall optimal allocation of computing power
随机推荐
[I2C of Renesas ra6m4 development board reads bmp180 air pressure and temperature]
ECCV 2022 | open source for generative knowledge distillation of classification, detection and segmentation
Pay equal attention to quality and efficiency, test left shift power block storage technology research and development
七月集训(第16天) —— 队列
质量与效率并重,测试左移助力块存储技术研发
26 top open source projects, 87 open tasks, Alibaba programming summer 2022 student registration channel opened
【开发教程7】疯壳·开源蓝牙智能健康手表-充电
What should I do if I can't see any tiles on SAP Fiori launchpad?
Today's sleep quality record 85 points
达梦数据库表SQL语句
If an element is above another element, clicking on the above element will trigger the following element event operation
【SQL注入】order by 注入:联合盲注、报错、堆叠注入
将c语言文件打包成exe可执行程序
PICASSO,一个高效的搜推广稀疏训练解决方案
老板想要的简单方案 vs. 程序员理解的需求 | 漫画
[动态规划]DP20 计算字符串的编辑距离-中等
Export data with navicatpremium
(pytorch进阶之路四)Vision Transformer
What is packet loss and why
随心玩玩(八)jenkins学习(待更新)