3.9日代码

今天被老师批了一顿,以后会努力的!I promise!
Java代码主要是2013年省赛题


package lq_2013;

import java.util.Calendar;

/** 

* @author 作者 党运楷: 

* @version 创建时间:2020年3月9日 下午12:16:43 
* 
* 曾有邪教称1999年12月31日是世界末日。当然该谣言已经不攻自破。还有人称今后的某个世纪末的12月31日,如果是星期一则会....

有趣的是,任何一个世纪末的年份的12月31日都不可能是星期一!! 于是,“谣言制造商”又修改为星期日......

1999年的12月31日是星期五,请问:未来哪一个离我们最近的一个世纪末年(即xx99年)的12月31日正好是星期天(即星期日)?

请回答该年份(只写这个4位整数,不要写12月31等多余信息)

* calendar类,熟悉set get方法

*/

public class Main1 {

	public static void main(String[] args) {
		Calendar calendar= Calendar.getInstance();
		for (int year = 1999; year < 10000; year+=100) {
			calendar.set(Calendar.YEAR, year);
			calendar.set(Calendar.MONTH, 11);//12月
			calendar.set(Calendar.DAY_OF_MONTH, 31);
			if(calendar.get(Calendar.DAY_OF_WEEK)==1){
				System.out.println(year);
				break;
			}
		}

	}

}


package lq_2013;

/** 

* @author 作者 党运楷: 

* @version 创建时间:2020年3月9日 下午12:22:05 

* 第二题 马虎的算式

小明是个急性子上小学的时候经常把老师写在黑板上的题目抄错了。 有一次老师出的题目是36 x 495 = ? 他却给抄成了396 x 45 = ? 

但结果却很戏剧性他的答案竟然是对的  因为 36 * 495 = 396 * 45 = 17820   类似这样的巧合情况可能还有很多比如27 * 594 = 297 * 54 

假设 a b c d e 代表1~9不同的5个数字注意是各不相同的数字且不含0   能满足形如ab * cde = adb * ce 这样的算式一共有多少种呢

一看就知道应该是用暴力法,5层for循环再加一个check就行:

*/

public class Main2 {
	//注意最后要验证一下可能会出错
	public static void main(String[] args) {
		int ans=0;
		for (int a = 1; a <10; a++) {
			for (int b = 1; b < 10; b++) {
				if(a!=b)for (int c = 1; c <10; c++) {
					if(c!=b&&c!=b)for (int d = 1; d <10; d++) {
						if(d!=a&&d!=b&&d!=c)for (int e = 1; e <10; e++) {
							if(e!=a&&e!=b&&e!=c&&e!=d)
							if((a*10+b)*(c*100+d*10+e)-(a*100+d*10+b)*(c*10+e)==0){
								ans++;
							}
						}
					}
				}
			}
		}
		System.out.println(ans);

	}

}
package lq_2013;
/** 
* @author 作者 党运楷: 
* @version 创建时间:2020年3月9日 下午12:28:34 
* 小明参加了学校的趣味运动会,其中的一个项目是:跳格子 地上画着一些格子,每个格子里写一个字,如下所示:(也可参见p1.jpg)
 从我做起振
 我做起振兴
 做起振兴中
 起振兴中华
比赛时,先站在左上角的写着“从”字的格子里,可以横向或纵向跳到相邻的格子里,但不能跳到对角的格子或其它位置。一直要跳到“华”字结束。
要求跳过的路线刚好构成“从我做起振兴中华”这句话。
请你帮助小明算一算他一共有多少种可能的跳跃路线呢?
行号0-3 列号0-4
*/
//递归  走楼梯 斐波那契
public class Main3 {
	public static void main(String[] args) {
		//步骤:重复,变化,边界
		int ans=f(0,0);
		System.out.println(ans);
	}
	private static int f(int i, int j) {
		if(i==3||j==4)return 1;
		return f(i+1, j)+f(i, j+1);
	}
}


package lq_2013;

import java.math.BigDecimal;
import java.math.BigInteger;

/** 

* @author 作者 党运楷: 

* @version 创建时间:2020年3月9日 下午3:16:35 

* 类说明 

*/

