第七届蓝桥杯大赛个人省赛javaB组

 


1.煤球数目


有一堆煤球,堆成三角棱锥形。具体:
第一层放1个,
第二层3个(排列成三角形),
第三层6个(排列成三角形),
第四层10个(排列成三角形),
....
如果一共有100层,共有多少个煤球?


请填表示煤球总数目的数字。

 

 

注意:你提交的应该是一个整数,不要填写任何多余的内容或说明性文字。

 

答案:171700

 

public class text
{
	public static void main(String[] args) {
		int all = 1;
		int number = 1;
		for(int i = 2; i <= 100; i++){
			number += i;
			all+= number;
		}
		System.out.println(all);
	}
	
}

 

 


2.生日蜡烛


某君从某年开始每年都举办一次生日party,并且每次都要吹熄与年龄相同根数的蜡烛。


现在算起来,他一共吹熄了236根蜡烛。



请问,他从多少岁开始过生日party的?


请填写他开始过生日party的年龄数。
注意:你提交的应该是一个整数,不要填写任何多余的内容或说明性文字。

 

 

 

 

 

答案:26

 

public class text
{
	public static void main(String[] args) {
		int all = 0;
		for(int i=1;i<80;i++){
			for(int j= i; j <80;j++){
					all +=j;
					if(all >= 236){
						break;
				}
			}
				if(all == 236){
					System.out.println(i);
					break;
				}else{
					all = 0;
				}
			}
		
	}
	
}

 

 

 

3.凑算式


     B      DEF
A + --- + ------- = 10
     C      GHI
     
(如果显示有问题,可以参见【图1.jpg】)
 
 
这个算式中A~I代表1~9的数字,不同的字母代表不同的数字。


比如:
6+8/3+952/714 就是一种解法,
5+3/1+972/486 是另一种解法。


这个算式一共有多少种解法?


注意:你提交应该是个整数,不要填写任何多余的内容或说明性文字。

 

 

 

 

答案:29

 

 

这题我选择暴力解法

 

public class text
{
	static int a[] = {1,2,3,4,5,6,7,8,9};
	static int n = a.length;
	static double all = 10;
	static int count = 0;
	public static void main(String[] args) {
		perm(0);
		System.out.println(count);
	}
	
	public static void  perm(int offset){
		
		if(offset == n-1){
			compare();
			return;
		}
		for(int i= offset; i< n; i++){
			swap(i, offset);
			perm(offset+1);
			swap(i,offset);
		}
		
	}
	
	public static void swap(int i, int offset){
		int temp;
		
		temp =a[offset];
		a[offset] =a[i];
		a[i] = temp;
		
	}
	public static void compare(){
		 double number = a[0] + a[1]*1.0/a[2] + (a[3]*100+a[4]*10+a[5])*1.0/(a[6]*100+a[7]*10+a[8]);
		 if(Math.abs(number - all) < 0.000001 ){
			 count++;
		 }
		 
	}
}

 

 

 

 

 

4.分小组


9名运动员参加比赛,需要分3组进行预赛。
有哪些分组的方案呢?


我们标记运动员为 A,B,C,... I
下面的程序列出了所有的分组方法。


该程序的正常输出为:
ABC DEF GHI
ABC DEG FHI
ABC DEH FGI
ABC DEI FGH
ABC DFG EHI
ABC DFH EGI
ABC DFI EGH
ABC DGH EFI
ABC DGI EFH
ABC DHI EFG
ABC EFG DHI
ABC EFH DGI
ABC EFI DGH
ABC EGH DFI
ABC EGI DFH
ABC EHI DFG
ABC FGH DEI
ABC FGI DEH
ABC FHI DEG
ABC GHI DEF
ABD CEF GHI
ABD CEG FHI
ABD CEH FGI
ABD CEI FGH
ABD CFG EHI
ABD CFH EGI
ABD CFI EGH
ABD CGH EFI
ABD CGI EFH
ABD CHI EFG
ABD EFG CHI
..... (以下省略,总共560行)。


