当前位置:网站首页>C#期末复习编程题(老师猜的)
C#期末复习编程题(老师猜的)
2022-06-12 16:36:00 【Vijurria】
1.判断n是奇是偶。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Vijurria
{
class Program
{
static void Main(string[] args)
{
int n = int.Parse(Console.ReadLine());
if (n % 2 == 1) Console.WriteLine("{0}为奇数", n);
else Console.WriteLine("{0}为偶数", n);
Console.ReadKey()
; }
}
}2.求“1-n”之间的偶数和/奇数和。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Vijurria
{
class Program
{
static void Main(string[] args)
{
int n = int.Parse(Console.ReadLine());
int sum0 = 0, sum1 = 0;
for(int i=1;i<=n;i++)
{
if (i % 2 == 1) sum0 += i;
else sum1 += i;
}
Console.WriteLine("奇数和{0},偶数和{1}", sum0, sum1);
Console.ReadKey();
; }
}
}3.判断字符中有几个大写,小写字母,数字,其他字符。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Vijurria
{
class Program
{
static void Main(string[] args)
{
string s=Console.ReadLine();
int da = 0, xiao = 0, shuzi = 0, qita = 0;
for(int i=0;i<s.Length;i++)
{
if (s[i] >= 'A' && s[i] <= 'Z') da++;
else if (s[i] >= 'a' && s[i] <= 'z') xiao++;
else if (s[i] >= '0' && s[i] <= '9') shuzi++;
else qita++;
}
Console.WriteLine("大写字母{0}个,小写字母{1}个,数字{2}个,其他{3}个。", da, xiao, shuzi, qita);
Console.ReadKey();
; }
}
}4.90-100优秀,80-90良好,60-80及格,低于60不及格。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Vijurria
{
class Program
{
static void Main(string[] args)
{
int n = int.Parse(Console.ReadLine());
if (n >= 90 && n <= 100) Console.WriteLine("优秀!");
else if (n >= 80 && n <= 90) Console.WriteLine("良好!");
else if (n >= 60 && n <= 80) Console.WriteLine("及格!");
else Console.WriteLine("不及格!");
Console.ReadKey();
; }
}
}5.定义两个方法:一个用来求圆的面积,一个用来求矩形的面积。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Vijurria
{
class Program
{
public static double solve(int x)
{
return Math.PI * x * x;
}
public static int fun(int x,int y)
{
return x * y;
}
public static void Main(string[] args)
{
int r = int.Parse(Console.ReadLine());
double sum = solve(r);
Console.WriteLine("圆的面积为{0}", sum);
int a= int.Parse(Console.ReadLine());
int b= int.Parse(Console.ReadLine()); ;
int ans = fun(a, b);
Console.WriteLine("矩形的面积为{0}", ans);
Console.ReadKey();
; }
}
}6.输入一个字符串,查看里面有多少个单词?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Vijurria
{
class Program
{
static void Main(string[] args)
{
bool flag = false;
string s = Console.ReadLine();
int sum = 0;
for(int i=0;i<s.Length;i++)
{
if (s[i] != ' ')
{
if (flag == false)
{
sum++;
flag = true;
}
else if (s[i - 1] == ' ') sum++;
}
}
Console.WriteLine(sum);
Console.ReadKey();
; }
}
}7.输入若干个成绩,输出平均分。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Vijurria
{
class Program
{
static void Main(string[] args)
{
int n = int.Parse(Console.ReadLine());
int sum = 0;
for(int i=1;i<=n;i++)
{
int x = int.Parse(Console.ReadLine());
sum += x;
}
Console.WriteLine(sum/n);
Console.ReadKey();
; }
}
}8.求1-n之中的完数个数。(一个数如果恰好等于它的因子之和,这个数就称为“完数”)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Vijurria
{
class Program
{
static void Main(string[] args)
{
int n = int.Parse(Console.ReadLine());
int sum = 0;
for(int i=2;i<=n;i++)
{
int flag = 0;
for(int j=1;j<i;j++)
{
if (i % j == 0) flag += j;
}
if (flag == i) Console.WriteLine(i);//如果他想输出个数,这里改成sum++;就行了
}
//Console.WriteLine(sum);//这里就不用输出个数了
Console.ReadKey();
; }
}
}
9.a能否整除b?(处理异常机制)。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Vijurria
{
class Program
{
static void Main(string[] args)
{
try
{
int a = int.Parse(Console.ReadLine ());
int b = int.Parse(Console.ReadLine ());
if (a % b == 0) Console.WriteLine("YES");
else Console.WriteLine("NO");
}
catch
{
Console.WriteLine("输入异常!");
}
Console.ReadKey();
; }
}
}10.给定一个数组,找到某个数第一次出现的下标。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Vijurria
{
class Program
{
static void Main(string[] args)
{
int[] a = new int[] { 2, 4, 6, 8, 10, 1, 3, 5, 7, 9 };
int n = int.Parse(Console.ReadLine());
int idx = -1;
for(int i=0;i<a.Length;i++)
{
if(a[i]==n)
{
idx = i;
break;
}
}
if (idx == -1) Console.WriteLine("没有出现过");
else Console.WriteLine("下标为:{0}",idx);
Console.ReadKey();
; }
}
}11.判断一个数是否为质数?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Vijurria
{
class Program
{
static void Main(string[] args)
{
int n = int.Parse(Console.ReadLine());
bool flag = false;
for(int i=2;i<=n/i;i++)
{
if(n%i==0)
{
flag = true;
break;
}
}
if (flag == false&&n!=1) Console.WriteLine("是质数");
else Console.WriteLine("不是质数");
Console.ReadKey();
; }
}
}12.求一个表达式的和:1!+2!+3!+4!+。。。n!=?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Vijurria
{
class Program
{
static void Main(string[] args)
{
int n = int.Parse(Console.ReadLine());
int sum = 0;
int flag = 1;
int ans = 1;
while(flag<=n)
{
ans *= flag;
sum += ans;
flag++;
}
Console.WriteLine(sum);
Console.ReadKey();
; }
}
}13.(冒泡排序163 选择排序171)排序
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Vijurria
{
class Program
{
static void Main(string[] args)
{
int[] a = new int[] { 1, 3, 5, 7, 9, 2, 4, 6, 8, 10 };
Array.Sort(a);
for (int i = 0; i < a.Length; i++)
Console.Write(a[i] + " ");
Console.ReadKey();
; }
}
}边栏推荐
- \Begin{algorithm} notes
- 武汉大学甘菲课题组和南昌大学徐振江课题组联合招聘启事
- The C programming language (version 2) notes / 8 UNIX system interface / 8.2 low level i/o (read and write)
- Project training of Shandong University rendering engine system (V)
- MySQL statement
- Programmers broke the news: 3 job hopping in 4 years, and the salary has tripled! Netizen: the fist is hard
- 1.delete
- Understand go modules' go Mod and go sum
- calibration of sth
- Preprocessing command section 3
猜你喜欢

