当前位置:网站首页>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);
}
边栏推荐
猜你喜欢
One week learning summary of STL Standard Template Library
Summary of SQL single table query 2020.7.27
S2b2b mall solution of intelligent supply chain in packaging industry: opening up a new ecosystem of e-commerce consumption
Design and implementation of spark offline development framework
[STM32 + esp-12s connect Tencent cloud IOT development platform 1] creation of cloud platform and burning of at firmware
在软件工程领域,搞科研的这十年!
2022 certified surveyors are still at a loss when preparing for the exam? Teach you how to take the exam hand in hand?
经纬度PLT文件格式说明
高效的S2B2C电商系统,是这样帮助电子材料企业提升应变能力的
SRM supplier cloud collaborative management platform solution for building materials industry to realize business application scalability and configuration
随机推荐
谷歌浏览器怎么登录及开启同步功能
1. Sum of two numbers
SAP HR 家庭成员信息
POJ2392 SpaceElevator [DP]
Anxinco EC series modules are connected to the multi protocol access products of onenet Internet of things open platform
Coreseek: the second step is index building and testing
【7.5】15. 三数之和
The text editor of markdown class should add colors to fonts (including typora, CSDN, etc.)
Mysql索引优化实战一
[untitled]
B_QuRT_User_Guide(36)
Oracle database backup and recovery
Mysql索引优化实战二
SAP HR 社会工作经历 0023
LDO voltage stabilizing chip - internal block diagram and selection parameters
建筑建材行业SRM供应商云协同管理平台解决方案,实现业务应用可扩展可配置
USB (XVI) 2022-04-28
在软件工程领域,搞科研的这十年!
Live-Server使用
[STM32 + esp-12s connect Tencent cloud IOT development platform 1] creation of cloud platform and burning of at firmware