java--實驗二語法基礎練習(2)AX

Gverzh發表於2020-10-14

判斷字串是否相等:
if (a . equals (b)) 如果相等返回值為true

String a="aaa";
String b="bbb";
String c="bbb";
if(a.equals(b)) System.out.print(1);
else System.out.print(2);

閏年:

if((n%4==0&&n%100!=0)||n%400==0)

判斷識別符號是否合法

String x=sc.nextLine();
if(Character.isJavaIdentifierPart(x.charAt(i)))

Java valueOf() 方法 //將字串形式的數字變成int型別的數字 也可以換成不同的進位制數
函式呼叫!//主類中自定義函式、自定義類中呼叫
JAVA中陣列作為函式引數傳遞

Y - 猜糖塊

import java.util.Scanner;
public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		int s=0;
		int i=1;
		while(s<n)
		{
			i++;
			s=s+i*i;
		}
		System.out.print(i-1);
	}
}

Z - 完美的素數

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		while (sc.hasNext()) {
			int n = sc.nextInt();
			int x = n;
			int s = 0;
			while (x > 0)
			 {
				s = s + x % 10;
				x = x / 10;
			}
			int f = 0;
			for (int i = 2; i < n; i++)
			{
				if (n % i == 0)
				{
					f = 1;
					break;
				}
				if (s > i && s % i == 0) 
				{
					f = 1;
					break;
				}
			}
			if (f == 1)
				System.out.printf("NO\n");
			else
				System.out.printf("YES\n");
		}
	}
}

AA - 九九乘法表

實驗一有做過,直接粘過來了。。。

import java.util.Scanner;
public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc=new Scanner(System.in);
		
		while(sc.hasNext())
		{
			int n=sc.nextInt();
			for(int i=1;i<=n;i++)
			{
				for(int j=1;j<=i;j++)
				{
					if(j==i) System.out.printf("%d*%d=%d",j,i,i*j);
					else System.out.printf("%d*%d=%d ",j,i,i*j);
				}
				System.out.println();
			}
		}
	}
}

AB - C語言實驗——求絕對值(選擇結構)

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int n=sc.nextInt();
		int x=Math.abs(n);
		System.out.print(x);
	}
}

AC - 小鑫の日常系列故事(二)——石頭剪子布

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		String a="Rock";
		String b="Scissors";
		String c="Cloth";
		String x=sc.nextLine();
		String y=sc.nextLine();
		if((x.equals(a)&&y.equals(a))||(x.equals(b)&&y.equals(b))||(x.equals(c)&&y.equals(c))) System.out.println("Equal");
		else if((x.equals(a)&&y.equals(b))||(x.equals(b)&&y.equals(c))||(x.equals(c)&&y.equals(a))) System.out.println("Win");
		else System.out.println("Lose");
	}
}

AE - JAVA判斷合法識別符號//Character.isJavaIdentifierPart(x.charAt(i))

import java.util.Scanner;
import java.util.Arrays;
import java.math.*;
public class Main {
	
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		while(sc.hasNext())
		{
			int f=0;
			String x=sc.nextLine();
			if(!Character.isJavaIdentifierStart(x.charAt(0))) f=1;
			for(int i=0;i<x.length();i++)
			{
				if(!Character.isJavaIdentifierPart(x.charAt(i))) f=1;
			}
			if(f==1) System.out.println("false");
			else System.out.println("true");
		}
	}	
}

AF - C語言實驗——輸入數字星期,輸出英文(switch語句)

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		
		int n=sc.nextInt();
		if(n==1) System.out.println("Monday");
		else if(n==2)  System.out.println("Tuesday");
		else if(n==3)  System.out.println("Wednesday");
		else if(n==4)  System.out.println("Thursday");
		else if(n==5)  System.out.println("Friday");
		else if(n==6)  System.out.println("Saturday");
		else if(n==7)  System.out.println("Sunday");
	}
}

AD - 閏年

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int n=sc.nextInt();
		if((n%4==0&&n%100!=0)||n%400==0)System.out.print("Yes\n");
		else System.out.print("No\n");		
	}
}

AG - C語言實驗——某年某月的天數 // valueOf()