public class Main4 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		BigInteger a= BigInteger.ONE;
		BigInteger b=BigInteger.ONE;
		for (int i = 3; i < 500; i++) {
			BigInteger t=b;
			b=a.add(b);
			a=t;
		}
		//bigdecimal第二个参数是长度 scale
		BigDecimal divdide=new BigDecimal(a,110).divide(new BigDecimal(b,110),BigDecimal.ROUND_HALF_DOWN);
		System.out.println(divdide.toPlainString().substring(0,103));
	}
	

}

package lq_2013;

/** 

* @author 作者 党运楷: 

* @version 创建时间:2020年3月9日 下午3:26:52 

* 类说明 

*/
//有理数类

//有理数就是可以表示为两个整数的比值的数字。一般情况下,我们用近似的小数表示。但有些时候,
//不允许出现误差,必须用两个整数来表示一个有理数。
//这时,我们可以建立一个“有理数类”,下面的代码初步实现了这个目标。为了简明,它只提供了
//加法和乘法运算。

//使用该类的示例:
//Rational a = new Rational(1,3);
//Rational b = new Rational(1,6);
//Rational c = a.add(b);
//System.out.println(a + "+" + b + "=" + c);

public class Main5 {
 static class Rational {
     private long ra;
     private long rb;

     private long gcd(long a, long b) {
         if (b == 0)
             return a;
         return gcd(b, a % b);
     }

     public Rational(long a, long b) {
         ra = a;
         rb = b;
         long k = gcd(ra, rb);
         if (k > 1) { // 需要约分
             ra /= k;
             rb /= k;
         }
     }

     // 加法
     public Rational add(Rational x) {
         // return ________________________________________;  //填空位置
         return new Rational(ra * x.rb + rb * x.ra, rb * x.rb);
     }

     // 乘法
     public Rational mul(Rational x) {
         return new Rational(ra * x.ra, rb * x.rb);
     }

     public String toString() {
         if (rb == 1)
             return "" + ra;
         return ra + "/" + rb;
     }
 }

 public static void main(String[] args) {
     Rational a = new Rational(1, 3);
     Rational b = new Rational(1, 6);
     Rational c = a.add(b);
     System.out.println(a + "+" + b + "=" + c);

     Rational a1 = new Rational(1, 3);
     Rational b1 = new Rational(1, 3);
     Rational c1 = a.add(b1);
     System.out.println(a1 + "+" + b1 + "=" + c1);
 }

}

package lq_2013;

/** 

* @author 作者 党运楷: 

* @version 创建时间:2020年3月9日 下午3:33:36 

* 类说明 

*/

//三部排序
//对一个整型数组中的数字进行分类排序:使得负数都靠左端,正数都靠右端,0在中部。
//注意问题的特点是:
//负数区域和正数区域内并不要求有序。可以利用这个特点通过1次线性扫描就结束战斗!!

public class Main6 {
 static void sort(int[] x)
 {
     int p = 0;
     int left = 0;
     int right = x.length-1;
     
     while(p<=right){
         if(x[p]<0){
             int t = x[left];
             x[left] = x[p];
             x[p] = t;
             left++;
             p++;
         }
         else if(x[p]>0){
             int t = x[right];
             x[right] = x[p];
             x[p] = t;
             right--;            
         }
         else{
             p++;  //代码填空位置
         }
     }
 }
}


package lq_2013;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Scanner;

import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils.Collections;

/** 

* @author 作者 党运楷: 

* @version 创建时间:2020年3月9日 下午3:39:35 

* 类说明 

*/

public class Main7 {

