-
命名空間中有若干個類
查看全部 -
for (int i = 1; i <= 7; i++)
{
????for (int j = 1; j <= 7; j++)
????{
????????Console.Write(i ==j || i + j == 8 ? 'O' : '.');
????}
????Console.WriteLine();
}
查看全部 -
10.各位相加
給定一個非負整數 num,反復將各個位上的數字相加,直到結果為一位數。返回這個結果。
輸入樣例1:
在這里給出一組輸入。例如:
38
輸出樣例1:
在這里給出相應的輸出。例如:
2
解釋樣例2:
數字38各位相加的過程為:
38 --> 3 + 8 --> 11
11 --> 1 + 1 --> 2
由于 2 是一位數,所以返回 2。輸入樣例2:
在這里給出一組輸入。例如:
0
輸出樣例2:
在這里給出相應的輸出。例如:
0
using System;
class Program
{
? ? static void Main()
? ? {
? ? ? ? int n = Convert.ToInt16(Console.ReadLine());
? ? ? ??
? ? ? ? while(n > 9)
? ? ? ? {
? ? ? ? ? ? int sum = 0;
? ? ? ? ? ? while(n != 0)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? sum += n % 10;
? ? ? ? ? ? ? ? n /= 10;
? ? ? ? ? ? }
? ? ? ? ? ? n = sum;
? ? ? ? }
? ? ? ? Console.WriteLine(n);
? ? }
? ??
}
查看全部 -
8.尋找數組中的最小值
尋找數組中最小值
注意:數組中元素并不一定是整數
輸入格式:
第一行輸入為數組元素的個數,假設為n
第二行輸入為數組中的第一個元素
...
第N輸入為數組的最后一個元素輸出格式:
輸出數組中的最小值
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
? ? class Program
? ? {
? ? ? ? static void Main(string[] args)
? ? ? ? {
? ? ? ? ? ? int num = Convert.ToInt32(Console.ReadLine());
? ? ? ? ? ? double[] arr = new double[num];
? ? ? ? ? ? double min = arr[0];
? ? ? ? ? ? for (int i = 0; i < arr.Length; i++)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? arr[i] = double.Parse(Console.ReadLine());
? ? ? ? ? ? ? ? if (arr[i] < min)
? ? ? ? ? ? ? ? ? ? min = arr[i];
? ? ? ? ? ? }
? ? ? ? ? ? Console.WriteLine(min);
? ? ? ? }
? ? }
}
查看全部 -
7.尋找數組中的最大值
尋找數組中最大值
注意:數組中元素并不一定是整數
輸入格式:
第一行輸入為數組元素的個數,假設為n
第二行輸入為數組中的第一個元素
...
第N輸入為數組的最后一個元素輸出格式:
輸出數組中的最大值
using System;
class Program
{
? ? static void Main()
? ? {
? ? ? ? int n = Convert.ToInt16(Console.ReadLine());
? ? ? ? double max = -0x3f3f3f3f;
? ? ? ? for(int i = 0; i < n; ++i)
? ? ? ? {
? ? ? ? ? ? double tmp = Convert.ToDouble(Console.ReadLine());
? ? ? ? ? ? if(tmp > max)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? max = tmp;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? Console.WriteLine(max);
? ? }
? ??
}
查看全部 -
6.計算整數n到整數m之間數的總和
計算整數n到整數m之間數的總和
輸入格式:
第一行輸入整數n
第二行輸入整數m輸出格式:
輸出和
using System;
class Program
{
? ? static void Main()
? ? {
? ? ? ? int n = Convert.ToInt16(Console.ReadLine());
? ? ? ? int m = Convert.ToInt16(Console.ReadLine());
? ? ? ? int sum = 0;
? ? ? ? for(int i = n; i <= m; ++i)
? ? ? ? {
? ? ? ? ? ? ? ? sum += i;
? ? ? ? }
? ? ? ? Console.WriteLine(sum);
? ? }
? ??
}
查看全部 -
5.求解分段函數
編寫程序,求解如下分段函數:
當x≤0時f(x)=x2+2
當x>0時f(x)=x+3
注意:
x為整數
輸入格式:
輸入為x
輸出格式:
輸出為f(x)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
? ? internal class Program
? ? {
? ? ? ? static void Main(string[] args)
? ? ? ? {
? ? ? ? ? ? double x;
? ? ? ? ? ? x = Convert.ToDouble(Console.ReadLine());
? ? ? ? ? ? if (x <= 0)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Console.WriteLine(x*x+2);
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Console.WriteLine(x+3);
? ? ? ? ? ? }
? ? ? ? }
? ? }
}
查看全部 -
4.查詢成績
輸入一個成績:
如果成績大于等于60,則輸出恭喜您,您通過了這次考試!
否則輸出非常抱歉,您沒有通過此次考試!。
注意:
成績都為整數
標點符號為中文標點符號
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
? ? class Program
? ? {
? ? ? ? static void Main(string[] args)
? ? ? ? {
? ? ? ? ? ? float cj;
? ? ? ? ? ? cj = Convert.ToSingle(Console.ReadLine());
? ? ? ? ? ? if (cj >= 60)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Console.WriteLine("恭喜您,您通過了這次考試!");
? ? ? ? ? ? }
? ? ? ? ? ? else?
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Console.WriteLine("非常抱歉,您沒有通過此次考試!");
? ? ? ? ? ? }
? ? ? ? ? ??
? ? ? ? }
? ? }
}
查看全部 -
3.統計字符串中指定字符的數量
統計字符串中指定字符的數量
輸入格式:
第一行輸入給定字符串
第二行輸入給定字符輸出格式:
輸出待統計字符個數
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
? ? internal class Program
? ? {
? ? ? ? static void Main(string[] args)
? ? ? ? {
? ? ? ? ? ? string str;
? ? ? ? ? ? str = Console.ReadLine();
? ? ? ? ? ? int count = 0;
? ? ? ? ? ? char y;
? ? ? ? ? ? y = Convert.ToChar(Console.ReadLine());
? ? ? ? ? ? for(int i = 0; i < str.Length; ++i)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? if (str[i] == y)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ++count;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? Console.WriteLine(count);
? ? ? ? }
? ? }
}
查看全部 -
2.求解互補數
請編寫程序求解輸入數據的互補數,如果輸入數據小于0則打印字符串輸入數據不合法。互補數定義如下:
兩個個位數(正整數)相加和為10的一對數,稱為互補數
例如?: 1的互補數是9。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
? ? class Program
? ? {
? ? ? ? static void Main(string[] args)
? ? ? ? {
? ? ? ? ? ? int a = Convert.ToInt32(Console.ReadLine());
? ? ? ? ? ? if (a < 0)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Console.WriteLine("輸入數據不合法");
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Console.WriteLine("{0}", 10 - a);
? ? ? ? ? ? }
? ? ? ? }
? ? }
}
查看全部 -
1.統計n到m之間能被s整除的數的總和
統計n到m之間(包括m)能被s整除的數的總和
輸入格式:
第一行輸入n,如1
第二行輸入m,如100
第三行輸入s,如3輸出格式:
輸出結果,如1683
using System;
class Program
{
? ? static void Main()
? ? {
? ? ? ? int n = Convert.ToInt16(Console.ReadLine());
? ? ? ? int m = Convert.ToInt16(Console.ReadLine());
? ? ? ? int s = Convert.ToInt16(Console.ReadLine());
? ? ? ? int sum = 0;
? ? ? ? for(int i = n; i <= m; ++i)
? ? ? ? {
? ? ? ? ? ? if(i % s == 0)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? sum += i;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? Console.WriteLine(sum);
? ? }
? ??
}
查看全部 -
for (int x = 1; x < 10; x++)
? ? ? ? ? ? {
? ? ? ? ? ? ? ?if (x==3||x==8)
? ? ? ? ? ? ? ?continue;//請添加代碼,過濾3和8
? ? ? ? ? ? ? ? Console.Write(x);
查看全部 -
C#中的switch,每個分支都應該以break;結束,break的作用是跳出switch結構。但是,如果某個分支中沒有語句,那么也可以不寫break;
查看全部 -
switch?中的(變量)只能是3種類型:整型(如?int?)、字符型(?char?)、字符串類型(?string?)。
查看全部 -
while循環
先判斷循環條件,若條件為?true?,就執行循環體一次,然后再判斷條件...當條件為?false?時,結束循環。
查看全部
舉報