当前位置:网站首页>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);
}
边栏推荐
- SAP 内存参数调优过程
- 在软件工程领域,搞科研的这十年!
- USB (XVI) 2022-04-28
- 2022 certified surveyors are still at a loss when preparing for the exam? Teach you how to take the exam hand in hand?
- HDU 4747 Mex「建议收藏」
- New potential energy of industrial integration, Xiamen station of city chain technology digital summit successfully held
- UE4_ Use of ue5 blueprint command node (turn on / off screen response log publish full screen display)
- SAP HR 家庭成员信息
- MATLAB signal processing [Q & A essays · 2]
- POJ2392 SpaceElevator [DP]
猜你喜欢
LeeCode -- 6. Zigzag transformation
Markdown
ROS2专题(03):ROS1和ROS2的区别【01】
As a new force, chenglian premium products was initially injected, and the shares of relevant listed companies rose 150% in response
电子设备行业智能供应链协同平台解决方案:解决低效, 赋能产业数字化升级
2022 certified surveyors are still at a loss when preparing for the exam? Teach you how to take the exam hand in hand?
Solution of intelligent supply chain collaboration platform in electronic equipment industry: solve inefficiency and enable digital upgrading of industry
在软件工程领域,搞科研的这十年!
USB (XV) 2022-04-14
SAP HR 家庭成员信息
随机推荐
SQL database execution problems
Add data analysis tools in Excel
三问TDM
Conversion between commonsmultipartfile and file
漏洞复现----49、Apache Airflow 身份验证绕过 (CVE-2020-17526)
The 19th Zhejiang Provincial College Programming Contest VP record + supplementary questions
Entity层、DAO层、Service层、Controller层 先后顺序
13、 System optimization
Deep understanding of MySQL lock and transaction isolation level
B_QuRT_User_Guide(38)
Live-Server使用
JS get the key and value of the object
SRM supplier cloud collaborative management platform solution for building materials industry to realize business application scalability and configuration
深入理解Mysql锁与事务隔离级别
Illegal behavior analysis 1
HDU 4747 mex "recommended collection"
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(38)
Mobile heterogeneous computing technology - GPU OpenCL programming (basic)
伸展树(一) - 图文解析与C语言实现