蓝桥杯准备阶段(练习CCF题集)

CCF题集测试页面

报数——Java版

package 蓝桥杯;

import java.util.Scanner;

public class P1_报数1 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner reader=new Scanner(System.in);
		int n=reader.nextInt();
		int number=0;//数字
		int count=0;//一个去逼近n的计数器。
		int[] player= {0,0,0,0,0};//玩家
		while(n>count) {
			number++;
			if(number%7==0||number%10==7||(number/10)%10==7) {//判断数字是否需要跳过
				if(number%4==1) {
					player[1]+=1;
				}
				else if(number%4==2) {
					player[2]+=1;
				}
				else if(number%4==3) {
					player[3]+=1;
				}
				else if(number%4==0) {
					player[4]+=1;
				}
			}
			else count++;
		}
		for(int i=1;i<=4;i++) {
			System.out.println(player[i]);
		}
	}
}
//在java开发过程中,应尽量少的创建实例对象,为的就是减少系统的开销。——java内存分配机制。

小明种苹果——Java版

package 蓝桥杯;

import java.util.Scanner;

class appletree{
	  int startnum=0;
	  int num=0;
}
//the static field should be accesse in a static way
//解决办法是不要将类里面的变量定义为static,并且要重新创建存放在数组里面的对象。
public class P2_小明种苹果 {

	public static void main(String[] args) {
		Scanner reader=new Scanner(System.in);
		int n=reader.nextInt();
		int m=reader.nextInt();
		int mbake=m;
		appletree[] tree =new appletree[1000000];//数组越界将会出现运行错误,这都是血淋淋的经验。
		int j=0;
		int T=0,K=0,P=-1;
		while(n!=0) {
			appletree t=new appletree();
			t.startnum=reader.nextInt();
			T+=t.startnum;
			while(mbake>0) {
			t.num-=reader.nextInt();
			mbake--;
			}
			T-=t.num;
			tree[j]=t;
			if(t.num>P) {
				P=t.num;
				K=j+1;
			}
			n--;
			j++;
			mbake=m;
			
		}
		System.out.println(T+" "+K+" "+P);
		reader.close();
	}
}

发布了20 篇原创文章 · 获赞 1 · 访问量 420

猜你喜欢

转载自blog.csdn.net/XMY_UPUPUP/article/details/103932276
今日推荐