import java.util.Scanner;
import java.util.Arrays;
import java.math.*;
public class Main {
	
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		String x[]=sc.next().split("\\\\");
		int day[]= {0,31,28,31,30,31,30,31,31,30,31,30,31};
		int nian=Integer.valueOf(x[0]);
		int yue=Integer.valueOf(x[1]);
		if(((nian%4==0&&nian%100!=0)||nian%400==0)&&yue==2)
		 System.out.printf("%d\n",day[yue]+1);
		else System.out.printf("%d\n",day[yue]);
	}
}

AH - 水仙花數

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		while(sc.hasNext())
		{
			int st[]=new int[999];
			int l,r; 
			l=sc.nextInt();
			r=sc.nextInt();
			int k=0;
			for(int i=l;i<=r;i++)
			{
				int n=i;
				int s=0;
				while(n>0)
				{
					int x=n%10;
					s=s+x*x*x;
					n=n/10;
				}
				if(s==i) st[k++]=i;	
			}
			if(k==0) System.out.printf("no\n");
			else 
			{
				for(int j=0;j<k;j++)
				{
					if(j==k-1) System.out.printf("%d\n",st[j]);
					else System.out.printf("%d ",st[j]);
				}
			}
		}
	}
}

AI - 小鑫數數兒

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		while(sc.hasNext())
		{
			int n=sc.nextInt();
			int x[]=new int [n];
			int s=0;
			int a=0,b=0,c=0;
			for(int i=0;i<n;i++)
			{
				x[i]=sc.nextInt();
				s=s+x[i];
			}
			s=s/n;
			for(int i=0;i<n;i++)
			{
				if(x[i]<s) a++;
				else if (x[i]==s) b++;
				else if(x[i]>s) c++;
			}
			System.out.printf("%d %d %d\n",a,b,c);
		}
	}
}

AJ - C語言實驗——各位數字之和排序


import java.util.Scanner;
import java.util.Arrays;
public class Main {
	
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		while(!sc.hasNext("0"))
		{
			int n=sc.nextInt();
			if(n!=0)
			{
				int x[]=new int[n+1];
				int y[]=new int[n+1];
				for(int i=0;i<n;i++)
				{
					x[i]=sc.nextInt();
					int xx=x[i];
					int s=0;
					while(xx>0)
					{
						s=s+xx%10;
						xx=xx/10;	
					}
					y[i]=s;			
				}
				f1(x,y,n);
				for(int i=0;i<n;i++)
				{
					if(i==n-1) System.out.printf("%d\n",x[i]);
					else System.out.printf("%d ",x[i]);
				}
			}
		
		}
	}
		
	static void f1(int x[],int y[],int n)
	{
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<n-1-i;j++)
			{
				if(y[j]>y[j+1])
				{
					int t=y[j];
					y[j]=y[j+1];
					y[j+1]=t;
					t=x[j];
					x[j]=x[j+1];
					x[j+1]=t;
				}
			}
		}
	}
}

AK - 小鑫の日常系列故事(六)——奇遇記

import java.util.Scanner;
import java.util.Arrays;
public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		while(sc.hasNext())
		{
			int n=sc.nextInt();
			int x[]=new int [45];
			int s[]=new int [45];
			x[0]=0;
			x[1]=1;
			x[2]=2;
			s[0]=0;
			s[1]=1;
			s[2]=3;
			for(int i=3;i<=n;i++)
			{
				x[i]=x[i-1]+x[i-2];
				s[i]=s[i-1]+x[i];
			}
			if(n==0) System.out.println(0);
			else System.out.printf("%d\n",s[n]+1);
		}
	}
}

AL - C語言實驗——保留整數

import java.util.Scanner;
import java.util.Arrays;
public class Main {
	
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		String a=sc.nextLine();
		int n=a.length();
		char b[]=new char[100];
		for(int i=0;i<n;i++)
		{
			b[i]=a.charAt(i);
		}
		int f=1;
		for(int i=0;i<n;i++)
		{
			if(b[i]>='0'&&b[i]<='9')
			{
				System.out.print(b[i]);
				f=1;
			}
			else if(f==1) 
			{
				System.out.print("*");
				f=0;
			}
		}
	}
}

AM - 大小寫轉換

