【Chapter two】

1. Question 1: Input two numbers, only recognize two-digit addition and two-digit output. Question 2: Input two numbers, >= 9 digits, how many digits are there when adding two digits

package acm;
//1、输入两个数字,只识别两位数相加和两位数输出
import java.util.Scanner;

public class 第二章19视频1 {
    
    
	
	
//?1、答:	public static void main(String[] args)
//	{
    
    
//		int a=0,b=0;
//	Scanner sc=new Scanner(System.in);
//	while(sc.hasNext()) {
    
    
//		a=sc.nextInt();
//		b=sc.nextInt();
//		System.out.println(((a%100)+(b%100))%100);	
//	}
//?	}
	
	
	
	//输入两个数字,>=9位,两位数相加有多少进位
//每次模拟加法,个位对齐,百位对百位,十位对十位,
	public static void main(String[] args)
	{
    
    
		int a=0,b=0;
		Scanner sc=new Scanner(System.in);
		while(sc.hasNext()) {
    
    
			a=sc.nextInt();
			b=sc.nextInt();
			if(a==0 && b==0) break;
			int count=0,jw=0;//count记录共有多少次进位,jw用来存放上一次是否需要进位
			for(int i=0;i<=9;i++)
			{
    
    
				jw=(a%10+b%10+jw)>=10?1:0;
				count+=jw;
				a=a/10;
				b=b/10;
			}
			System.out.println(count);
			
		}
	}
	
}


2. Greatest common divisor

//The divisor is that if an integer is divisible by two integers, then the two numbers are divisors of the number. Divisors are limited, and the greatest common divisor is generally used. To put it bluntly,
//A divisor is a divisor that can divide it evenly. If the quotient obtained by dividing an integer a by an integer b (b≠0) is exactly an integer without a remainder, we say that a is divisible by b, or b is divisible by a.
//A prime number refers to a natural number that has no other factors except 1 and itself among the natural numbers greater than 1. 1 3 5 7
//Definition of factors. In elementary school mathematics, when two positive integers are multiplied, then these two numbers are called the factors of the product, or divisors.

package acm;

import java.util.Scanner;

//约数是如果一个整数能被两个整数整除,那么这两个数就是这个数的约数。 约数是有限的,一般用最大公约数。 直白地说,
//约数就是能将其整除的除数。 整数a除以整数b (b≠0) 除得的商正好是整数而没有余数,我们就说a能被b整除,或b能整除a。
//质数是指在大于1的自然数中,除了1和它本身以外不再有其他因数的自然数。1 3 5 7
//因数定义. 在 小学数学里,两个正整数相乘,那么这两个数都叫做 积 的因数,或称为 约数 。
public class 第二章20最大公约数 {
    
    
	public static void main(String[] args)
	{
    
    
		int a=0,b=0;
		Scanner sc=new Scanner(System.in);
		while(sc.hasNext())
		{
    
    
			a=sc.nextInt();
			b=sc.nextInt();
			System.out.println("欧几里得"+gcd1(a,b));
			System.out.println("递归"+gcd(a,b));
		}
		
	}
	//求a,b的最大公约数,对比a,b,大数取余小数===大方块里取一个小方快,再从小方块取小小方块
	public static int gcd(int a,int b) {
    
    
		return b==0?a:gcd(b,a%b); //如果b==0,最大公约数为a
	}
	 
	public static int gcd1(int a,int b)
	{
    
    
		while(b>0) {
    
    
			int t=a%b;
			a=b;
			b=t; //gcd(a,b)=>gcd(b.a%b)
		}
		return a;
	}

}

3. Addition of large integers

//Conventional high-precision + - * division
//Addition simulation of large integers
//Input a, b reverse a, b so that ones digits are ones digits, tens digits are ten digits
//for (beginning and ending) and then from left to right Add to the right, every ten enters one
//output and print backwards

package acm;

import java.util.Scanner;

//常规高精度+ - * 除
//大整数的加法模拟
//输入a,b 反转a,b  使得个位对个位 、十位对十位 
//for(开头    结尾)      然后从左往右加,逢十进一
//输出倒着打印
public class 第二章大整数加法 {
    
    