public class A
{
public static String remain(int[] a)
{
String s = "";
for(int i=0; i<a.length; i++){
if(a[i] == 0) s += (char)(i+'A');
}
return s;
}

public static void f(String s, int[] a)
{
for(int i=0; i<a.length; i++){
if(a[i]==1) continue;
a[i] = 1;
for(int j=i+1; j<a.length; j++){
if(a[j]==1) continue;
a[j]=1;
for(int k=j+1; k<a.length; k++){
if(a[k]==1) continue;
a[k]=1;
System.out.println(__________________________________);  //填空位置
a[k]=0;
}
a[j]=0;
}
a[i] = 0;
}
}

public static void main(String[] args)
{
int[] a = new int[9];
a[0] = 1;

for(int b=1; b<a.length; b++){
a[b] = 1;
for(int c=b+1; c<a.length; c++){
a[c] = 1;
String s = "A" + (char)(b+'A') + (char)(c+'A');
f(s,a);
a[c] = 0;
}
a[b] = 0;
}
}
}


仔细阅读代码,填写划线部分缺少的内容。


注意:不要填写任何已有内容或说明性文字。

答案:s+ " " + (char)(i+'A') + (char)(j + 'A')+ (char)(k + 'A') + " " + remain(a)

这题好可惜没做出来,想了好久,中间三个写不出来,关键是代码没有完全看清楚

 

 



抽签


X星球要派出一个5人组成的观察团前往W星。
其中:
A国最多可以派出4人。
B国最多可以派出2人。
C国最多可以派出2人。
....


那么最终派往W星的观察团会有多少种国别的不同组合呢?


下面的程序解决了这个问题。
数组a[] 中既是每个国家可以派出的最多的名额。
程序执行结果为:
DEFFF
CEFFF
CDFFF
CDEFF
CCFFF
CCEFF
CCDFF
CCDEF
BEFFF
BDFFF
BDEFF
BCFFF
BCEFF
BCDFF
BCDEF
....
(以下省略,总共101行)




public class A
{
public static void f(int[] a, int k, int n, String s)
{
if(k==a.length){ 
if(n==0) System.out.println(s);
return;
}

String s2 = s;
for(int i=0; i<=a[k]; i++){
_____________________________;   //填空位置
s2 += (char)(k+'A');
}
}

public static void main(String[] args)
{
int[] a = {4,2,2,1,1,3};

f(a,0,5,"");
}
}




仔细阅读代码,填写划线部分缺少的内容。


注意:不要填写任何已有内容或说明性文字。

答案:f(a,k+1,n-i,s2);

这题涉及到深度优先搜索,不难。

 



方格填数


如下的10个格子
   +--+--+--+
   |  |  |  |
+--+--+--+--+
|  |  |  |  |
+--+--+--+--+
|  |  |  |
+--+--+--+


(如果显示有问题,也可以参看【图1.jpg】)


填入0~9的数字。要求:连续的两个数字不能相邻。
(左右、上下、对角都算相邻)


一共有多少种可能的填数方案?


请填写表示方案数目的整数。
注意:你提交的应该是一个整数,不要填写任何多余的内容或说明性文字。

 

答案:1580

 

public class text {  
  
