当前位置:网站首页>C method question 2
C method question 2
2022-07-07 23:34:00 【Bobo in summer】
13 Write a method , Fractional sequence :2/1,1/3,3/4,4/7,7/11,11/18… Before 10 Sum of items , And back to . Require application writing , To test the correctness of the method .
public static double summ()
{
double sum = 0;
double a = 2;
double b = 1;
double temp;
for (int i = 0;i<10;i++)
{
sum += a / b;
temp = b;
b = a + b;
a = temp;
}
return sum;
}
Console.WriteLine(summ());
14. Write a method , seek 1+1/2!+1/3!+…+1/n! And , And return the sum as the return value , Require application writing , To test the correctness of the method .
public static double summ(double a)
{
double sum = 0;
double b = 1;
double temp = 0;
for(int i = 1;i<=a;i++)
{
b = b * i;
temp = 1 / b;
sum += temp;
}
return sum;
}
double aaa =double.Parse( Console.ReadLine());
Console.WriteLine(summ(aaa));
15. Write a method , Yes 4 Bit integer encryption , The encryption rules are as follows : Add... To every number 7, Then divide the sum by 10 The remainder of replace the number , Then exchange the first and second positions , The fourth and third bits are exchanged , This method returns the encrypted number . Require application writing , To test the correctness of the method .
public static int JiaMi(int n)
{
int mima = 0;
int gw = (n % 10 + 7) % 10;
int sw = (n / 10 % 10 + 7) % 10;
int bw = (n / 100 % 10 + 7) % 10;
int qw = (n / 1000 + 7) % 10;
mima = bw * 1000 + qw * 100 + gw * 10 + sw;
return mima;
}
public static void Main(string[] args)
{
Console.WriteLine(" Please enter a four digit password :");
int nnn = int.Parse(Console.ReadLine());
Console.WriteLine(" The result after encryption is :");
Console.WriteLine(JiaMi(nnn));
}
17. Write a method , Find the longest word in a string , Words are separated by spaces , And return the longest word as the method return value . Require application writing , To test the correctness of the method .
public static string MaxWord(string ss)
{
string s = string.Empty;
string[] words = new string[100];
words = ss.Split(' ');
for (int i = 0; i < words.Length; i++)
{
if (s.Length < words[i].Length)
{
s = words[i];
}
}
return s;
}
public static void Main(string[] args)
{
Console.WriteLine(" Please enter a string :");
string st = Console.ReadLine();
Console.WriteLine(" The longest word is :");
Console.WriteLine(MaxWord(st));
Console.ReadLine();
}
18. Write a method , For a given date , Return the day of the week . for example 2002-3-28 Back to Thursday . Require application writing , To test the correctness of the method .
public static void Rdate(string date)
{
string weekst = DateTime.Parse(date).DayOfWeek.ToString();
switch (weekst)
{
case "Monday": weekst = " Monday "; break;
case "Tuesday": weekst = " Tuesday "; break;
case "Wednesday": weekst = " Wednesday "; break;
case "Thursday": weekst = " Thursday "; break;
case "Friday": weekst = " Friday "; break;
case "Saturday": weekst = " Saturday "; break;
case "Sunday": weekst = " Sunday "; break;
default: Console.WriteLine(" The input data is illegal "); break;
}
Console.WriteLine(weekst);
}
string datetimes = Console.ReadLine();
Rdate(datetimes);
19. Write a method , Randomly generated 10 individual [20,50] The positive integer of is stored in the array , And output the maximum value of all elements in the array 、 minimum value 、 Average value and sum of all elements . Require application writing , To test the correctness of the method .
public static void MaxMinAvg(out int max, out int min, out double avg, out int sum)
{
Random ran = new Random();
int[] arr = new int[10];
max = 0;
min = 50;
sum = 0;
Console.WriteLine(" produce 10 individual [20,50] The random number sequence between is :");
for (int i = 0; i < arr.Length; i++)
{
arr[i] = ran.Next(20, 51);
sum += arr[i];
if (arr[i] < min)
{
min = arr[i];
}
if (arr[i] > max)
{
max = arr[i];
}
Console.Write("{0} ", arr[i]);
}
avg = sum / 10;
}
public static void Main(string[] args)
{
int max;
int min;
double avg;
int sum;
MaxMinAvg(out max, out min, out avg, out sum);
Console.WriteLine(" Maximum :{0}, minimum value :{1}, Average :{2}, and :{3}", max, min, avg, sum);
Console.ReadLine();
}
20. It is known that the first two terms of a sequence are 1,2, The following items are the sum of the first two adjacent items , Write a method , Calculate and return to the front of the sequence n Sum of square roots of terms sum. Require application writing , To test the correctness of the method .
public static double nSum(int n)
{
int[] arr = new int[n];
arr[0] = 1;
arr[1] = 2;
double sum = 0;
for(int i = 2; i <arr.Length; i++)
{
arr[i] = arr[i - 1] + arr[i - 2];
}
for(int i = 0; i < arr.Length; i++)
{
sum += Math.Sqrt(arr[i]);
}
return sum;
}
Console.WriteLine(" Please input before n term n Value :");
int n = int.Parse(Console.ReadLine());
Console.WriteLine( nSum(n));
Console.ReadLine();
21. Write a method , Judge whether a number can be 3 Divide but not be 7 to be divisible by , Write applications , Output 1-100 All within can be 3 Divide but not be 7 Divisible number . Require application writing , To test the correctness of the method .
public static void Jisuan(int a)
{
if (a % 3 == 0 && (a % 7) != 0)
{
Console.WriteLine(" Sure ");
}
else
{
Console.WriteLine(" no way ");
}
Console.WriteLine("1-100 There are these :");
for (int i = 1;i<=100;i++)
{
if (i % 3 == 0 && (i % 7)!=0)
{
Console.WriteLine(i);
}
}
}
Console.WriteLine(" Please enter :");
int aaa = int.Parse(Console.ReadLine());
Jisuan(aaa);
22. Write a method , Calculation 1 To n Sum the squares of all numbers between , Require application writing , To test the correctness of the method .
public static double summ(int a)
{
double b = 0;
double c = 0;
for (int i = 1;i<=a;i++)
{
c = i * i;
b += c;
}
return b;
}
int aaa = int.Parse(Console.ReadLine());
Console.WriteLine(summ(aaa));
23. Write a method , Judge whether a three digit number is equal to the cubic sum of each digit , for example 153=13+53+33, Require application writing , To test the correctness of the method . Require application writing , To test the correctness of the method .
public static void lifanghe(int a)
{
if (((a / 100)* (a / 100)* (a / 100) + (a % 100 / 10)* (a % 100 / 10)* (a % 100 / 10) + (a % 10)* (a % 10)* (a % 10)) == a)
{
Console.WriteLine(" accord with ");
}
else
{
Console.WriteLine(" Do not conform to the ");
}
}
int aaa =int.Parse( Console.ReadLine());
lifanghe(aaa);
}
边栏推荐
- In the field of software engineering, we have been doing scientific research for ten years!
- [stm32+esp8266 connect Tencent cloud IOT development platform 2] stm32+esp8266-01s connect Tencent cloud
- 欢聚时代一面
- 产业共融新势能,城链科技数字峰会厦门站成功举办
- RE1 attack and defense world reverse
- Force deduction solution summary 648 word replacement
- USB (XVIII) 2022-04-17
- 生鲜行业数字化采购管理系统:助力生鲜企业解决采购难题,全程线上化采购执行
- Unity3d learning notes 4 - create mesh advanced interface
- B_ QuRT_ User_ Guide(37)
猜你喜欢
在软件工程领域,搞科研的这十年!
[STM32 + esp-12s connect Tencent cloud IOT development platform 1] creation of cloud platform and burning of at firmware
B_QuRT_User_Guide(38)
电子设备行业智能供应链协同平台解决方案:解决低效, 赋能产业数字化升级
Open source hardware small project: anxinco esp-c3f control ws2812
PCB wiring rules of PCI Express interface
Mysql索引优化实战一
[compilation principle] lexical analysis design and Implementation
How to change the formula picture in the paper directly into the formula in word
Unity3d learning notes 5 - create sub mesh
随机推荐
LDO稳压芯片-内部框图及选型参数
B / Qurt Utilisateur Guide (36)
Experience sharing of system architecture designers in preparing for the exam: the direction of paper writing
2022 届的应届生都找到工作了吗?做自媒体可以吗?
0-1背包问题
高效的S2B2C电商系统,是这样帮助电子材料企业提升应变能力的
Right click the idea file to create new. There is no solution to create new servlet
13、 System optimization
移动端异构运算技术 - GPU OpenCL 编程(基础篇)
UE4_ Ue5 combined with Logitech handle (F710) use record
B_QuRT_User_Guide(36)
SAP 内存参数调优过程
POJ2392 SpaceElevator [DP]
城联优品作为新力量初注入,相关上市公司股价应声上涨150%
在软件工程领域,搞科研的这十年!
LM12丨Rolling Heikin Ashi二重K线滤波器
How to login and enable synchronization function in Google browser
Markdown
SAP HR 家庭成员信息
FreeLink开源呼叫中心设计思想