输⼊是个不相等的正整数,输出这10个正整数中的第⼆⼤的数。 样例输⼊ 3 5 7 2 9 5 3 10 3 8 样例输出 9
string str = Console.ReadLine();
string[] strArray = str.Split(" ");
int[] intArray = new int[strArray.Length];
for (int i = 0; i < strArray.Length; i++)
{
int number = Convert.ToInt32(strArray[i]);
intArray[i] = number;
}
int max1 = 0;int max2 = 0;
for(int i = 0; i < intArray.Length; i++)
{
if (intArray[i] > max1)
{
max2 = max1;
max1 = intArray[i];
}
else
{
if (intArray[i] > max2)
{
max2 = intArray[i];
}
}
}
Console.WriteLine("第一大值是" + max1 + " 第二大值是" + max2);
描述给定⼀个只包含⼩写字⺟的字符串,请你找出第⼀个仅出现⼀次的字符。如果没有,输出 no。
//时间 空间
string str = Console.ReadLine();
int[] countArray = new int[26];//计数,保存每个字符出现的个数
for (int i = 0; i < str.Length; i++)
{
countArray[str[i] - 'a'] = countArray[str[i] - 'a'] + 1;
}
bool isFind = false;
char c = ' ';
for (int i = 0; i < str.Length; i++)
{
if (countArray[str[i] - 'a'] == 1)
{
isFind = true;
c = str[i];
break;
}
}
if (isFind == false)
{
Console.WriteLine("no");
}
else
{
Console.WriteLine(c);
}
⼤⼩写字⺟互换 把⼀个字符串中所有出现的⼤写字⺟都替换成⼩写字⺟,同时把⼩写字⺟替换成⼤写字⺟。
string str = Console.ReadLine();
//a-z A-Z
int cha = 'A' - 'a';
for (int i = 0; i < str.Length; i++)
{
if (str[i] >= 'a' && str[i] <= 'z')
{
char c = (char)(str[i] + cha);
Console.Write(c);
}
else if (str[i] >= 'A' && str[i] <= 'Z')
{
char c = (char)(str[i] - cha);
Console.Write(c);
}
else
{
Console.Write(str[i]);
}
}
在⼀个数组中查找⼀个给定的值,输出第⼀次出现的位置(从1开始) 输⼊第⼀⾏是数组中的元素 第⼆⾏是要查找的数 输出输出第⼀次出现的位置 输⼊ 3 8 23 45 2 5 23 输出3
string str = Console.ReadLine();
string[] strArray = str.Split(" ");
int[] intArray = new int[strArray.Length];
for (int i = 0; i < strArray.Length; i++)
{
int n = Convert.ToInt32(strArray[i]);
intArray[i] = n;
}
int number = Convert.ToInt32(Console.ReadLine());
int location = -1;
for(int i = 0; i < intArray.Length; i++)
{
if (number == intArray[i])
{
location = i + 1;
break;
}
}
Console.WriteLine(location);
张三总爱乱花钱。
每个⽉的⽉初妈妈给张三300元钱 ,张三会预算这个⽉的花销,并且能做到实 际的花销和预算相同。为了让张三学会对⾦钱的管理,妈妈提出,张三可以随时把整百的前存在 她那⾥,到了12⽉31⽇,他会加上20%还给张三,也就是说,张三给妈妈的每⼀个100元,到年 底都会变成120元!因此张三制定了⼀个存储计划:每个⽉的⽉初,在得到妈妈给的零花钱后, 如果他预计到这个⽉的⽉末⼿中还会有多于100元或恰好100元,他就会把整百的钱存在妈妈那 ⾥,剩余的钱存在⾃⼰⼿⾥。⽐如11⽉初张三⼿中还有83元,妈妈留了张三300元。张三预计11 ⽉的花销是180元,那么他就会在妈妈那⾥存200元,⾃⼰留下183元。到了11⽉⽉末,张三⼿ 中会剩下3元钱。 张三发现这个存储计划的主要⻛险是,存在妈妈那⾥的钱在年末之前不能取出。有可能在某个⽉ 的⽉初,张三⼿中的钱加上这个⽉妈妈给的钱,不够这个⽉的预算。如果出现这种情况,张三将 不得不在这个⽉省吃简⽤,压缩预算。 现在请你根据2019年1⽉到12⽉每个⽉张三的预算,判断会不会出现这种情况。如果储蓄计划实 施过程中会出现某个⽉钱不够⽤的情况,输出-x,x表⽰出现这种情况的第⼀个⽉;如果不会,计 算到2019年12⽉31⽇,妈妈将张三平时存的钱加上20%还给张三后,张三⼿⾥会有多少钱。 样例输⼊ 240 280 200 300 120 350 80 50 270 260 样例输出 -7 样例输⼊ 290 230 280 200 300 170 130 50 90 80 200 300 样例输出 1520
using System;
namespace _035_模拟法_张三的零花钱
{
class Program
{
static void Main(string[] args)
{
string str = Console.ReadLine();
string[] strArray = str.Split(" ");
int[] budget = new int[strArray.Length];
for (int j = 0; j < strArray.Length; j++)
{
int n = Convert.ToInt32(strArray[j]);
budget[j] = n;
}
int self = 0;
int mom = 0;
int i = 0;
int tmp = 0;
for (i = 0; i < 12; i++)//0-11 i+1
{
//本月一共有多少300+self
//预算budget[i]
tmp = 300 + self - budget[i];//剩余的钱 280/100 = 2*100 =200
if (tmp < 0)
{
//i+1
break;
}
//整百的
mom += (tmp / 100)*100;
self = tmp % 100;
}
//
if (tmp < 0)
{
Console.WriteLine("-" + (i + 1));
}
else
{
self = self + (int)(mom * 1.2);
Console.WriteLine(self);
}
}
}
}
输出9x9乘法表
for (int i = 1; i <= 9; i++)
{
//i 右乘数 左乘数1-i
for (int j = 1; j <= i; j++)
{
Console.Write("{0}x{1}={2} ", j, i, i * j);
}
Console.WriteLine();
}
由系统⽣成⼀个随机数(1-100),让玩家猜数字,如果猜的数字⽐随机数⼩,输出猜⼩了,如 果猜的数字⽐随机数⼤,输出猜⼤了,猜中的话,输出猜中了,并结束游戏,没有猜中的话,就 让玩家⼀直猜。
using System;
namespace _027_猜数字
{
class Program
{
static void Main(string[] args)
{
//随机数的生成Random
Random rd = new Random();
//伪随机
//Console.WriteLine(rd.Next(1, 10));
int number = rd.Next(1, 101);
while (true)
{
Console.WriteLine("猜猜我的数字是多少:");
int numberUser = Convert.ToInt32(Console.ReadLine());
if (numberUser > number)
{
Console.WriteLine("你猜大了");
}else if (numberUser < number)
{
Console.WriteLine("你猜小了");
}
else
{
Console.WriteLine("你猜中了");
break;
}
}
}
}
}
假设有个隧道,隧道以字符 ‘#’ 结束,挖矿的过程中,会遇到钻⽯ ‘*’ 和美⾦ ‘1’ ~ ‘9’ ,让矿⼯ ⼩六挖到隧道的尽头,假设每个钻⽯价值500美⾦,统计⼩六挖到了价值多少美⾦的收获? 样例输⼊ ka4d*s6kkl8s*d9fyo# 样例输出 102
char c;// '0' -- 55 '9' --
int sum = 0;
do
{
c = (char)Console.Read();
if (c >= '0' && c <= '9')
{
//
int number = c - '0';
sum += number;
}
else if (c == '*')
{
sum += 500;
}
} while (c != '#');
Console.Write(sum);
数字和字符混合在⼀起了,作为⼀个优秀的挖掘⼈员,把输⼊的数字挖出来,并计算这些数字的 和,并输出。输⼊以@作为结束。 样例输⼊ 23a34b34@ 样例输出 19
char c = (char)Console.Read();
Console.WriteLine(c);
char c;// '0' -- 55 '9' --
int sum = 0;
do
{
c = (char)Console.Read();
if (c >= '0' && c <= '9')
{
//
int number = c - '0';
sum += number;
}
} while (c != '@');
Console.Write(sum);
班上有若⼲名学⽣,输⼊学⽣的个数,然后输⼊每⼀个学⽣的年龄,计算出来平均年龄,保留到 ⼩数点后两位,输出平均年龄。 样例输⼊: 2 20 19 样例输出 19.5
int n = Convert.ToInt32(Console.ReadLine());
int sum = 0;
int cheng = 1;// 1 10 100 1000
while (n != 0)
{
int i = n % 10;//i就是遍历各个位上的数字
if (i != 0)
{
i *= cheng;// ge 1*
sum += i;
cheng *= 10;
}
n = n / 10;
}
Console.WriteLine(sum);
已知:Sn = 1 + 1/2 + 1/3 + ... + 1/n。 显然对于任意⼀个整数k,当n⾜够⼤的时候,Sn⼤于K。 现输⼊⼀个整数K(1≤K≤15),要求计算出⼀个最⼩的n,使得Sn>K。 输⼊⼀个整数K,输出⼀个整数n。 样例输⼊1,样例输出2
int n = Convert.ToInt32(Console.ReadLine());
//标志位,标志是否达到了某个条件
bool yudao = false;// 默认还没有遇到第一个非0
while (n != 0)//从低位到高位遍历
{
int i = n % 10;//23407800 i = 0 0 8 7 0 4 3 2
//判断是否遇到了第一个非0数字
//已经遇到了第一个非0数字
//不需要去掉0了
//还没有遇到第一个非0数字
//去掉
if (yudao == false)//还没有遇到第一个非0数字
{
if (i != 0)//i=8
{
Console.Write(i);
yudao = true;
}
}
else //i=7
{
Console.Write(i);
}
//if (i != 0)
//{
// Console.Write(i);
//}
n = n / 10;
}