import java.util.Scanner;
import java.util.Arrays;
public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		while(sc.hasNext())
		{
			String x=sc.nextLine();
			int n=x.length();
			for(int i=0;i<n;i++)
			{
				char a=x.charAt(i);
				if(a>='a'&&a<='z')
				{
					System.out.printf("%c",a-32);
				}
				else if (a>='A'&&a<='Z')
				{
					System.out.printf("%c",a+32);
				}
				else System.out.print(a);
			}
			System.out.println();
		}
	}
}

AO - C語言實驗——餘弦

import java.util.Scanner;
import java.util.Arrays;
import java.math.*;
public class Main {
	
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		while(sc.hasNext())
		{
			double a=1;
			double s=0;
			double x=sc.nextDouble();
			int n=sc.nextInt();
			for(int i=0;i<=n;i++)
			{
				a=1;
				for(int j=1;j<=2*i;j++)
					a=a*j;
				
				s+=Math.pow(-1, i)*Math.pow(x, 2*i)/a;
			}
			
			System.out.printf("%.4f\n",s);
		}
	}
		
}

AR - 計算題

import java.util.Scanner;
import java.util.Arrays;
import java.math.*;
public class Main {
	
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		int t=sc.nextInt();
		while(t>0)
		{
			t--;
			int m=sc.nextInt();
			int n=sc.nextInt();
			System.out.printf("%d\n",f1(m,n));
			
		}
	}
	static int f1(int m,int n)
	{
		int y=0;
		if(m==1) y=n;
		if(n==1) y=m;
		if(m>1&&n>1) y=f1(m-1,n)+f1(m,n-1);
		return y;
	}
}

AP - 小金問呀問不會問題

import java.util.Scanner;
import java.util.Arrays;
public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		while(sc.hasNext())
		{
			int n=sc.nextInt();
			if(n>=0) System.out.printf("Yes\n");
			else System.out.printf("No\n");
		}
	}
}

AT - 漢諾塔

import java.util.Scanner;
import java.util.Arrays;
import java.math.*;
public class Main {
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n=sc.nextInt();
		move(n,'A','B','C');
	}	
	static void move(int n,char a,char b,char c)
	{
		if(n>1)
		{
			move(n-1,a,c,b);
			p(n,a,c);
			move(n-1,b,a,c);
		}
		else p(n,a,c);
		
	}
	static void p(int n,char x,char y)
	{
		System.out.printf("Move disk %d from %c to %c\n",n,x,y);
	}	
}

AW - 計算組合數

import java.util.Scanner;
import java.util.Arrays;
public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int t=sc.nextInt();
		while(t>0)
		{
			t--;
			int n=sc.nextInt();
			int m=sc.nextInt();
			System.out.println(hanshu(n,m));
		}
	}
	static int hanshu(int n,int m)
	{
		int f;
		if(m==0)  f=1;
		else if(n==1) f=1;
		else if(n==m) f=1;
		else 
		{
			f=hanshu(n-1,m-1)+hanshu(n-1,m);
		}
		return f;
	}
}

AN - 編碼

這個題交的時候把package也給粘上了
因為這個錯了還幾次。。。

import java.util.Scanner;
import java.util.Arrays;
public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		while(sc.hasNext())
		{
			String x=sc.nextLine();
			int n=x.length();
			int k=0;
			for(int i=1;i<n;i++)
			{
				char a=x.charAt(i-1);
				char b=x.charAt(i);
				if(a==b) k++;
				else
				{					
					if(k>=1) System.out.printf("%d%c",k+1,a);
					else System.out.printf("%c",a);
					k=0;
				}
			}
			char b=x.charAt(n-1);
			if(k==0) System.out.printf("%c",b);
			else System.out.printf("%d%c",k+1,b);
			System.out.println();	
		}
	}
}
import java.util.Scanner;
import java.util.Arrays;
public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		while(sc.hasNext())
		{
			String x=sc.nextLine();
			char a[]=new char [10010];
			for(int i=0;i<x.length();i++)
			{
				a[i]=x.charAt(i);
			}
			int k=1;
			for(int i=0;i<x.length();i++)
			{
				if(a[i]==a[i+1]) k++;
				else 
				{
					if(k==1) System.out.printf("%c",a[i]);
					else System.out.printf("%d%c",k,a[i]);
					k=1;
				}
			}
			System.out.println();	
		}
	}
}

相關文章