    public static void main(String[] args) {  
  
       
          
        //只要结果,暴力穷举  
        int count=0;  
        //个位  
        for (int a = 0; a < 10; a++) {  
            for (int b = 0; b < 10; b++) {  
                if(a==b) continue;  
                for (int c = 0; c < 10; c++) {  
                    if(c==b||c==a) continue;  
                    for (int d = 0; d < 10; d++) {  
                        if(d==a||d==b||d==c) continue;  
                        for (int e = 0; e < 10; e++) {  
                            if(e==a||e==b||e==c||e==d) continue;  
                            for (int f = 0; f < 10; f++) {  
                                if(f==a||f==b||f==c||f==d||f==e) continue;  
                                for (int g = 0; g < 10; g++) {  
                                    if(g==a||g==b||g==c||g==d||g==e||g==f) continue;  
                                    for (int h = 0; h < 10; h++) {  
                                        if(h==a||h==b||h==c||h==d||h==e||h==f||h==g) continue;  
                                        for (int i = 0; i < 10; i++) {  
                                            if(i==a||i==b||i==c||i==d||i==e||i==f||i==g||i==h) continue;  
                                            {  
                                                for (int j = 0; j < 10; j++) {  
                                                    if(j==a||j==b||j==c||j==d||j==e||j==f||j==g||j==h||j==i) continue;  
                                                    if(check(a,b,c,d,e,f,g,h,i,j))  
                                                    {  
                                                        System.out.println(a+" "+b+" "+c+" "+d+" "+e+" "+f+" "+g+" "+h+" "+i+" "+j);  
                                                        count++;  
                                                    }  
                                                }  
                                            }  
                                        }  
                                    }  
                                }  
                            }  
                        }  
                    }  
                }  
            }  
        }  
        System.out.println(count);  
    }  
  
    private static boolean check(int a, int b, int c, int d,int e, int f, int g, int h, int i, int j) {  
          
        //a--->b,d,e,f  
        if(Math.abs(a-b)==1||Math.abs(a-e)==1||Math.abs(a-f)==1||Math.abs(a-d)==1){  
            return false;  
        }  
        //b--->c,e,f,g  
        if(Math.abs(b-c)==1||Math.abs(b-e)==1||Math.abs(b-f)==1||Math.abs(b-g)==1){  
            return false;  
        }  
        //c-->f,g  
        if(Math.abs(c-f)==1||Math.abs(c-g)==1){  
            return false;  
        }  
          
        //d--->e,h,i  
        if(Math.abs(d-e)==1||Math.abs(d-h)==1||Math.abs(d-i)==1){  
            return false;  
        }  
          
        //e--->f,i,j  
        if(Math.abs(e-f)==1||Math.abs(e-i)==1||Math.abs(e-j)==1){  
            return false;  
        }  
          
        //f--->g,j  
        if(Math.abs(f-g)==1||Math.abs(f-j)==1){  
            return false;  
        }  
          
        //h--->i,e  
        if(Math.abs(h-i)==1||Math.abs(h-e)==1){  
            return false;  
        }  
          
        //i--->j,f  
        if(Math.abs(i-j)==1||Math.abs(i-f)==1){  
            return false;  
        }  
          
        //j--->g  
        if(Math.abs(j-g)==1){  
            return false;  
        }  
          
        return true;  
    }  
}  

 

 


7.剪邮票


如【图1.jpg】, 有12张连在一起的12生肖的邮票。
现在你要从中剪下5张来,要求必须是连着的。
(仅仅连接一个角不算相连)
比如,【图2.jpg】,【图3.jpg】中,粉红色所示部分就是合格的剪取。


请你计算,一共有多少种不同的剪取方法。


请填写表示方案数目的整数。
注意:你提交的应该是一个整数,不要填写任何多余的内容或说明性文字。

思路:先找到5个数的组合,然后从第一个数字开始遍历,经过上下左右操作检测5个数是否都被访问一遍,如果5个数都可以遍历到则种类+1。

在原图中向上为-4,向下为+4,向左为-1,向右为+1,但是遇到3 4 5 7 8这种4+1=5但是这种情况不符合,所以重构一下原图:

 1   2   3   4

 6   7   8   9

11 12 13 14

 

这样,向上为-5,向下为+5,向左为-1,向右为+1,避免了每行最后一个+1后等于下一行第一个的情况。

 

答案:116