	public static void main(String[] args) {
    
    
		Scanner sc=new Scanner(System.in);
		while(sc.hasNext())//从键盘输入
		{
    
    
			String a=sc.next();
			String b=sc.next();
			int len=Math.max(a.length(), b.length());
			String ans="";//存放模拟相加的结果
			String ans1="";
			
			//ab两个加数翻转,这样个位在左,能对齐
			a=new StringBuffer(a).reverse().toString();
			//把a 传入StringBuffer()构造函数里,得到StringBuffer()的对象
			b=new StringBuffer(b).reverse().toString();
			
			//开始模拟进位 此时个位对齐
			int jw=0;//开始的时候进位是0(从低位到高位的进位)
			for(int i=0;i<=len;i++)
			{
    
    
				int ai=i<a.length()?a.charAt(i)-'0':0;//? : 当a,b的长度不同时,需要打这个补丁
				int bi=i<b.length()?b.charAt(i)-'0':0;
				int num=ai+bi+jw;//结果 两数相加,再加上前面的进位
				//取个位 当前这一位应该留下的 合并字符串
				//ans=ans+num%10;
				ans1=num%10+ans1;
				jw=num/10;//锋十进一 应该往上面进位的
				
				//System.out.println(ans);
				
				//System.out.println(ans1);
			}
			//if(jw==1)ans="1"+ans;
			System.out.println(ans1);
			
		}
		

	}

}

4. Hexadecimal

//Enter a number a (decimal by default), add the decimal, hexadecimal, and binary numbers of a to be equal, then a is a sky number
//for example, the sum of the four digits of 2992 is 22;
//2992 The sum of the four digits of 1894 in twelve binary is 22;
//The sum of BB0 in hexadecimal of 2992 is 11+11+0=22
//So 2992 is the sky number

package acm;

import java.util.Scanner;
//输入一个数a(默认为十进制),将a的十进制、十六进制、十二进制  数字相加都相等,则a为天空数
//例如2992 四位数之和22;
//2992的十二进制为1894 四位数之和22;
//2992的十六进制为BB0   数子之和为11+11+0=22 
//所以2992是天空数
public class 第二章进制 {
    
    
	public static void main(String[] args)
	{
    
    
		int a=0;//定义两个整形变量用来存储 到时候读入整数a b
		Scanner sc=new Scanner(System.in);
		while(sc.hasNext())
		{
    
    
			a=sc.nextInt();
			if(getRsum(a,10)==getRsum(a,12)&&  getRsum(a,12)==getRsum(a,12))
				System.out.println("是天空数");
			else
				System.out.println("no");
			
		}
		
	}
	public static int getRsum(int n,int r)
	{
    
    
		int sum=0;
		while(n>0)
		{
    
    
			sum+=n%r;
			n=n/r;
		}
		return sum;
	}

}

5. Print all subsets of a set in binary

package acm;
//打印一个集合的全部子集
public class 第二章进制1 {
    
    
	public static void main(String[] args)
	{
    
    
		int[] a= {
    
    1,2,3,4};
		for(int i=0;i<15;i++)
		{
    
    
			System.out.print("{");
			int n=i;//存储当前要去转换成二进制的i
			int index=0;//当前是第几次去除以2
			while(n>0)//只要n>0,就不断的除2取余
			{
    
    
				if(n%2==1)// 15%2=1 集合的第一个数字打印
					      // 15/2=7 7%2=1 集合的第二个数字打印
					      // 7/2=3 3%2=1  集合的第三个数字打印
					      // 3/2=1 1%2=1 集合的第四个数字打印
				{
    
    
					if(n>2)
						System.out.print(a[index]+" ");
					else
						System.out.print(a[index]);
				}
				index++;
				n=n/2;//不断缩小为原来的二分之一
			}
			System.out.println("}");
		}
	}

}

6. Bit operations

