目录
编程找出四位整数abcd 中满足下述关系的数:(ab+cd)(ab+cd)=abcd
faibonacci(费波那契)数列,编程求出此数列的前n 项。
一个int 类型的整数由32 个二进制位组成,每个二进制位的值要么为0要么为1。要求读入一个int 类型的整数n,计算它的32 个二进 制位中总共有多少位为1?
读入一个表示年份的整数,判断这一年是否是闰年。如何判断一个年份是否是闰年:
1)如果这个年份能够被4 整除,且不能被100 整除,则这一年是闰年。例如, 1996 年是闰年,而相应的, 1993 年就不是闰年。
2)如果这个年份能够被100 整除,则这个数必须要能被400 整除,才是闰年。例如, 2000 年是闰年, 1900 年不是闰年
import java.util.Scanner;
public class test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int year = sc.nextInt();
if(year%4==0&&year%100!=0){
System.out.println(year+"年是闰年");
}else if (year%100==0&&year%400==0) {
System.out.println(year+"年是闰年");
}else{
System.out.println(year+"年不是闰年");
}
}
}
完成一个简单的计算器程序。程序要求如下:
1)读入两个整数
2) 提示用户选择对这两个整数的操作,即输出 1 : + 2 : 3 : * 4 : /
请输入您的选择:
读入用户的选择,输出运算结果。
import java.util.Scanner;
public class test5{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double a = sc.nextInt();
double b = sc.nextInt();
System.out.println("+、-、*、/");
String x = sc.next();
double c ;
switch (x) {
case "+":
c = a+b;
System.out.println(a+"+"+b+"="+c);
break;
case "-":
c = a-b;
System.out.println(a+"-"+b+"="+c);
break;
case "*":
c = a*b;
System.out.println(a+"*"+b+"="+c);
break;
case "/":
c = a/b;
System.out.println(a+"/"+b+"="+c);
break;
default:
System.out.println("error");
break;
}
}
}
输入一个数n,输出它的阶乘n!
import java.util.Scanner;
public class test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
int y= 1;
for (int i = 1; i <= x; i++) {
y *= i;
}
System.out.println(y);
}
}
“百钱买百鸡”是我国古代的著名数学题。题目这样描述:3文钱可以买1只公鸡,2文钱可以买一只母鸡,1文钱可以买3只小鸡。用100 文 钱买100 只鸡,那么各有公鸡、母鸡、小鸡多少只?
public class test {
public static void main(String[] args) {
for(int a=0;a<34;a++){
for(int b=0;b<=50;b++){
for(int c=0;c<=100;c++){
if(((3*a)+(2*b)+(c/3))==100&&(a+b+c)==100&&c%3==0){
System.out.println("公鸡数为:"+a+" 母鸡数为:"+b+" 小鸡数为:"+c);
}
}
}
}
}
}
搬砖问题:36块砖,36人搬,男搬4,女搬3,两个小孩抬1砖,要求一次全搬完,问男、女和小孩各若干?
public class test {
public static void main(String[] args) {
for(int a = 1;a<=36;a++){
for(int b = 1;b<=36;b++){
for(int c = 1;c<=36;c++){
if (((4*a)+(3*b)+(0.5*c))==36&&a+b+c==36) {
System.out.println("男人有:"+a+"人 女人有:"+b+"人 小孩有"+c+"人");
}
}
}
}
}
}
编程找出四位整数abcd 中满足下述关系的数:(ab+cd)(ab+cd)=abcd
public class test {
public static void main(String[] args) {
for(int i = 1000;i<=9999;i++){
int a = i/1000;
int b = (i%1000)/100;
int c = (i%100)/10;
int d = i%10;
int x = ((a*10)+b)+((c*10)+d);
if (x*x==(a*1000)+(b*100)+(c*10)+d) {
System.out.println(i);
}
}
}
}
输出99 乘法表
public class test {
public static void main(String[] args) {
for(int i = 1;i<=9;i++){
for(int j = 1;j<=i;j++){
System.out.print(j+"*"+i+"="+i*j+"\t");
}
System.out.println(" ");
}
}
}
求水仙花数。所谓水仙花数,是指一个三位数abc,如果满足a3 + b3 + c3 = abc,则abc是水仙花数。
public class test {
public static void main(String[] args) {
for(int i=100;i<=999;i++){
int a = i/100;
int b = i%100/10;
int c = i%10;
if((a*a*a)+(b*b*b)+(c*c*c)==(a*100+b*10+c)){
System.out.println(i);
}
}
}
}
输入一个整数, 计算它各位上数字的和。(注意:是任意位的整数
import java.util.Scanner;
public class test {
public static void main(String[] args) {
int sum = 0;
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
while(x!=0){
sum += x%10;
x = x/10;
}
System.out.println(sum);
}
}
输入一整数,判断它是否质数。
import java.util.Scanner;
public class test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
int n = 0;
if (x<2) {
System.out.println(x+"不是质数");
}else{
for(int i = 2;i<x;i++){
if (x%i==0) {
System.out.println(x+"不是质数");
n++;
break;
}
}
if (n==0) {
System.out.println(x+"是质数");
}
}
}
}
如果一个数等于其所有因子之和,我们就称这个数为"完数",例如6 的因子为1,2,3,6=1+2+3,6就是一个完数.请编程打印出1000 以内所有的完数
public class test {
public static void main(String[] args) {
int a =0;
for(int i=2;i<1000;i++){
int sum = 1;
for (int j = 2; j< i; j++) {
if (i%j==0) {
a = j;
sum += a;
}
}
if (i==sum) {
System.out.println(i);
}
}
}
}
计算圆周率:中国古代数学家研究出了计算圆周率最简单的办法:PI=4/1-4/3+4/5-4/7+4/9-4/11+4/13-4/15+4/17......这个算式 的结果会无限接近于圆周率的值,我国古代数学家祖冲之计算出,圆周率在3.1415926和3.1415927 之间,请编程计算,要想得到这样的结 果,他要经过多少次加减法运算?
public class test {
public static void main(String[] args) {
double PI = 0;
int a = 1;
double b = 4.0;
int c = 3;
int num = 0;
while(PI<3.1415926||PI>3.1415927){
PI = PI + (b/a);
num +=1;
a+=4;
PI = PI - (b/c);
num +=1;
c+=4;
//System.out.println(PI);
}
System.out.println(num);
}
}
已知:faibonacci(费波那契)数列的前几个数分别为0,1,1,2,3,5……。从第3 项开始,每一项都等于前两项的和。读入一个整数 n,编程求出此数列的前n 项。
import java.util.Scanner;
public class test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
long a = 0;
long b = 1;
long c ;
if(n==1){
System.out.println(a);
}else if (n==2) {
System.out.println(a);
System.out.println(b);
}else {
System.out.println(a);
System.out.println(b);
for(int i = 1;i<=n-2;i++){
c = a+b;
a = b;
b = c;
System.out.println(c);
}
}
}
}
一个int 类型的整数由32 个二进制位组成,每个二进制位的值要么为0要么为1。要求读入一个int 类型的整数n,计算它的32 个二进 制位中总共有多少位为1?
方法一:
import java.util.Scanner;
public class test23 {
public static void main(String[] args) {
System.out.print("请输入一个int类型的整数:");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
//计数变量
int count = 0;
//通过for循环实现
for(int i = 0;i<32;i++){
//先进行移位
int t = n>>i;
//让移位后的数与1做按位与
if((t&1)==1){
count++;
}
}
System.out.println(count);
}
}
方法二:
public class test23 {
public static void main(String[] args) {
System.out.print("请输入一个int类型的整数:");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int sum = 0;
int n1 = n;
while(n != 0) {
sum = sum + n % 2;
n = n / 2;
}
System.out.println(n1 + "的二进制位中总共有" + sum + "位为1");
}
}