canvas 高级功能(下)

WebRTC 的音频网络对抗概述

程序员爆料:4年3次跳槽,薪资翻了3倍!网友:拳头硬了......

从50亿图文中提取中文跨模态新基准Zero,奇虎360全新预训练框架超越多项SOTA

'virtue and art' in the field of recurrent+transformer video recovery

acwing 800. Target and of array elements
![[research] reading English papers -- the welfare of researchers in English poor](/img/8a/671e6cb6a3f4e3b84ea0795dc5a365.png)
[research] reading English papers -- the welfare of researchers in English poor

双写一致性问题

Anyone who watches "Meng Hua Lu" should try this Tiktok effect

Collect | 22 short videos to learn Adobe Illustrator paper graphic editing and typesetting
随机推荐
Anfulai embedded weekly report no. 268: May 30, 2022 to June 5, 2022
Recommend AI intelligent drawing repair software
丁总路由器设置以及401联网
MySQL - server configuration related problems
博士申请 | 新加坡国立大学Xinchao Wang老师招收图神经网络方向博士/博后
Qcustomplot notes (I): qcustomplot adding data and curves
IDEA在控制台显示出services,统一管理所有的jetty服务,
[MySQL] internal connection, external connection and self connection (detailed explanation)
The C programming language (version 2) notes / 8 UNIX system interface / 8.2 low level i/o (read and write)
Servlet API
generate pivot data 1
calibration of sth
The C programming language (version 2) notes / 8 UNIX system interface / 8.7 instance (storage allocator)
Double write consistency problem
Mongodb learning and sorting (basic command learning of users, databases, collections and documents)
Loading shellcode in C and go languages
Leetcode 2190. The number that appears most frequently in the array immediately after the key (yes, once)
Analysis of Nacos config dynamic refresh source code
generate pivot data 2
WebRTC 的音频网络对抗概述