day 2020 / 02 / 26
1.(for循环)计算1+2+3+…+100的和
2.(for循环)计算1+3+5+…+99的和。
3.(while/dowhile循环)把1、2两题用while循环或do…while循环改写。
4.(for循环)读入一个小于10的整数n,输出它的阶乘n。
5.(for循环)求100以内所有能被3整除但不能被5整除的数字的和。
import java.util.Scanner;
public class MyHomeWork5 {
public static void main(String[] args) {
// 第一题
int cont = 0;
for (int i = 1; i < 101; i++) {
cont = cont + i;
}
System.out.println("从1加到100=" + cont);
// 第二题
int cont2 = 0;
for (int i = 1; i < 100; i = i + 2) {
cont2 = cont2 + i;
}
System.out.println("1+3+5+...+99=" + cont2);
// 第三题
int i = 1;
int cont3 = 0;
while (i <= 100) {
cont3 = cont3 + i;
i++;
}
System.out.println("从1加到100=" + cont3);
// 第四题
Scanner in = new Scanner(System.in);
System.out.println("请输入一个数n");
int n = in.nextInt();
int cont4 = 1;
i = 1;
while (i <= n) {
cont4 = cont4 * i;
i++;
}
System.out.println(n + "的阶乘=" + cont4);
// 第五题
int cont5 = 0;
for (i = 1; i < 101; i++) {
if (i % 3 == 0 && i % 5 != 0) {
cont5 = cont5 + i;
continue;
}
}
System.out.println("100以内被3整除不被5整除的数的和为" + cont5);
}
}
6.(for循环)“百钱买百鸡”是我国古代的著名数学题。题目描述:3文钱可以买1只公鸡,2文钱可以买一只母鸡,1文钱可以买3只小鸡。用100文钱买100只鸡,那么各有公鸡、母鸡、小鸡多少只?
7.(for循环)搬砖问题:36块砖,36人搬,男搬4,女搬3,两个小孩抬1砖,要求一次全搬完,问
男、女和小孩各若干?
8.(for循环)编程找出四位整数abcd中满足(ab+cd)(ab+cd)=abcd的数。
9.(循环)*读入一个整数n,输出如下图形n=3
10.(for循环)输出99乘法表。
import java.util.Scanner;
public class MyInformation {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 第六题
int gongji, muji, xiaoji;
for (gongji = 1; gongji <= 33; gongji++) {
for (muji = 1; muji <= 50; muji++) {
for (xiaoji = 3; xiaoji <= 100; xiaoji = xiaoji + 3) {
if (xiaoji / 3 + muji * 2 + gongji * 3 == 100) {
System.out.println("公鸡" + gongji + "母鸡" + muji + "小鸡" + xiaoji);
}
}
}
}
// 第七题
int man, woman, kid;
for (man = 1; man <= 9; man++) {
for (woman = 1; woman <= 12; woman++) {
for (kid = 2; kid <= 36; kid = kid + 2) {
if (kid / 2 + woman * 3 + man * 4 == 36) {
System.out.println("男人" + man + "女人" + woman + "小孩" + kid);
}
}
}
}
// 第八题
for (int i = 1000; i <= 9999; i++) {
int ge = i % 10;
int shi = i / 10 % 10;
int bai = i / 100 % 10;
int qian = i / 1000;
if (((qian * 10 + bai) + (shi * 10 + ge)) * ((qian * 10 + bai) + (shi * 10 + ge)) == i) {
System.out.println(i);
}
}
// 第九题
System.out.println("请输入一个数字");
int n = in.nextInt();
for (int i = 1; i <= n; i++) {
for (int j = n - 1; j >= i; j--) {
System.out.print(" ");
}
for (int j = 1; j <= 2 * i - 1; j++) {
System.out.print("*");
}
System.out.println();
}
// 第十题
for (int i = 1; i < 10; i++) {
for (int j = 1; j < 10; j++) {
if (i >= j) {
System.out.print(i + "x" + j + "=" + i * j + " ");
}
}
System.out.println();
}
}
}