#include <iostream>  
using namespace std;  
int mp[12]= {1,2,3,4,6,7,8,9,11,12,13,14};  
int aa[5],vis[5],sum=0;  
int b[4]= {-1,1,-5,+5};  
void dfs(int n)  
{  
    for(int i=0; i<4; i++)  
    {  
        int t=aa[n]+b[i];  
        if(t<1||t>14||t==5||t==10) continue;  
        for(int j=0; j<5; j++)  
            if(!vis[j]&&aa[j]==t)  
            {  
                vis[j]=1;  
                dfs(j);  
            }  
    }  
}  
  
int main()  
{  
  
    for(int a=0; a<12; a++)  
        for(int b=a+1; b<12; b++)  
            for(int c=b+1; c<12; c++)  
                for(int d=c+1; d<12; d++)  
                    for(int e=d+1; e<12; e++)  
                    {  
                        aa[0]=mp[a];  
                        aa[1]=mp[b];  
                        aa[2]=mp[c];  
                        aa[3]=mp[d];  
                        aa[4]=mp[e];  
                        for(int i=0; i<5; i++)  
                            vis[i]=0;  
                        vis[0]=1; 
			//深搜 
                        dfs(0);  
                        int flag=1;;  
                        for(int i=0; i<5; i++)  
                        {  
                            if(vis[i]!=1)  
                            {  
                                flag=0;  
                                break;  
                            }  
                        }  
                        if(flag==0) continue;  
                        else  
                            sum++;  
                    }  
  
    cout<<sum<<endl;  
  
    return 0;  
}  			//深搜 
                        dfs(0);  
                        int flag=1;;  
                        for(int i=0; i<5; i++)  
                        {  
                            if(vis[i]!=1)  
                            {  
                                flag=0;  
                                break;  
                            }  
                        }  
                        if(flag==0) continue;  
                        else  
                            sum++;  
                    }  
  
    cout<<sum<<endl;  
  
    return 0;  
}  

//广搜

#include <iostream>  
#include <cstring>  
#include <algorithm>  
#include <queue>  
using namespace std;  
int a[12]= {1,2,3,4,6,7,8,9,11,12,13,14},vis[20];  
int main()  
{  
    int sum=0;  
    for(int i1=0; i1<12; i1++)  
        for(int i2=i1+1; i2<12; i2++)  
            for(int i3=i2+1; i3<12; i3++)  
                for(int i4=i3+1; i4<12; i4++)  
                    for(int i5=i4+1; i5<12; i5++)  
                    {  
                        memset (vis,0,sizeof(vis));  
                        int p=0;  
                        vis[a[i1]]=1;  
                        vis[a[i2]]=1;  
                        vis[a[i3]]=1;  
                        vis[a[i4]]=1;  
                        vis[a[i5]]=1;  
                        queue<int>q;  
                        q.push(a[i1]);  
                        vis[a[i1]]=0;  
                        while(!q.empty())  
                        {  
                            int top=q.front();  
                            q.pop();  
                            p++;  
                            if(vis[top+1]) { q.push(top+1);vis[top+1]=0; }  
                            if(vis[top+5]) { q.push(top+5);vis[top+5]=0; }  
                            if(vis[top-1]) { q.push(top-1);vis[top-1]=0; }  
                            if(vis[top-5]) { q.push(top-5);vis[top-5]=0; }  
                        }  
                        if(p==5)  
                        {  
                            sum++;  
                            cout<<a[i1]<<" "<<a[i2]<<" "<<a[i3]<<" "<<a[i4]<<" "<<a[i5]<<endl;  
                        }  
                    }  
    cout<<sum<<endl;  
  
    return 0;  
}  




8.四平方和
四平方和定理,又称为拉格朗日定理:
每个正整数都可以表示为至多4个正整数的平方和。
如果把0包括进去,就正好可以表示为4个数的平方和。
比如:
5 = 0^2 + 0^2 + 1^2 + 2^2
7 = 1^2 + 1^2 + 1^2 + 2^2
(^符号表示乘方的意思)
对于一个给定的正整数,可能存在多种平方和的表示法。
要求你对4个数排序:
0 <= a <= b <= c <= d
并对所有的可能表示法按 a,b,c,d 为联合主键升序排列,最后输出第一个表示法
程序输入为一个正整数N (N<5000000)
要求输出4个非负整数,按从小到大排序,中间用空格分开
例如,输入:
5
则程序应该输出:
0 0 1 2


