A:
1、编程将所有“水仙花数”打印出来,并打印其总个数。“水仙花数”是一个各个位立方之和等于该整数的三位数。
for (int i = 100; i < 1000; i++) {
int a, b, c;
a = (i / 100) % 10;
b = (i / 10) % 10;
c = i % 10;
if (a*a*a +b*b*b +c*c*c == i) {
Console.WriteLine ("{0}",i);
}
}
int sum = 0;
for (int i = 100; i < 1000; i++) {
int a = (i / 100) % 10;
int b = (i / 10) % 10;
int c = i % 10;
if (Math.Pow(a,3)+ Math.Pow(b,3)+ Math.Pow(c,3) == i) {
sum++;
Console.WriteLine ("{0}",i);
}
Console.WriteLine(sum);
}
2、自定义一个数组并求数组中的所有元素最大值、最小值、平均值及各元素之和。
float[] num = {1,2,3,4,5};
float max = 0;
float min = num[0];
float avg = 0;
float sum = 0;
for (int i = 0; i < num.Length; i++) {
if (num[i]>=max) { //max = max
max = num[i];
}
if (num[i]<=min) { //min = min >num[i]?num[i]:min;
min = num[i];
}
sum += num [i];
}
avg = sum / num.Length;
Console.WriteLine ("max={0},min={1},sum={2},ave={3}",max,min,sum,avg);
3、已知abc+cba = 1333,其中a,b,c均为一位数,编程求出满足条件的a,b,c所有组合。
for (int a = 1; a < 10; a++) {
for (int b = 0; b < 10; b++) {
for (int c = 1; c < 10; c++) {
if ((a*100 + b*10 +c)+(c*100 +b*10 +a) == 1333) {
Console.WriteLine ("a={0},b={1},c={2}",a,b,c);
}
}
}
}
4、打印100以内的所有质数. (只能被1和它本身整除的数是质数)
for (int i = 2; i < 100; i++) {
bool tag = false;
for (int j = 2; j <=i/2; j++) {
if (i%j == 0) {
tag = true;
break;
}
}
if (!tag) {
Console.WriteLine ("{0}:是质数",i);
}
}
5、输入两个数,求最大公约数和最小公倍数。(有两种方法:辗转相除法和普通方法)
普通方法
int a = int.Parse(Console.ReadLine());
int b = int.Parse(Console.ReadLine());
int min = a < b ? a : b;
int max_grs = 0;
for (int i = 1; i <=min; i++) {
if (a%i == 0 && i == 0) {
max_grs = i;
}
}
Console.WriteLine ("最大公约数为:"+ max_grs);
Console.WriteLine ("最小公倍数为:"+ (a*b)/max_grs);
//辗转相除法
//120 56
Console.WriteLine("请输入一个数:");
int num1 = int.Parse(Console.ReadLine());
Console.WriteLine("请输入另一个数:");
int num2 = int.Parse(Console.ReadLine());
int a = num1; int b = num2;
int temp;
do {
temp = a%b; //120%56=8 56%8=0
a = b; // a=56 a = 8
b = temp; // b=8 b = 0(循环停止)
} while (b != 0);
Console.WriteLine ("最大公约数为:{0}",a);
Console.WriteLine ("最小公倍数为:{0}",(num1*num2)/a);
6、一个球从100m高度自由落下,每次落地后反跳回原来高度的一半,再落下,再反弹。
求它在第10次落地时,共经过多少米?第10次反弹多高
float sum, hight;
sum = 100.0f;
hight = 100.0f;
for (int i = 2; i <=10; i++) {
hight /= 2.0f;
sum += hight * 2;
}
Console.WriteLine ("{0},{1}",sum,hight/2);
B:
7、百钱百鸡问题:鸡翁一值钱5;母鸡一值钱3;鸡雏三值钱1,百钱买百鸡,问鸡翁鸡母鸡雏各几何?
//5x +3y +z/3 = 100;
//x + y + z = 100; =>z = 100-(x+y);
//15x +9y +100 -(x+y)=300; =>14x +8y = 200;
for (int x = 0; x <= 20; x++) {
for (int y = 0; y < 34 ; y++) {
if (14*x +8*y == 200) {
Console.WriteLine("x={0},y ={1},z= {2}",x,y,100-(x+y));
}
}
}
8、输入n,分别用*输出边长为n的实心菱形。例如:n = 3时,输出
i 空格j *
* -2 2 1 (2n-1)-2*|i|
* * * -1 1 3
* * * * * 0 0 5
* * * 1 1 3
* 2 2 1
int n = int.Parse (Console.ReadLine());
for (int i = -n+1; i <=n-1; i++) {
for (int j = 0; j < Math.Abs(i); j++) {
Console.Write (" ");
}
for (int k = 0; k < (2*n-1)-2*Math.Abs(i); k++) {
Console.Write ("*");
}
Console.WriteLine ();
}
思考打印题8为空心棱形
int n = int.Parse (Console.ReadLine()); *
for (int i = -n+1; i <=n-1; i++) {
for (int j = 0; j < Math.Abs(i); j++) {
Console.Write (" ");
}
for (int k = 0; k < (2*n-1)-2*Math.Abs(i); k++) {
if (k == 0 || k == (2*n-2)-2*Math.Abs(i)) {
Console.Write ("*");
} else {
Console.Write (" ");
}
}
Console.WriteLine ();
}
*
* *
* *
* *
*
//