知识点:
1、如何读取⼀个字符输⼊
c#char a = (char)Console.Read();
2、输⼊缓冲区 (代码、输入缓冲区、控制台)
3、break跳出循环(循环和switch)
中止整个循环
✋假设有个隧道,隧道以字符 ‘#’ 结束,挖矿的过程中,会遇到钻⽯ ‘*’ 和美⾦ ‘1’ ~ ‘9’ ,让矿⼯⼩六挖到隧道的尽头,假设每个钻⽯价值500美⾦,统计⼩六挖到了价值多少美⾦的收获?
样例输⼊ ka4d*s6kkl8s*d9fyo#
样例输出 1027
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;
} else if (c == '*') {
sum += 500;
}
} while (c != '#');
Console.Write(sum);
✋输⼊⼀个整数,输出该整数的因数个数和所有因数。
输⼊的整数⼤于0,⼩于100000
样例输⼊ 9
样例输出
3
1 3 9
c#int n = Convert.ToInt32(Console.ReadLine());
int count = 0;
String str = "";
for (int i = 1; i <= n; i++) {
if (n % i == 0) {
count++;
str += i + " ";
}
}
Console.WriteLine(count);
Console.WriteLine(str);
概念:
因数是可以把这个数整除的数,任何⼀个⾮1正整数都⾄少有两个因数1和它本⾝。
只有1和⾃⾝这两个因数的⾮1正整数 成为质数(素数)
✋输⼊⼀个正整数,判断该数是否是质数。
输⼊的数字⼤于0,⼩于1000000
如果为质数输出 yes,如果不是输出no
样例输⼊103
输出yes
c#int n = Convert.ToInt32(Console.ReadLine());
int count = 0;
for (int i = 1; i <= n; i++) {
if (n % i == 0) {
count++;
}
}
if (count == 2) {
Console.WriteLine("yes");
} else {
Console.WriteLine("no");
}
学习continue
✋输出1-100中所有的奇数,使⽤continue。
c#//方法1:
for (int i = 1; i <= 100; i++) {
if (i % 2 == 1) {
Console.WriteLine(i);
}
}
break;
//方法2:
for (int i = 1; i <= 100; i++) {
if (i % 2 == 0) {
continue; //中止当前循环,继续下次循环
//break;
}
Console.WriteLine(i);
}
本文作者:千纸鹤
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!