再例如,输入:
12
则程序应该输出:
0 2 2 2

再例如,输入:
773535
则程序应该输出:
1 1 267 838

资源约定:
峰值内存消耗(含虚拟机) < 256M
CPU消耗  < 3000ms
请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。
所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。
注意:不要使用package语句。不要使用jdk1.7及以上版本的特性。
注意:主类的名字必须是:Main,否则按无效代码处理。

 

解题思路:用暴力依次层层遍历,每层的最大值不会超过输入值的平方根,这样能避免遍历数据的值过大。

 

 

 

import java.util.Scanner;

public class Test{
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
	
		int maxn = (int)Math.sqrt(n);
		for(int i = 0; i <= maxn; i++) {
			for(int j = 0; j <= maxn; j++) {
				for(int k = 0; k <= maxn; k++) {
					for(int m = 0; m <= maxn; m++) {
						
						int sum = i*i + j*j + k*k + m*m	;	
						if(sum == n) {
							
							System.out.println(i + " " + j + " " + k + " " + m + " ");
							return ;
						}
					}
				}
			}
		}
	}
}

 

 



取球博弈


两个人玩取球的游戏。
一共有N个球,每人轮流取球,每次可取集合{n1,n2,n3}中的任何一个数目。
如果无法继续取球,则游戏结束。
此时,持有奇数个球的一方获胜。
如果两人都是奇数,则为平局。


假设双方都采用最聪明的取法,
第一个取球的人一定能赢吗?
试编程解决这个问题。


输入格式:
第一行3个正整数n1 n2 n3,空格分开,表示每次可取的数目 (0<n1,n2,n3<100)
第二行5个正整数x1 x2 ... x5,空格分开,表示5局的初始球数(0<xi<1000)


输出格式:
一行5个字符,空格分开。分别表示每局先取球的人能否获胜。
能获胜则输出+,
次之,如有办法逼平对手,输出0,
无论如何都会输,则输出-


例如,输入:
1 2 3
1 2 3 4 5


程序应该输出:
+ 0 + 0 -


再例如,输入:
1 4 5
10 11 12 13 15


程序应该输出:
0 - 0 + +


再例如,输入:
2 3 5
7 8 9 10 11


程序应该输出:
+ 0 0 0 0




资源约定:
峰值内存消耗(含虚拟机) < 256M
CPU消耗  < 3000ms




请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。


所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。
注意:不要使用package语句。不要使用jdk1.7及以上版本的特性。
注意:主类的名字必须是:Main,否则按无效代码处理。

答案:

 

 

 

 

import java.util.Scanner;
 
