数字处理相关编程题
来源:互联网 发布:阿里云服务器开放3306 编辑:程序博客网 时间:2024/06/05 10:17
数字方面:
1、判断随机整数是否是素数
产生100个0-999之间的随机整数,然后判断这100个随机整数哪些是素数,哪些不是?
public class PrimeTest{
public static void main(String args[]){
for(int i=0;i<100;i++){
int num = (int)(Math.random()*1000);
PrimeTest t = new PrimeTest();
if(t.isPrime(num)){
System.out.println(num+" 是素数!");
}else{
System.out.println(num+" 不是素数!");
}
System.out.println();
}
}
public boolean isPrime(int num){
for(int i=2;i<=num/2;i++){
if(num%i==0){
System.out.println(num+"第一个被"+i+"整除!");
return false;
}
}
return true;
}
}
2、求 a+aa+aaa+.......+aaaaaaaaa=?
其中a为1至9之中的一个数,项数也要可以指定。
import java.util.Scanner;
class Multinomial{
public static void main(String[] args){
int a; //定义输入的 a
int howMany; //定义最后的一项有多少个数字
Scanner scanner = new Scanner(System.in);
System.out.println("请输入一个 1~9 的 a 值");
a = scanner.nextInt();
System.out.println("请问要相加多少项?");
howMany = scanner.nextInt();
int sum=0;
int a1=a; // 用来保存 a 的初始值
for (int i=1; i<=howMany; i++){
sum+= a;
a = 10*a +a1; // 这表示a 的下一项
// 每次 a 的下一项都等于前一项*10,再加上刚输入时的 a ;注意,这时的 a 已经变化了。
}
System.out.println("sum="+sum);
}
}
3、计算圆周率
PI=4-4/3+4/5-4/7.......
打印出第一个大于 3.1415小于 3.1416的值
class Pi {
public static void main(String[] args){
double pi =0; //定义初始值
double fenZi = 4; //分子为4
double fenMu = 1; //第一个4,可看作分母为1 的分式,以后的分母每次递增2
for (int i = 0; i < 1000000000; i++){ //运行老久,减少循环次数会快很多,只是精确度小些
pi += (fenZi/fenMu) ;
fenZi *= -1.0; //每项分子的变化是+4,-4,+4,-4 ....
fenMu += 2.0; //分母的变化是1,3,5,7, .... 每项递加2
}
System.out.println(pi);
}
}
输出结果为pi = 3.1415926525880504,应该不精确
4、输入一个数据n,计算斐波那契数列(Fibonacci)的第n个值
1 1 2 3 5 8 13 21 34
规律:一个数等于前两个数之和
//计算斐波那契数列(Fibonacci)的第n个值
public class Fibonacci{
public static void main(String args[]){
int n = Integer.parseInt(args[0]);
int n1 = 1;//第一个数
int n2 = 1;//第二个数
int sum = 0;//和
if(n<=0){
System.out.println("参数错误!");
return;
}
if(n<=2){
sum = 1;
}else{
for(int i=3;i<=n;i++){
sum = n1+n2;
n1 = n2;
n2 = sum;
}
}
System.out.println(sum);
}
}
//计算斐波那契数列(Fibonacci)的第n个值
//并把整个数列打印出来
public class FibonacciPrint{
public static void main(String args[]){
int n = Integer.parseInt(args[0]);
FibonacciPrint t = new FibonacciPrint();
for(int i=1;i<=n;i++){
t.print(i);
}
}
public void print(int n){
int n1 = 1;//第一个数
int n2 = 1;//第二个数
int sum = 0;//和
if(n<=0){
System.out.println("参数错误!");
return;
}
if(n<=2){
sum = 1;
}else{
for(int i=3;i<=n;i++){
sum = n1+n2;
n1 = n2;
n2 = sum;
}
}
System.out.println(sum);
}
}
5、求1-1/3+1/5-1/7+1/9......的值。
a,求出前50项和值。
b,求出最后一项绝对值小于1e-5的和值。
6、歌德巴赫猜想,任何一个大于六的偶数可以拆分成两个质数的和打印出所有的可能
//任何一个大于六的偶数可以拆分成两个质数的和
//打印出所有的可能
public class Gedebahe{
public static void main(String args[]){
int num = Integer.parseInt(args[0]);
if(num<=6){
System.out.println("参数错误!");
return;
}
if(num%2!=0){
System.out.println("参数错误!");
return;
}
Gedebahe g = new Gedebahe();
//1不是质数,2是偶数,因此从3开始循环
for(int i=3;i<=num/2;i++){
if(i%2==0){//如果为偶数,退出本次循环
continue;
}
//当i与num-i都为质数时,满足条件,打印
if(g.isPrime(i) && g.isPrime(num-i)){
System.out.println(i+" + "+(num-i)+" = "+num);
}
}
}
7、让数值在 0~9 之间循环。
public class test{
public static void main(String[] args){
int i=0;
while(true){
i = (i+1)%10;
System.out.println(i);
}
}
}
8、编写程序,判断给定的某个年份是否是闰年。
闰年的判断规则如下:
(1)若某个年份能被4整除但不能被100整除,则是闰年。
(2)若某个年份能被400整除,则也是闰年。
import java.util.Scanner;
class Bissextile{
public static void main(String[] arge){
System.out.print("请输入年份");
int year; //定义输入的年份名字为“year”
Scanner scanner = new Scanner(System.in);
year = scanner.nextInt();
if (year<0||year>3000){
System.out.println("年份有误,程序退出!");
System.exit(0);
}
if ((year%4==0)&&(year%100!=0)||(year%400==0))
System.out.println(year+" is bissextile");
else
System.out.println(year+" is not bissextile ");
}
}
9、给定一个百分制的分数,输出相应的等级。
90分以上 A级
80~89 B级
70~79 C级
60~69 D级
60分以下 E级
import java.util.Scanner;
class Mark{
public static void main(String[] args){
System.out.println("请输入一个分数");
//定义输入的分数为“mark”,且分数会有小数
double mark;
Scanner scanner = new Scanner(System.in);
mark = scanner.nextDouble();
//判断是否有输入错误。
if(mark<0||mark>100){
System.out.println("输入有误! ");
System.exit(0);
}
/*判断分数的等级
90分以上者A级, 80~89分者 B级,70~79分者 C级, 60~69者 D级,60分以下 E级 */
if (mark>=90) System.out.println("this mark is grade \'A\' ");
else if (mark>=80) System.out.println("this mark is grade \'B\' ");
else if (mark>=70) System.out.println("this mark is grade \'C\' ");
else if (mark>=60) System.out.println("this mark is grade \'D\' ");
else System.out.println("this mark is grade \'E\' ");
}
}
10、输出所有的水仙花数,把谓水仙花数是指一个数3位数,其各各位数字立方和等于其本身,
例如: 153 = 1*1*1 + 3*3*3 + 5*5*5
class DafodilNumber{
public static void main(String[] args){
System.out.println("以下是所有的水仙花数");
int number = 100; // 由于水仙花数是三位数,故由100开始算起
int i, j, k; // i j k 分别为number 的百位、十位、个位
for (int sum; number<1000; number++){
i=number/100;
j=(number-i*100)/10;
k=number-i*100-j*10;
sum=i*i*i+j*j*j+k*k*k;
if (sum==number)
System.out.println(number+" is a dafodil number! ");
}
}
}
11、一个数如果恰好等于它的因子之和,这个数就称为“完数”。分别编写一个应用程序和小应用程序求 1000之内的所有完数。
public class Wanshu {
public static void main(String[] args) {
int sum=0,i,j;
for(i=1;i<=1000;i++)
{
for(j=1,sum=0;j<i;j++)
{
if(i%j==0)
sum=sum+j;
}
if(sum==i)
{
System.out.print (“完数:”+i+” ”+”其因子是:“ );
for(int k=1;k<=sum/2;k++)
{
if(sum%k==0)
System.out.print(“ ”+k);
}
System.out.println();
}
}
}
}
12、编写程序,求2-1000内的所有素数,并按每行5列的格式输出。
public class PrimeTest{
public static void main(String args[]) {
int num=2;
System.out.print(2 + ” ”);
for(int i=3;i<=1000;i+=2){
boolean f = true;
for (int j=2;j<i;j++) {
if(i % j == 0){
f= false;
break;
}
}
if(!f) {continue;}
System.out.print(i + ” ”);
if(num++%5 == 0)System.out.println();
}
}
}
13、编写程序,生成100个1~6之间的随机数,统计1~6每个数字出现的概率。
public class RandomTest {
public static void main(String[] args) {
int[] randomnum = new int[100];
int[] n = new int[6];
double a;
for (int i = 0; i < 100; i++) {
a = Math.random() * 6;
a = Math.ceil(a);
randomnum[i] = new Double(a).intValue();
System.out.print(randomnum[i]);
switch (randomnum[i]) {
case 1:
n[0]++;
break;
case 2:
n[1]++;
break;
case 3:
n[2]++;
break;
case 4:
n[3]++;
break;
case 5:
n[4]++;
break;
case 6:
n[5]++;
break;
}
}
System.out.println();// 以下可改为循环输出
System.out.println(" 数字1出现的概率="+(n[0]/100.0)*100+"%");
System.out.println(" 数字2出现的概率="+(n[1]/100.0)*100+"%");
System.out.println(" 数字3出现的概率="+(n[2]/100.0)*100+"%");
System.out.println(" 数字4出现的概率="+(n[3]/100.0)*100+"%");
System.out.println(" 数字5出现的概率="+(n[4]/100.0)*100+"%");
System.out.println(" 数字6出现的概率="+(n[5]/100.0)*100+"%");
}
}
14、编写程序,求1!+2!+3!+…+15!。
public class FactorialSum {
static int f(int x) {
if (x <= 0)
return 1;
else
return x * f(x - 1);
}
public static void main(String[] args) {
int sum = 0;
for (int j = 1; j <= 3; j++) {
sum += f(j);
}
System.out.println(sum);
}
}
15、编写程序,分别用do-while和for循环计算1+1/2!+1/3!+1/4!+…的前15项的和。
for循环代码:
public class For_FactorialSum {
static int f(int x) {
if (x <= 0)
return 1;
else
return x * f(x - 1);
}
public static void main(String[] args) {
double sum = 0;
for (int j = 1; j <= 15; j++) {
sum += 1.0 / f(j);
}
System.out.println(sum);
}
}
do-while循环代码:
public class DoWhile_FactorialSum {
static int f(int x) {
if (x <= 0)
return 1;
else
return x * f(x - 1);
}
public static void main(String[] args) {
double sum = 0;
int j = 1;
do {
sum += 1.0 / f(j);
j++;
} while (j <= 15);
System.out.println(sum);
}
}16.编写一个程序,用parseInt方法将字符串200由十六进制转换为十进制的int型数据,用valueOf方法将字符串123456转换为float型数据。
public class Ex16 {
public static void main(String[] args) {
String str1 = "200";
System.out.println(Integer.parseInt(str1, 16));
String str2 = "123456";
System.out.println(Float.parseFloat(str2));
}
}17.编写程序,将long型数据987654转换为字符串,将十进制数365转换为十六进制数表示的字符串。
参考代码:
public class Ex17 {
public static void main(String[] args) {
long num=987654L;
int i=365;
System.out.println(“Long类型转换为String:“+String.valueOf(num));
String HexI=DtoX(i);
System.out.println(HexI);
}
//转换函数
public static String DtoX(int d)
{
String x=”";
if(d<16){
x=change(d);
}
else{
int c;
int s=0;
int n=d;
while(n>=16){
s++;
n=n/16;
}
String [] m=new String[s];
int i=0;
do{
c=d/16;
//判断是否大于10,如果大于10,则转换为A-F的格式
m[i++]=change(d%16);
d=c;
}while(c>=16);
x=change(d);
for(int j=m.length-1;j>=0;j–){
x+=m[j];
}
}
return x;
}
//判断是否为10-15之间的数,如果是则进行转换
public static String change(int d){
String x=”";
switch(d){
case 10:
x=”A”;
break;
case 11:
x=”B”;
break;
case 12:
x=”C”;
break;
case 13:
x=”D”;
break;
case 14:
x=”E”;
break;
case 15:
x=”F”;
break;
default:
x=String.valueOf(d);
}
return x;
}
}
18.编写一个程序,接收以克为单位的一包茶叶的单位重量、卖出的包数和每克的价格,计算并显示出销售的总额。其中三个数据一行输入,数据间用“-”分隔。比如:输入“3-100-2.1”,表示每包的重量为3克,共卖出100包,每克的价格为2.1元。此时的销售总额为630元。
import java.util.Scanner;
public class Ex18 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("请依次输入重量、包数、价格,并以-分隔:");
String strIn = scan.nextLine();
Scanner sc = new Scanner(strIn);
sc.useDelimiter("-"); // 设置分隔符
int num = sc.nextInt();
int bag = sc.nextInt();
float price = sc.nextFloat();
double total = price * num * bag;
System.out.println("销售总额为:" + total);
}
}
19、编写程序将34.5和68.4两个数相加,并将结果显示成以下形式:x+y=34.5+68.4=***.*
public class test {
public static void main(String[] args) {
float x=34.5f, y=68.4f;
System.out.println( ”x+y=”+x+”+”+y+”=”+(x+y));
20、计算1-2+3-4+5-6...n n足够大(考虑效率)
private static void result(int n) {
int i = n % 2;
int result = 0;
if (i == 0) {
result = n / 2 * (-1);
System.out.println(result);
} else {
result = n / 2 * (-1) + n;
System.out.println(result);
}
}
21、求两数的最大公约数和最小公倍数
import java.util.Scanner;
public class C {
//辗转相除法
public static int fun1(int a1, int b1) {
int c;
c = a1 % b1;
while (c > 0) {
a1 = b1;
b1 = c;
c = a1 % b1;
}
return b1; // 把return写在这里;
}
public static void main(String[] args) {
int a, b, temp;
int m;
Scanner in = new Scanner(System.in);
a = in.nextInt();
b = in.nextInt();
if (a < b) {
temp = a;
a = b;
b = temp;
}
m = fun1(a, b);
//最大公约数使用的是辗转相除
System.out.println("最大公约数为:" + m);
//最小公倍数即两数之积与最大公约数的商
System.out.println("最小公倍数为:" + (a * b) / m);
}
}
22、判断1-100每个数是不是素数 将结果输出
public class ZhiShu {
public static void main(String[] args) {
for (int k = 1; k <= 100; k++) {
if (isZhiShu(k)) {
System.out.println(k + "是素数");
} else {
System.out.println(k + "不是素数");
}
}
}
private static boolean isZhiShu(int n) {
int temp = n / 2;
for (int j = 2; j <= temp; j++) {
if (n % j == 0) {
return false;
}
}
return true;
}
}
23、从键盘输入一个整数使用非递归计算1*2*3*4*5*n=?即n!
public class JieCheng {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = Integer.parseInt(scanner.next());
int result = 1;
for(int i = 1; i <= n; i ++){
result = result * i;
}
System.out.println(result);
}
}
使用递归算法计算1*2*3*4*5*n=?即n!
public class JieCheng2 {
public static void main(String[] args) {
System.out.println(result(2));
}
public static int result(int n) {
int result = 1;
if (1 == n) {
result = 1;
} else {
result = n * result(n - 1);
}
return result;
}
}
24、求 2/1+3/2+5/3+8/5+13/8.....前20项之和?
class Sum{
public static void main(Sting[] args){
double sum=0;
double fenZi=2.0, fenMu=1.0; //初始的分子 (fenZi)=2,分母(fenMu)=1
for(int i=1; i<=20; i++){
sum += fenZi / fenMu ;
fenMu = fenZi; //下一项的分母 = 上一项的分子
fenZi += fenMu; //下一项的分子 = 上一项的分子加分母
}
System.out.println("sum= "sum);
}
}
- 数字处理相关编程题
- Java 数字的相关处理工具类
- [编程题]数字分类
- [编程题] 数字翻转
- [编程题]数字颠倒
- [编程题] 数字游戏
- [编程题] 数字翻转
- [编程题] 数字翻转
- SAP ABAP编程 字符串相关处理
- 网络编程1-URL相关的处理
- 数字多媒体处理系统(multimedia)相关概念整理
- 集合相关编程题
- [编程题] 有趣的数字
- [编程题] 有趣的数字
- [编程题] 有趣的数字
- 网易编程题--数字翻转
- [编程题]数字分类 (20)
- 网易 编程题 数字翻转
- SQL Server T-SQL高级查询
- Duilib 使用IE
- SQL Server 索引和视图
- 【转】win32 application 和win32 console application 的区别
- [Quora] What is the most elegant line of code you've seen?
- 数字处理相关编程题
- Android中使用隐藏API(大量图解)
- SQL Server 存储过程
- iphone5和iphone4的屏幕兼容问题
- SQL Server 事务、异常和游标
- B/S和C/S的区别
- Error link2001:unresolved external symbol _main ...(不少编程初学者遇到的问题)
- 关于位运算x&(x-1)等
- OpenCV训练分类器制作xml文档