就你出的題來說,沒看懂,就java來說表示起始2,一直到N做循環
import java.util.Scanner;
public class FunNumber {
public static void main(String[] args) {
Scanner fin = new Scanner(System.in);
int N = fin.nextInt();
long i = (long)Math.pow(10, N-1);
long j = (long)Math.pow(10, N);
long count = 0;
for(;i<j;i++){
if(isFun(i,N)) {
count++;
System.out.println(i);
}
}
System.out.println("Count % 1000000007 = " + count%1000000007);
}
public static boolean isFun(long num,int N){
boolean[] num_exist = new boolean[4];
for(;N>0;N--){
int i = (int)(num%Math.pow(10,N)/Math.pow(10,N-1));
switch(i){
case 0:
num_exist[0] = true;
//0必須都在1前
if(num_exist[1]){
return false;
}
break;
case 1:
num_exist[1] = true;
break;
case 2:
num_exist[2] = true;
//2必須都在3前
if(num_exist[3]){
return false;
}
break;
case 3:
num_exist[3] = true;
break;
default:
//不只包含0123
return false;
}
}
//0123至少出現1次
if(num_exist[0]&&num_exist[1]&&num_exist[2]&&num_exist[3]){
return true;
}
return false;
}
}
『貳』 一道簡單的java編程題
import java.text.ParseException;
import java.text.SimpleDateFormat;
//日期類
public class Date {
private String year;
private String month;
private String day;
public Date(String year, String month, String day) {
this.year = year;
this.month = month;
this.day = day;
}
public void format(){
System.out.println(day + "/" + month + "/" + year);
}
public void calculate(){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
try {
java.util.Date startDate = sdf.parse(year + "/" + "01" + "/" + "01");
java.util.Date inputDate = sdf.parse(year + "/" + month + "/" + day);
long resultDay = (inputDate.getTime() - startDate.getTime())/(24 * 1000 * 60 * 60);
System.out.println("第" + (resultDay + 1) + "天");
} catch (ParseException e) {
e.printStackTrace();
}
}
}
//測試類
public class Test {
public static void main(String[] args) {
Date date1 = new Date("2020","04","11");
Date date2 = new Date("2020","01","02");
date1.format();
date1.calculate();
date2.format();
date2.calculate();
}
}
『叄』 一道JAVA編程題
class
WangTi2
{
public
static
void
main(String[]
args)
{
long
start
=
System.currentTimeMillis();//看一下要運行多長時間
shuanShu();
long
end
=
System.currentTimeMillis();//看一下要運行多長時間
System.out.println("用時"+(end-start));
}
public
static
void
shuanShu()
{
int[]
arr
=
new
int[100];
arr[0]
=
1;
for(int
x=0;x<11213;x++)
//可以把11213改成100驗證方法的正確性
{
//2^20=1048576
int
z
=
0;
/*
這個循環是記錄乘2的結果
*/
for(int
y
=0;y<arr.length;y++)
{
arr[y]
=
arr[y]<<1;
arr[y]
=
arr[y]
+
z;
if
(arr[y]>9)
{
arr[y]
-=
10;
if(y!=arr.length-1)
z
=
1;
}
else
z
=
0;
}
}
arr[0]--;
//這個給最後一個位減1,這個值不會是負數。
System.out.println("這個數的最後100位是:");
for(int
x=arr.length-1;x>=0;x--)
{
System.out.print(arr[x]);
if(x%3==0&&x!=0)
System.out.print(",");
}
System.out.println();
}
}
思路是有的。定義數組,只存儲最後100位。然後不停的乘2,大於9的向上一個數組加1。重復11213次。再把第一個數組減1。這樣做是可以的。效率很低。求高人解答。。呵呵。
『肆』 一道有趣的Java編程題,如何輸出Hello world!
……v_v……
System.out.append("hello")
==
null
append代表在printStream加上輸出。
這回該我的答案該通過了吧?再不通過我就貼到空間上去。
『伍』 一道有趣的Java編程題,如何輸出Hello world!
可以這樣:
publicclassDemo{
publicstaticvoidmain(String...a){
if(newObject(){{System.out.print("Hello,");}}==null){
System.out.print("Hello,");
}else{
System.out.println("World");
}
}
}
輸入結果:
『陸』 有關java編程題目
按照題目要求編寫的圓,圓錐和測試類的Java程序如下
Test.java文件內容如下
class Circle{
private double r;
private String color;
public Circle(double r){
this.r=r;
}
public double area(){
return Math.PI*r*r;
}
public double perimeter(){
return Math.PI*2*r;
}
public double getR(){
return this.r;
}
public void setR(double r){
this.r=r;
}
public String getColor(){
return this.color;
}
public void setColor(String color){
this.color=color;
}
public String toString(){
return "圓的半徑為"+r+",顏色為"+color;
}
}
class Cone{
private Circle c;
private double h;
private String color;
public Cone(Circle c,double h){
this.c=c;
this.h=h;
}
public double volume(){
return 1.0/3*c.area()*h;
}
public Circle getCircle(){
return this.c;
}
public void setCircle(Circle c){
this.c=c;
}
public double getH(){
return this.h;
}
public void setH(double h){
this.h=h;
}
public String getColor(){
return this.color;
}
public void setColor(String color){
this.color=color;
}
public String toString(){
return "圓錐的底面積為"+c.area()+",高為"+h+",顏色為"+color;
}
}
public class Test{
public static void main(String[] args){
Circle circle1=new Circle(2.5);
circle1.setColor("紅色");
System.out.println(circle1.toString());
System.out.println("圓的面積為"+circle1.area());
System.out.println("圓的周長為"+circle1.perimeter());
Cone circlar1=new Cone(circle1,2.7);
circlar1.setColor("藍色");
System.out.println(circlar1.toString());
System.out.println("圓錐的體積為"+circlar1.volume());
}
}
『柒』 JAVA 4道編程題 高分!
//1、2:
public class Student{
//1:創建Student類
char id;//學號
String classes;//班級
String name;//姓名
char gender;//性別
int age;//年齡
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public char getId() {
return id;
}
public String getClasses() {
return classes;
}
public String getName() {
return name;
}
public char getGender() {
return gender;
}
//2.創建Application並實例化Student
public static void main(String[] args){
new Student();
}
}
//3:
public class Student2 {
int age;
String name;
public int getAge() {
return age;
}
public String getName() {
return name;
}
}
class Undergraate extends Student2{
String specialty;
public String getSpecialty() {
return specialty;
}
}
class Postgraate extends Student2{
String studydirection;
public String getStudydirection() {
return studydirection;
}
}
//4:
public class Book {
String name;
String author;
double monthlySale;
public Book(String name, String author, double monthlySale) {
this.name = name;
this.author = author;
this.monthlySale = monthlySale;
}
public Book(){
name="name";
author="author";
monthlySale=0;
}
//這兩個成員方法LZ沒有說清楚,我猜可能是個意思。
public void setBook(String name, String author, double monthlySale) {
this.name = name;
this.author = author;
this.monthlySale = monthlySale;
}
public void getBook(){
System.out.println("name:"+name);
System.out.println("author:"+author);
System.out.println("monthly sale:"+monthlySale);
}
}
這種題目以後盡量自己做,又不難。
『捌』 JAVA編程題,知道寫的來看看(有點難度)
class ComplexNumber{
private double m_dRealpart;
private double m_dImaginpart;
ComplexNumber(double r,double i){
m_dRealpart=r;
m_dImaginpart=i;
}
void setRealPart(double d){
m_dRealpart=d;
}
void SetImaginpart(double d){
m_dImaginpart=d;
}
ComplexNumber complexAdd(ComplexNumber c){
return new ComplexNumber(m_dRealpart+c.m_dRealpart,m_dImaginpart+c.m_dImaginpart);
}
ComplexNumber complexMinus(ComplexNumber c){
return new ComplexNumber(m_dRealpart-c.m_dRealpart,m_dImaginpart-c.m_dImaginpart);
}
ComplexNumber complexMulti(ComplexNumber c){
return new ComplexNumber(m_dRealpart*c.m_dRealpart-m_dImaginpart*c.m_dImaginpart,m_dImaginpart*c.m_dRealpart+m_dRealpart*c.m_dImaginpart);}
public String toString(){
return m_dRealpart+"+"+m_dImaginpart+"i";
}
}
第二個就不會了;
忘了個括弧,抱歉.
1
class Year{
public static void cal(int year){
if(((year%4==0) & (year%100!=0)) | (year%400==0))
System.out.println("是閏年");
else
System.out.println("不是閏年");
}
}
class TestYear{
public static void main(String[] args){
Year.cal(1932);
}
}
2
class TestString{
public static void main(String[] args){
String[] n;
String s="I am good person";
System.out.println("該字元串中字元的個數:"+s.length());
n=s.split(" ",4);
System.out.println("該字元串中單詞的個數:"+n.length);
}
}
『玖』 5道簡單的JAVA編程題(高分懸賞)
很詳細的幫你寫下,呵呵,所以要給分哦!
1、
(1)源程序如下:
public class One {
public static void main(String[] args) {
String name = "張三";
int age = 23;
char sex = '男';
String myclass = "某某專業2班";
System.out.println("姓名:" + name);
System.out.println("姓名:" + age);
System.out.println("姓名:" + sex);
System.out.println("姓名:" + myclass);
}
}
(2)
編寫完程序的後綴名是.java,如本題,文件名就是One.java。
開始\運行\cmd,進入「命令提示符窗口」,然後用javac編譯器編譯.java文件,語句:javac One.java。
(3)
編譯成功後,生成的文件名後綴是.class,叫做位元組碼文件。再用java解釋器來運行改程序,語句:java One
2、編寫程序,輸出1到100間的所有偶數
(1)for語句
public class Two1 {
public static void main(String[] args) {
for(int i=2;i<=100;i+=2)
System.out.println(i);
}
}
(2)while語句
public class Two2 {
public static void main(String[] args) {
int i = 2;
while (i <= 100) {
System.out.println(i);
i += 2;
}
}
}
(3)do…while語句
public class Two3 {
public static void main(String[] args) {
int i = 2;
do {
System.out.println(i);
i += 2;
}while(i<=100);
}
}
3、編寫程序,從10個數當中找出最大值。
(1)for循環
import java.util.*;
public class Three1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int number;
int max = 0;
for (int i = 0; i < 10; i++) {
System.out.print("輸入第" + (i + 1) + "個數:");
number = input.nextInt();
if (max < number)
max = number;
}
System.out.println("最大值:" + max);
}
}
(2)while語句
import java.util.*;
public class Three2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int number;
int max = 0;
int i = 0;
while (i < 10) {
System.out.print("輸入第" + (i + 1) + "個數:");
number = input.nextInt();
if (max < number)
max = number;
i++;
}
System.out.println("最大值:" + max);
}
}
(3)do…while語句
import java.util.*;
public class Three3 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int number;
int max = 0;
int i = 0;
do {
System.out.print("輸入第" + (i + 1) + "個數:");
number = input.nextInt();
if (max < number)
max = number;
i++;
}while(i<10);
System.out.println("最大值:" + max);
}
}
4、編寫程序,計算從1到100之間的奇數之和。
(1)for循環
public class Four1 {
public static void main(String[] args) {
int sum=0;
for(int i = 1;i<=100;i+=2){
sum+=i;
}
System.out.println("1~100間奇數和:" + sum);
}
}
(2)while語句
public class Four2 {
public static void main(String[] args) {
int sum = 0;
int i = 1;
while (i <= 100) {
sum += i;
i += 2;
}
System.out.println("1~100間奇數和:" + sum);
}
}
(3)do…while語句
public class Four3 {
public static void main(String[] args) {
int sum = 0;
int i = 1;
do {
sum += i;
i += 2;
} while (i <= 100);
System.out.println("1~100間奇數和:" + sum);
}
}
5、
(1)什麼是類的繼承?什麼是父類?什麼是子類?舉例說明。
繼承:是面向對象軟體技術當中的一個概念。如果一個類A繼承自另一個類B,就把這個A稱為"B的子類",而把B稱為"A的父類"。繼承可以使得子類具有父類的各種屬性和方法,而不需要再次編寫相同的代碼。在令子類繼承父類的同時,可以重新定義某些屬性,並重寫某些方法,即覆蓋父類的原有屬性和方法,使其獲得與父類不同的功能。另外,為子類追加新的屬性和方法也是常見的做法。繼承需要關鍵字extends。舉例:
class A{}
class B extends A{}
//成員我就不寫了,本例中,A是父類,B是子類。
(2)編寫一個繼承的程序。
class Person {
public String name;
public int age;
public char sex;
public Person(String n, int a, char s) {
name = n;
age = a;
sex = s;
}
public void output1() {
System.out.println("姓名:" + name + "\n年齡:" + age + "\n性別:" + sex);
}
}
class StudentPerson extends Person {
String school, department, subject, myclass;
public StudentPerson(String sc, String d, String su, String m, String n,
int a, char s) {
super(n, a, s);
school = sc;
department = d;
subject = su;
myclass = m;
}
public void output2() {
super.output1();
System.out.println("學校:" + school + "\n系別:" + department + "\n專業:"
+ subject + "\n班級:" + myclass);
}
}
public class Five2 {
public static void main(String[] args) {
StudentPerson StudentPersonDemo = new StudentPerson("某某大學", "某某系別",
" 某專業", "某某班級", " 張三", 23, '男');
StudentPersonDemo.output2();
}
}