public class Main {
    public static int[] value = new int[1000];
    public static int[] getN = new int[3];
    public static int[] init = new int[5];
    public static char[] result = {'-','0','0','+'};
     
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        for(int i = 0;i < 3;i++)
            getN[i] = in.nextInt();
        for(int i = 0;i < 5;i++)
            init[i] = in.nextInt();
        int minN = Math.min(getN[0], Math.min(getN[1], getN[2]));
        if(minN % 2 == 1) //此处关于平局,两者是均为奇数,还是均为偶数问题,这里处理原因,是我自己猜想
            value[0] = 1;  //代表平局,;两者默认均为奇数个
        else
            value[0] = 2;  //代表平局,;两者默认均为偶数个
        for(int i = 1;i < minN;i++)
            value[i] = 2;  //0代表负,1和2代表平局,3代表胜
        for(int i = minN;i < 1000;i++) {
            int temp = 0;      //初始化,当前i个球,先取者必输
             
            for(int j = 0;j < 3;j++) {
                if(i - getN[j] < 0)
                    continue;
                if(i - getN[j] == 0 && getN[j] % 2 == 1)
                    temp = 3;
                if(value[i - getN[j]] == 0) {  //表示i - getN[j]个球先取时,必输
                    if(getN[j] % 2 == 0)
                        temp = 3;
                        //此时最终结果为两人取的球均为偶数个,但是若temp取三个数中另外一个数时
                        //,是必赢结果,则舍弃这个平局结果
                    else
                        temp = 2 > temp ? 2 : temp;
                }
                if(value[i - getN[j]] == 1) { //表示i - getN[j]个球先取时,两人取球均为奇数个
                    if(getN[j] % 2 == 0)
                        temp = 1 > temp ? 1 : temp;  //此处做比较同上
                }
                if(value[i - getN[j]] == 2) {//表示i - getN[j]个球先取时,两人取球均为偶数个
                    if(getN[j] % 2 == 1)
                        temp = 3;     //此种情况出现,必赢,不必做比较判断
                    else
                        temp = 2 > temp ? 2 : temp;   //此处比较同上,排除必输情况
                }
                if(value[i - getN[j]] == 3) {//表示i - getN[j]个球先取时,必赢
                    if(getN[j] % 2 == 1)
                        temp = 1 > temp ? 1 : temp;
                }
            }
             
            value[i] = temp;  //当前i个球,先取者最终输赢结果
        }
        //打印题意最终结果
        for(int i = 0;i < 5;i++)
            System.out.print(result[value[init[i]]]+" ");
    }
}

 

深搜解决

 

 

import java.util.Arrays;  
import java.util.Scanner;  
  
public class Main {  
    static int[] canGet = new int[3];  
    static int[][] cache;  
  
    public static void main(String[] args) {  
        Scanner in = new Scanner(System.in);  
        for (int i = 0; i < 3; i++)   
            canGet[i] = in.nextInt();  
        Arrays.sort(canGet);  
        for (int k = 0; k < 5; k++) {  
            int num = in.nextInt();  
            cache = new int[num + 1][num + 1];  
              
                for (int j = 0; j < num + 1; j++) {  
                    for (int j2 = 0; j2 < num + 1; j2++) {  
                        cache[j][j2] = -999;  
                    }  
                }  
            int ans = dfs(num, 0, 0);  
            if (ans == 1) {  
                System.out.print("+");  
            }  
            if (ans == -1) {  
                System.out.print("-");  
            }  
            if (ans == 0) {  
                System.out.print("0");  
            }  
            if (k != 4) {  
                System.out.print(" ");  
            }  
        }  
        System.out.println();  
    }  
  
    public static int dfs(int sum, int a, int b) {  
        if(cache[a][b]!=-999)  
            return cache[a][b];  
        if(sum<canGet[0]){  
            if(a%2==1&&b%2==0)  
                return cache[a][b]=1;  
            if(a%2==1 && b%2==1||(b%2==0 && a%2==0))  
                return cache[a][b]=0;  
            return cache[a][b]=-1;  
        }  
        int canwin=-999;  
        for(int i=0;i<canGet.length;i++){  
            if(sum-canGet[i]>=0){  
                canwin=Math.max(canwin, -dfs(sum-canGet[i], b, a+canGet[i]));  
            }  
        }  
        return cache[a][b]=canwin;  
              
    }  
}  

 

 

 

 

 

 

 



10.压缩变换


小明最近在研究压缩算法。
他知道,压缩的时候如果能够使得数值很小,就能通过熵编码得到较高的压缩比。
然而,要使数值很小是一个挑战。


最近,小明需要压缩一些正整数的序列,这些序列的特点是,后面出现的数字很大可能是刚出现过不久的数字。对于这种特殊的序列,小明准备对序列做一个变换来减小数字的值。


变换的过程如下:
从左到右枚举序列,每枚举到一个数字,如果这个数字没有出现过,刚将数字变换成它的相反数,如果数字出现过,则看它在原序列中最后的一次出现后面(且在当前数前面)出现了几种数字,用这个种类数替换原来的数字。


