当前位置:网站首页>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!
- Anxin vb01 offline voice module access intelligent curtain guidance
- 13、 System optimization
- Explain
- [compilation principle] lexical analysis design and Implementation
- B_ QuRT_ User_ Guide(40)
- Installing spss25
- Windows set redis to start automatically
- v-for遍历对象
- B_ QuRT_ User_ Guide(36)
猜你喜欢

B_ QuRT_ User_ Guide(36)

Digital procurement management system for fresh food industry: help fresh food enterprises solve procurement problems and implement online procurement throughout the process

B_ QuRT_ User_ Guide(37)

0-1背包问题

re1攻防世界逆向

Right click the idea file to create new. There is no solution to create new servlet

What if once again forgets the login password of raspberry pie? And you don't have a monitor yet! Today, I would like to introduce a method
![给出一个数组,如 [7864, 284, 347, 7732, 8498],现在需要将数组中的数字拼接起来,返回「最大的可能拼出的数字」](/img/21/2e99dd6173ab4925ec22290cd4a357.png)
给出一个数组,如 [7864, 284, 347, 7732, 8498],现在需要将数组中的数字拼接起来,返回「最大的可能拼出的数字」

漏洞复现----49、Apache Airflow 身份验证绕过 (CVE-2020-17526)

LDO稳压芯片-内部框图及选型参数
随机推荐
Open source hardware small project: anxinco esp-c3f control ws2812
Anxinco esp32-a1s development board is adapted to Baidu dueros routine to realize online voice function
MySQL Index Optimization Practice II
系统设计概述
Unity3d learning notes 5 - create sub mesh
B / Qurt Utilisateur Guide (36)
8.31 Tencent interview
Cloud native is devouring everything. How should developers deal with it?
电子设备行业智能供应链协同平台解决方案:解决低效, 赋能产业数字化升级
SAP HR 社会工作经历 0023
First week of July
Experience sharing of system architecture designers in preparing for the exam: the direction of paper writing
统计电影票房排名前10的电影并存入还有一个文件
IDEA 2021.3. X cracking
B_ QuRT_ User_ Guide(37)
Coreseek:第二步建索引及測试
Puce à tension stabilisée LDO - schéma de bloc interne et paramètres de sélection du modèle
POJ2392 SpaceElevator [DP]
FreeLink开源呼叫中心设计思想
SAP HR 家庭成员信息