本文主要是介绍重庆大学2018编程大赛赛题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
A.放置鲜花问题
import java.util.Scanner;
公共课Main {// static int MAXarrSize = 20000;public static void main(String [] args){int n;扫描仪扫描仪=新扫描仪(System.in); String line = scanner.nextLine();行= line.trim();int linesize = line.length();int flowerbed [] = new int [(linesize-2)/ 2 + 2];//System.out.println(flowerbed[0]);int numindex = 1;for(int i = 1; i <linesize-2; i + = 2){花坛[numindex] =的Integer.parseInt(line.substring(I,I + 1)); numindex + = 1;}/ * for(int i = 0; i <flowerbed.length; i ++){的System.out.println(花坛[I]);} * /N =的Integer.parseInt(line.substring(LINESIZE-1,LINESIZE));//System.out.println(n);System.out.println(ifPlaned(flowerbed,n));}静态字符串ifPlaned(int arr [],int n){int length = arr.length;int count = n;如果(计数<0){返回“假”;}for(int i = 1; i <length-1; i ++){如果(计数<= 0){返回“真”;}如果(ARR [I] == 1){继续;}如果(ARR [I] == 0){如果(ARR [I-1] == 0 && ARR第[i + 1] == 0){ARR [I] = 1;计数 - ;}其他{继续;}}}如果(计数> 0){返回“假”;}如果(计数<= 0){返回“真”;}返回“假”; }
}
2.融雪问题
包È;
import java.util.Scanner;
公共课Main { public static void main(String [] args){ 扫描仪扫描仪=新扫描仪(System.in); 整天; 天= scanner.nextInt (); int daySnow [] = new int [day]; for(int i = 0; 我<day; i ++){ daySnow [I] = scanner.nextInt(); } int Tday [] = new int [day]; for(int i = 0; 我<day; i ++){ Tday [I] = scanner.nextInt(); } int result [] = getResult(day,daySnow,Tday); //System.out.println(); for(int i = 0; 我<day; i ++){ 如果(i ==天-1){ 是System.out.print(导致[I]); }其他{ System.out.print(result [i] +“”); } }} int int [] getResult(int day,int [] daySnow,int [] Tday){ int result [] = new int [day];} for(int i = 0; 我<day; i ++){ int resultday = 0; for(int j = 0; j <= i; j ++){ 如果(daySnow [J]> Tday [I]){ resultday + = Tday [I]; daySnow [J] - = Tday [I]; }其他{ resultday + = daySnow [J]; daySnow [J] = 0; } } 结果[I] = resultday; } 返回结果; }
}
3.组正方形
Java的的的暴力代码但是会超时
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.ListIterator;
import java.util.Scanner;
公共课Main {
public static void main(String [] args){扫描仪扫描仪=新扫描仪(System.in);int N;N = scanner.nextInt();int cases [] [] = new int [N] [];for(int i = 0; i <N; i ++){int length = scanner.nextInt();例[i] = new int [length];for(int j = 0; j <length; j ++){cases [i] [j] = scanner.nextInt();}}
/ * for(int i = 0; i <N; i ++){for(int j = 0; j <cases [i] .length; j ++){是System.out.print(例[i] [j]);}的System.out.println();} * /String result [] = new String [N];result = getresult(N,cases);for(int i = 0; i <N; i ++){的System.out.println(结果[I]);}}
private static String [] getresult(int n,int [] cases]){字符串结果[] = new String [n];// String result [] = new String [n];for(int i = 0; i <n; i ++){结果[I] = “无”;}for(int i = 0; i <n; i ++){List <List> reslist = new ArrayList <List>();long longn =(long)Math.pow(2,cases [i] .length);清单组合;//求所有组合for(long l = 0L; l <longn; l ++){combine = new ArrayList <>();for(int j = 0; j <cases [i] .length; j ++){如果((1- >>> J&1)== 1){//combine.add(list.get(i));combine.add(例[i] [j]);}}reslist.add(结合);}int casess [] = new int [(int)longn];for(int o = 0; o <casess.length; o ++){int res = 0;for(int j = 0; j <reslist.get(o).size(); j ++){RES + =(int)的reslist.get(O)获得(j)的;}//System.out.println(res);casess [O] = RES;}//System.out.println("&&&&&&&&“);int length = reslist.size();int count = 0;for(int j = 0; j <length; j ++){计数= 1;for(int k = 0; k <length; k ++){如果(K!= j的&& casess [J] == casess [K]){//System.out.println(cases[i][k]);计数+ = 1;//System.out.println(count);}如果(计数> = 4){结果[I] = “是”;打破;}}如果(计数> = 4){结果[I] = “是”;打破;} }}返回结果;}
}
ç语言DFS优化代码
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <算法>
使用namespace std;
int N,seq [25],使用[25],mode;
bool dfs(int cap,int last,int num)
{if(num == N){返回true;}if(cap == 0){if(dfs(mode,N + 1,num)){返回true;}else {返回false;}}else {for(int i = last-1; i> = 1; --i){if(cap> = seq [i] &&!use [i]){使用[i] = 1;if(dfs(cap-seq [i],i,num + 1)){返回true;}else {使用[i] = 0;if(i == N){返回false;}while(seq [i-1] == seq [i])--i;} }}}返回false;
}
int main()
{int T,sum,Max;scanf(“%d”,&T);而(T--){sum = 0,Max = -1; memset的(使用,0,的sizeof(使用)); 的scanf(“%d”,&N); for(int i = 1; i <= N; ++ i){ scanf(“%d”,&seq [i]); Max = max(Max,seq [i]); sum + = seq [i]; } if(sum%4!= 0 || max> sum / 4){ 放(“否”); 继续; } sort(seq + 1,seq + N + 1); mode = sum / 4; //每一边的大小printf(dfs(mode,N + 1,0)?“yes \ n”:“no \ n”); } 返回0;
}
这篇关于重庆大学2018编程大赛赛题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!