比如,序列(a1, a2, a3, a4, a5)=(1, 2, 2, 1, 2)在变换过程为:
a1: 1未出现过,所以a1变为-1;
a2: 2未出现过,所以a2变为-2;
a3: 2出现过,最后一次为原序列的a2,在a2后、a3前有0种数字,所以a3变为0;
a4: 1出现过,最后一次为原序列的a1,在a1后、a4前有1种数字,所以a4变为1;
a5: 2出现过,最后一次为原序列的a3,在a3后、a5前有1种数字,所以a5变为1。
现在,给出原序列,请问,按这种变换规则变换后的序列是什么。


输入格式:
输入第一行包含一个整数n,表示序列的长度。
第二行包含n个正整数,表示输入序列。


输出格式:
输出一行,包含n个数,表示变换后的序列。


例如,输入:
5
1 2 2 1 2


程序应该输出:
-1 -2 0 1 1


再例如,输入:
12
1 1 2 3 2 3 1 2 2 2 3 1


程序应该输出:
-1 0 -2 -3 1 1 2 2 0 0 2 2


数据规模与约定
对于30%的数据,n<=1000;
对于50%的数据,n<=30000;
对于100%的数据,1 <=n<=100000,1<=ai<=10^9




资源约定:
峰值内存消耗(含虚拟机) < 256M
CPU消耗  < 3000ms

 

 

请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。注意:不要使用package语句。不要使用jdk1.7及以上版本的特性。注意:主类的名字必须是:Main,否则按无效代码处理。答案:自己写的代码有点乱,再看看别人的代码,感觉好简洁

 

 

import java.util.Scanner;

public class Test{
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int arr[] = new int[n];
		int arr2[] = new int[n];
		int index = 0;
		boolean check = false;
		for(int i = 0; i < n; i++) {
			arr[i] = sc.nextInt();
		}
		for(int i = 0; i < n; i++) {
			check = false;
			for(int j =i-1; j >=0; j-- ) {
				if(arr[j] == arr[i]) {
					check = true;
					index = j;
					break;
				}
			}
			if(check == true) {
				//计算种类
				int count = 0;
				//两个数之间存在几个数
				int size = i - index - 1;
				if(size == 1) {
					if(arr[index+1] != arr[index]) {
						count++;
					}
				}
				if(size > 1) {
					int arr3[] = new int[size];
					arr3[0] = arr[index + 1];
					int count2 = size;
					int m = 0;
					int p = 1;
					for(int k = 1; k < size; k++ ) {
						
						int temp = arr[index+k+1];
						for(m = 0 ; m < k; m++) {
							if(temp == arr3[m]) {
								count2--;
								break;
							}
						}
						if(m == k) {
							arr3[p++] = temp;
							
							
						}
					}
					count = count2;
				}
				
				arr2[i] = count;
			}else {
				arr2[i] = -arr[i];
			}
		}
		for(int i = 0; i <n; i++) {
			System.out.print(arr2[i] +" ");
		}
                System.out.println(arr2[n-1]);
	}
}
import java.util.*;
public class Main{
    static int[] a,b;
    public static void main(String [] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){//相当于!=EOF
            int n = sc.nextInt();
            a = new int[n+5];
            b = new int[n+5];
            for(int i = 0; i < n; ++i)
                a[i] = sc.nextInt();
            for(int i = 0; i < n; ++i)
                b[i] = solve(i);
            for(int i = 0; i < n-1; ++i)
                System.out.print(b[i]+" ");
            System.out.println(b[n-1]);
        }
    }
    static int solve(int n){
        int tmp = a[n];
        HashSet<Integer> hashset = new HashSet<Integer>();
        for(int i = n-1; i >= 0; --i){
            if(a[i] == tmp){
                return hashset.size();
            }
            hashset.add(a[i]);
        }
        return (-1)*tmp;
    }
}

 

 

 

 

 
 
 
 
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值