当前位置:网站首页>C simple question one
C simple question one
2022-07-07 23:34:00 【Bobo in summer】
1. Output in reverse order
string aa = Console.ReadLine();
string bb="";
for (int i = aa.Length-1; i >= 0; i--)
{
bb += aa[i];
}
Console.WriteLine(bb);
2. The length of three sides of a triangle is known a,b,c, The three sides are input by the user , Programming judgment a、b、c Whether the value of constitutes a triangle , If it forms a triangle , Then calculate and output the area of the triangle , Otherwise output “ Can't make a triangle ” Calculate the area of the triangle
double a =double.Parse(Console.ReadLine());
double b = double.Parse(Console.ReadLine());
double c = double.Parse(Console.ReadLine());
double s = 0;
double area = 0;
if ((a + b > c) && (a + c > b) && (b + c > a))
{
Console.WriteLine(" Sure ");
s = 0.5 * (a + b + c);
area = Math.Sqrt(s * (s - a) * (s - b) * (s - c));
}
else
{
Console.WriteLine(" Can't form a triangle ");
}
3. Enter a string str1, Delete str1 All of them 0-9 The number character of , Output the processed string
string aa = Console.ReadLine();
string bb = "";
for (int i = 0; i < aa.Length; i++)
{
if (!char.IsDigit(aa[i]))
{
bb += aa[i];
}
}
Console.WriteLine(bb);
4. Input 10 Number , Calculate average , Count the number of data below the average and output the data below the average
Console.Write(" Please enter ten integers : Press enter Continue to enter ");
int[] a = new int[10];
for (int i = 0; i < 10; i++)
{
a[i] = int.Parse(Console.ReadLine());
}
int b = 0;
for (int j = 0; j < 10; j++)
{
b += a[j];
}
float c = b / 10;
int count = 0;
for (int k = 0; k < 10; k++)
{
if (a[k] < c)
{
Console.Write("{0},", a[k]);
count++;
}
}
Console.WriteLine(" share {0} A number less than the average ", count);
5. Input 10 Number , Calculate average , Count the number of data higher than the average value and output the data higher than the average value
int[] srr = new int[10] {
1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
double sum = 0;
double ave = 0;
int count = 1;
for (int i = 0; i < 10; i++)
{
sum += srr[i];
count++;
}
ave = sum / count;
string aa = "";
int count1 = 1;
for (int j = 0; j < 10; j++)
{
if (srr[j] > ave)
{
aa += srr[j]+" ";
count1++;
}
}
Console.WriteLine("{0} ",aa);
Console.WriteLine(count1);
6. Enter some integers , Find the largest number , Secondary maximum , minimum value , Secondary minimum
int[] srr = new int[10];
for (int a = 0; a < 10; a++)
{
srr[a] =int.Parse( Console.ReadLine());
}
int temp = 0;
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 9-i; j++)
{
if (srr[j] > srr[j + 1])
{
temp = srr[j];
srr[j] = srr[j + 1];
srr[j + 1] = temp;
}
}
}
Console.WriteLine("{0},{1},{2},{3}",srr[9],srr[8],srr[0],srr[1]);
7. Enter several ordered positive integers , For the same data, only one , Output retained data . for example , The input data is : 2,2,2,3,3,4,5,5,6,6,8,8,9,9,9,10,10,10 The final output is : 2,3,4,5,6,8,9,10.
string str1=Console.ReadLine();;
string ret = "";
for (int i = 0; i < str1.Length; i++)
{
char ch = str1[i];
if (ret.IndexOf(ch.ToString()) >= 0)
{
continue;
}
ret = ret + ch.ToString();
}
Console.WriteLine(ret);
Console.ReadLine();
8. Enter a string , Judge if it's all numbers , Convert it to an integer , If other symbols are included , Give an error .
string aa=Console.ReadLine();
for (int i = 0; i < aa.Length; i++)
{
if (!char.IsDigit(aa[i]))
{
Console.WriteLine("error");
break;
}
else
Console.WriteLine(aa);
break;
}
9. Input 20 A positive integer , Count and output the number of odd and even numbers , And classify and output all odd and even numbers .
int[] arr = new int[10] {
1,2,3,4,5,6,7,8,9,10 };
string aa="";
string bb="";
int count1 = 1;
int count2 = 1;
for (int i = 0; i < 10; i++)
{
if (arr[i] % 2 == 0)
{
aa += arr[i]+" ";
count1++;
}
else
{
bb += arr[i]+" ";
count2++;
}
}
Console.WriteLine(aa);
Console.WriteLine(count1);
Console.WriteLine(bb);
Console.WriteLine(count2);
10. Input from terminal 5 Number , Output from small to large .
int[] a = new int[6];
Console.WriteLine(" Please enter six numbers :");
for (int l = 0; l < 6; l++)
{
a[l] = int.Parse(Console.ReadLine());
}
for (int j = 0; j < 6; j++)
{
for (int i = 0; i<5; i++)
{
int temp;
if (a[i] > a[i+1])
{
temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
}
}
}
for (int k = 0; k < 6; k++)
{
Console.Write(" " + a[k]);
}
using System.Collections;
ArrayList list = new ArrayList();
for (int i = 0; i < 5; i++){
list.Add(int.Parse(Console.ReadLine()));
}
list.Sort();
for (int i = 0; i < list.Count; i++) {
Console.WriteLine(list[i]);
}
11. Read in... From the keyboard 20 Data into array , Count the number of negative numbers , And calculate the sum of these negative numbers .
int[] a = new int[20];
Console.WriteLine(" Please enter twenty numbers :");
for (int i = 0; i < 20; i++)
{
a[i] = int.Parse(Console.ReadLine());
}
int count = 0;
double sum = 0;
for (int i = 0; i < 20; i++)
{
if (a[i] < 0)
{
count++;
sum += a[i];
}
}
Console.WriteLine(" Number :{0} Negative numbers and :{1}", count, sum);
12. seek n within ( barring n) Can't be 2 and 5 to be divisible by ( Can be 2 perhaps 5 Divisible but not divisible at the same time ) The square root of the sum of all natural numbers s,n Input from keyboard .
Console.WriteLine(" Please enter n");
int n = int.Parse(Console.ReadLine());
int sum = 0;
for(int i=0;i<n;i++)
{
if(i%2!=0&&i%5!=0)
{
sum += i;
}
}
double s = Math.Sqrt(sum);
Console.WriteLine(s);
random Generate 10 Number , Sort from small to large , Output max min , And the rest , Find the sum of the remaining numbers , Average .
public static void avee()
{
int[] srr = new int[10];
Random shuzu = new Random();
for (int i = 0; i < 10; i++)
{
srr[i]= shuzu.Next(60, 100);
}
//for (int i = 0; i < 10; i++)
//{
// srr[i] =int.Parse( Console.ReadLine());
//}
int m;
string ssss = "";
double ave;
double sum = 0;
for (int n = 0; n < 10; n++)
{
for (int j = 0; j < 9 - n; j++)
{
if (srr[j] > srr[j + 1])
{
m = srr[j];
srr[j] = srr[j + 1];
srr[j + 1] = m;
}
}
}
for(int i=1;i<srr.Length-1;i++)
{
ssss += srr[i]+" ";
sum += srr[i];
}
ave = sum / 8;
Console.WriteLine(" The maximum value is {0}", srr[9]);
Console.WriteLine(" The minimum value is {0}", srr[0]);
Console.WriteLine(" and :{0} Average {1}",sum,ave);
Console.WriteLine(ssss);
}
avee();
边栏推荐
猜你喜欢
UE4_ Ue5 combined with Logitech handle (F710) use record
SAP HR 家庭成员信息
B_QuRT_User_Guide(38)
Unity3d Learning Notes 6 - GPU instantiation (1)
Cloud native is devouring everything. How should developers deal with it?
SAP 内存参数调优过程
伸展树(一) - 图文解析与C语言实现
Home appliance industry channel business collaboration system solution: help home appliance enterprises quickly realize the Internet of channels
PCI-Express接口的PCB布线规则
给出一个数组,如 [7864, 284, 347, 7732, 8498],现在需要将数组中的数字拼接起来,返回「最大的可能拼出的数字」
随机推荐
USB (XIV) 2022-04-12
包装行业智能供应链S2B2B商城解决方案:开辟电商消费新生态
0-1背包问题
MySQL Index Optimization Practice II
The 19th Zhejiang Provincial College Programming Contest 2022 f.easyfix chairman tree
【7.5】15. 三数之和
HDU 4747 Mex「建议收藏」
在软件工程领域,搞科研的这十年!
leetcode-520. Detect capital letters -js
Cloud native data warehouse analyticdb MySQL user manual
windows设置redis开启自动启动
Have all the fresh students of 2022 found jobs? Is it OK to be we media?
USB (XVI) 2022-04-28
Force deduction solution summary 648 word replacement
Digital procurement management system for fresh food industry: help fresh food enterprises solve procurement problems and implement online procurement throughout the process
One week learning summary of STL Standard Template Library
Ros2 topic (03): the difference between ros1 and ros2 [01]
LM12丨Rolling Heikin Ashi二重K线滤波器
Dynamic agent explanation (July 16, 2020)
Illegal behavior analysis 1