	public static void main(String[] args) {
		// 这是一个输入模板。记住!!!
		Scanner scanner= new Scanner(System.in);
		ArrayList<Integer> arrayList = new ArrayList<Integer>();
		int N= scanner.nextInt();
		scanner.nextLine();//输入整数的时候要去掉数字后面的换行符
		for (int i = 0; i < N; i++) {
			String line= scanner.nextLine();
//			System.out.println(line);
			String[] split= line.split(" ");
			for (int j = 0; j < split.length; j++) {
				arrayList.add(Integer.parseInt(split[j]));
			}
		}
		java.util.Collections.sort(arrayList);
		int a=0,b=0;//注意这里局部变量初始化
		for (int i = 1; i < arrayList.size(); i++) {
			//注意这里取的是对象,对象和对象比较用equals
			int cur=(int)(arrayList.get(i));
			int pre=(int)(arrayList.get(i-1));
			if(cur-pre==2){
				a=(int)(arrayList.get(i))-1;
			}
			if(cur-pre==0){
				b=(int)(arrayList.get(i));
			}
		}
		System.out.println(a+" "+b);
	}

}

在写个蓝桥杯练习系统上边的题:这周宿舍几个要刷到25题以上,今天先划个水,写个辗转相除的:


package UsedYear_55;

import java.util.Scanner;

/** 

* @author 作者 党运楷: 

* @version 创建时间:2020年3月10日 上午12:19:17 

* 最小公倍,主要是辗转相除法

*/

public class year_01 {

	public static void main(String[] args) {
		// 辗转相除法求最大公约数
//		int a=21,b=9,t;
//		System.out.println(a%b);
//		while(a%b!=0){
//			t=a%b;
//			a=b;
//			b=t;
//			System.out.println(a+" "+b+" "+t);
//		}
//		System.out.println(a+" "+b);
		//参考网址:明天再看,先整个暴力写上...https://blog.csdn.net/jopus/article/details/18971035
		Scanner scanner= new Scanner(System.in);
		int a=scanner.nextInt();
		int b=scanner.nextInt();
		int c=scanner.nextInt();
		int i=1;
		while(i<=a*b*c){
			if(i%a==0&&i%b==0&&i%c==0){
				System.out.println(i);
				break;
			}
			else {
				i++;
			}
		}
		
	}

}

下边这个是北大百炼OJ的题目:
2019年推免复试A题,今天时间不够了,明天继续写

#include <iostream>
#include<stdio.h>
#include<math.h>
/* 3.9日23:55 北大百炼OJ 2019年推免复试A题 简单枚举+数组排序  c语言的解法*/
using namespace std;
int main_A(int argc, char** argv) {
	int n,a[3005],b[3005];
	int i,j;
	scanf("%d",&n);
	for(i=0;i<n;i++){
		scanf("%d",&a[i]);
	}
	//注意这里是n-1
	for(i=0;i<n-1;i++){
		b[i]=fabs(a[i]-a[i+1]);
	}
	//b[]={3,2,1};
	//对数组排序,模板,记住!
	for(i=0;i<n-1;i++){
		 for(j=i;j<n-1;j++){
			if(b[i]>b[j]){
				int temp=b[i];
				b[i]=b[j] ;
				b[j]=temp;
			}
			  
		 }
	}
	//b[]={1,2,3};
	//3个值只需要测n-2下,用i==n-2很巧妙判断出来
	for(i=0;i<n-2;i++){
		if(b[i+1]-b[i]==1){
			cout<<i<<endl;
			continue;
		}
		break;
	}
	if(i==n-2){
		 cout<<"jolly";
	}
	else{
		cout<<"not jolly";
	}
	return 0;
}

在附上C++的解法,主要熟悉vector数组的push_back

#include <iostream>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<math.h>
/* 3.9日23:55 北大百炼OJ 2019年推免复试A题 简单枚举+数组排序 */
using namespace std;
int main(int argc, char** argv) {
	int n,x,ans,k;
	vector<int> a;
	vector<int> b;
	a.push_back(0);
	b.push_back(0);
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>x;
		a.push_back(x);
	}
	for(int i=1;i<n;i++){
		ans=abs(a[i+1]-a[i]);
		b.push_back(ans);
	}
	sort(b.begin(),b.end()) ;
	for(int i=1;i<n;i++){
		if(b[i]==i){
			k++;
		}
	}
	if(k==n-1){
		cout<<"jolly"<<endl;
	}
	else{
		cout<<"Not jolly"<<endl;
	}
	return 0;
}
发布了8 篇原创文章 · 获赞 3 · 访问量 170

猜你喜欢

转载自blog.csdn.net/weixin_42491641/article/details/104765134
3.9