-
命名空間中有若干個(gè)類
查看全部 -
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.各位相加
給定一個(gè)非負(fù)整數(shù) num,反復(fù)將各個(gè)位上的數(shù)字相加,直到結(jié)果為一位數(shù)。返回這個(gè)結(jié)果。
輸入樣例1:
在這里給出一組輸入。例如:
38
輸出樣例1:
在這里給出相應(yīng)的輸出。例如:
2
解釋樣例2:
數(shù)字38各位相加的過(guò)程為:
38 --> 3 + 8 --> 11
11 --> 1 + 1 --> 2
由于 2 是一位數(shù),所以返回 2。輸入樣例2:
在這里給出一組輸入。例如:
0
輸出樣例2:
在這里給出相應(yīng)的輸出。例如:
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.尋找數(shù)組中的最小值
尋找數(shù)組中最小值
注意:數(shù)組中元素并不一定是整數(shù)
輸入格式:
第一行輸入為數(shù)組元素的個(gè)數(shù),假設(shè)為n
第二行輸入為數(shù)組中的第一個(gè)元素
...
第N輸入為數(shù)組的最后一個(gè)元素輸出格式:
輸出數(shù)組中的最小值
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.尋找數(shù)組中的最大值
尋找數(shù)組中最大值
注意:數(shù)組中元素并不一定是整數(shù)
輸入格式:
第一行輸入為數(shù)組元素的個(gè)數(shù),假設(shè)為n
第二行輸入為數(shù)組中的第一個(gè)元素
...
第N輸入為數(shù)組的最后一個(gè)元素輸出格式:
輸出數(shù)組中的最大值
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.計(jì)算整數(shù)n到整數(shù)m之間數(shù)的總和
計(jì)算整數(shù)n到整數(shù)m之間數(shù)的總和
輸入格式:
第一行輸入整數(shù)n
第二行輸入整數(shù)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.求解分段函數(shù)
編寫程序,求解如下分段函數(shù):
當(dāng)x≤0時(shí)f(x)=x2+2
當(dāng)x>0時(shí)f(x)=x+3
注意:
x為整數(shù)
輸入格式:
輸入為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.查詢成績(jī)
輸入一個(gè)成績(jī):
如果成績(jī)大于等于60,則輸出恭喜您,您通過(guò)了這次考試!
否則輸出非常抱歉,您沒有通過(guò)此次考試!。
注意:
成績(jī)都為整數(shù)
標(biāo)點(diǎn)符號(hào)為中文標(biāo)點(diǎn)符號(hào)
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("恭喜您,您通過(guò)了這次考試!");
? ? ? ? ? ? }
? ? ? ? ? ? else?
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Console.WriteLine("非常抱歉,您沒有通過(guò)此次考試!");
? ? ? ? ? ? }
? ? ? ? ? ??
? ? ? ? }
? ? }
}
查看全部 -
3.統(tǒng)計(jì)字符串中指定字符的數(shù)量
統(tǒng)計(jì)字符串中指定字符的數(shù)量
輸入格式:
第一行輸入給定字符串
第二行輸入給定字符輸出格式:
輸出待統(tǒng)計(jì)字符個(gè)數(shù)
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.求解互補(bǔ)數(shù)
請(qǐng)編寫程序求解輸入數(shù)據(jù)的互補(bǔ)數(shù),如果輸入數(shù)據(jù)小于0則打印字符串輸入數(shù)據(jù)不合法。互補(bǔ)數(shù)定義如下:
兩個(gè)個(gè)位數(shù)(正整數(shù))相加和為10的一對(duì)數(shù),稱為互補(bǔ)數(shù)
例如?: 1的互補(bǔ)數(shù)是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("輸入數(shù)據(jù)不合法");
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Console.WriteLine("{0}", 10 - a);
? ? ? ? ? ? }
? ? ? ? }
? ? }
}
查看全部 -
1.統(tǒng)計(jì)n到m之間能被s整除的數(shù)的總和
統(tǒng)計(jì)n到m之間(包括m)能被s整除的數(shù)的總和
輸入格式:
第一行輸入n,如1
第二行輸入m,如100
第三行輸入s,如3輸出格式:
輸出結(jié)果,如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;//請(qǐng)?zhí)砑哟a,過(guò)濾3和8
? ? ? ? ? ? ? ? Console.Write(x);
查看全部 -
C#中的switch,每個(gè)分支都應(yīng)該以break;結(jié)束,break的作用是跳出switch結(jié)構(gòu)。但是,如果某個(gè)分支中沒有語(yǔ)句,那么也可以不寫break;
查看全部 -
switch?中的(變量)只能是3種類型:整型(如?int?)、字符型(?char?)、字符串類型(?string?)。
查看全部 -
while循環(huán)
先判斷循環(huán)條件,若條件為?true?,就執(zhí)行循環(huán)體一次,然后再判斷條件...當(dāng)條件為?false?時(shí),結(jié)束循環(huán)。
查看全部
舉報(bào)