//Enter a number n, and judge whether n=2 to the power of x is—yes or not—no
//N is converted to binary, and all 0s after the beginning of 1


	public static void main(String[] args)
	{
    
    
		int a=0;
		Scanner sc=new Scanner(System.in);
		while(sc.hasNext())
		{
    
    
			a=sc.nextInt();
			System.out.println((a&(a-1))==0?"yes":"no");
		}
	}

//Enter a number a, and judge how many 1s there are in the binary of a


	public static void main(String[] args)
	{
    
    
		int a=0;
		Scanner sc=new Scanner(System.in);
		while(sc.hasNext())//循环读入若干组测试数据
		{
    
    
			a=sc.nextInt();
			int count=0;//统计a的二进制中1的个数
			while(a>0)
			{
    
    
				a=a&(a-1);//做一次,就把a的最右边的1置为0
				//在计算器内数字为二进制
				count++;
			}
			System.out.println(count);
		}
	}


//Enter an integer from n~2n-1 to find out the number that appears only once
//X^x=0 0^x=xa b c=a c b
//Case:
// 4
// 1 2 3 2 1 4 4
//
// 0 1 1 2 2 4 4 3==0 3==3


	public static void main(String[] args)
	{
    
    
		int n=0,ans=0;//存放读入几个数
		Scanner sc=new Scanner(System.in);
		while(sc.hasNext())//循环读入若干组测试数据
		{
    
    
			n=sc.nextInt();//知道要读入多少个数
			ans=0;
			for(int i=0;i<2*n-1;i++)
			{
    
    
				ans=ans^sc.nextInt();
				
			}
			System.out.println(ans);
		}
	}

//Enter two numbers a, ba=17 b=25
//Flip two numbers a=71 b=52
//a0=7 a1=1 b0=5 b1=2
//a[0]*b[0 ]=35
//a[0]*b[1]=14 C[0]=35 C[1]=14+5 C[2]=2 //
a[1]*b[0]=5 35 19 2
//a[1]*b[1]=2 Decimal to the right forward and take the remainder 5 2 4 Finally load flip 425

for(a[0]~~a[n])
	for(b[0]~~b[n])
		a[i]*b[j]====c[i+j]		
for(c[0]~~c[n])
package acm;

import java.util.Arrays;
import java.util.Scanner;


public class 第二章位运算 {
    
    

	public static void main(String[] args)
	{
    
    
		Scanner sc=new Scanner(System.in);
		while(sc.hasNext())
		{
    
    
			String a=sc.next();
			String b=sc.next();
			int[] c=new int[a.length()+b.length()];
			//ab两个数翻转,个位在左可以对齐
			a=new StringBuffer(a).reverse().toString();
			b=new StringBuffer(b).reverse().toString();
			for(int i=0;i<a.length();i++)
			{
    
    
				int ai=a.charAt(i)-'0';
				for(int j=0;j<b.length();j++)
				{
    
    
					int bj=b.charAt(j)-'0';
					c[i+j]=c[i+j]+ai*bj;
					
				}
			}
			System.out.println(Arrays.toString(c));
			
			for(int i=0;i<c.length-1;i++)
			{
    
    
				int jw=c[i]/10;//算出进位
				c[i]=c[i]%10;//取个位数
				c[i+1]+=jw;//后一位进位后的数
			}
			System.out.println(Arrays.toString(c));
			int pos=c.length;
			while(c[--pos]==0);//去除数组最后的0
			
			while(pos>=0)
				System.out.print(c[pos--]);
		}
	}

}

7. How many days

package acm;

import java.util.Calendar;

public class 第几天 {
    
    
public static void main(String[] args){
    
    
	Calendar calendar1=Calendar.getInstance();
	calendar1.set(2000, 0,1);
	Calendar calendar2=Calendar.getInstance();
	calendar2.set(2000,4,4);
	//xxxx年 calendar2-开始的那一年 经历的毫秒数
	//System.out.println((calendar2.getTimeInMillis()-calendar1.getTimeInMillis())/1000/60/60/24);
	System.out.println(calendar2.get(Calendar.DAY_OF_YEAR));
	
}

}

Guess you like

Origin blog.csdn.net/m0_53524766/article/details/131382334