乐视编程题通过率100%的大牛贴个程序呗

乐视编程题通过率100%的大牛贴个程序呗
全部评论
Java写的,为了图快变量名比较逗逼,请见谅 第一题 import java.util.*; public class test1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNextInt()) { int no = in.nextInt(); List<List<Integer>> kezhi = new ArrayList<>(); for(int i = 0; i < no; i++) { String kezhi_str = in.next(); ArrayList<Integer> kezhi_lst = new ArrayList<>(); for(char c: kezhi_str.toCharArray()) { kezhi_lst.add(c - '0'); } kezhi.add(kezhi_lst); } int cnt = generateMethod(kezhi, no-1).size(); System.out.println(cnt); } } public static List<Set<Integer>> generateMethod(List<List<Integer>> kezhi, int i) { if(i == 0) { List<Set<Integer>> res = new ArrayList<>(); for(int duishou: kezhi.get(0)) { Set<Integer> duishoujihe = new HashSet<>(); duishoujihe.add(duishou); res.add(duishoujihe); } return res; } else { List<Set<Integer>> zhiqianduishoujiheliebiao = generateMethod(kezhi, i-1); List<Set<Integer>> res = new ArrayList<>(); for(Set<Integer> zhiqianduishoujihe: zhiqianduishoujiheliebiao) { for(int duishou: kezhi.get(i)) { if(!zhiqianduishoujihe.contains(duishou)) { Set<Integer> xinduishoujihe = new HashSet<>(); xinduishoujihe.addAll(zhiqianduishoujihe); xinduishoujihe.add(duishou); res.add(xinduishoujihe); } } } return res; } } } 第一题简单地利用递归就能满足要求了,不用想得很复杂,就算超时了再改就行了 *************************************************************************************************** 第二题 import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class test2 { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNextLong()) { int cost = in.nextInt(); long money = in.nextInt(); long cnt = 0; long min_paid = (long)Math.ceil(cost/0.95); long max_paid = Math.min((long)Math.floor(cost/0.90), money); while(min_paid%5 != 0) min_paid++; while (max_paid%5 != 0) max_paid--; if(max_paid >= min_paid) cnt = (max_paid - min_paid) / 5 + 1; System.out.println(cnt); } } } 第二题主要是int有可能会溢出,所以尽量用long,还有就是边界处理有一点麻烦 ************************************************************************************************ 第三题 import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class test3 { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNextInt()) { long S = in.nextLong(); long cnt = 0; for(long i = 0; i*i < S; i++) { long resi = S - i*i; if(hasRoot(resi)) cnt++; } System.out.println(cnt*4); } } public static boolean hasRoot(long i) { if(i == 0 || i == 1) return true; long p = 0, r = i; while(true) { long q = p + (r-p)/2; if(q*q > i) r = q; else if(q*q < i) p = q; else return true; if(p > r) return false; if(r-p <= 1) { if(r*r == i || p*p == i) return true; else return false; } } } } 第三题也是可能有溢出问题,所有直接上long,然后就是判断整数有没有根用了一个二分法
点赞 回复
分享
发布于 2016-06-15 12:13
我觉得第一题最难了 
点赞 回复
分享
发布于 2016-06-14 22:15
阅文集团
校招火热招聘中
官网直投
等下整理下,我发给你,给个邮箱吧
点赞 回复
分享
发布于 2016-06-14 21:17
//第3题,5%—10%小费 #include<iostream> #include <stdio.h> using namespace std; int main() { long a, b, count, start; long double min, max; while(cin>>a>>b) { count = 0; min = a*(1.0+0.05/0.95); max = a*(1+0.1/0.9); start = long(min/5); start *= 5; for (long i = start; i <= max && i <= b; i +=5) { if (min <= i && max >= i && i <= b) { count++; } } cout<<count<<endl; } } //第4题,圆形魔法 #include<iostream> #include <stdio.h> #include<math.h> using namespace std; int main() { int count; long s, y; long double k; while(cin>>s) { count = 0; k = sqrt(s/2.0); for (long x = 1; x < k-0.01; ++x) { y = (int)sqrt((s-x*x)*1.0); if ( x*x+y*y == s ) { count++; } } count*=8; if (sqrt(s*1.0) == (int)sqrt(s*1.0)) { count+=4; } if (sqrt(s*2.0) == (int)sqrt(s*2.0)) { count+=4; } cout<<count<<endl; } }
点赞 回复
分享
发布于 2016-06-14 21:22
有没有java写的啊?求java大神贴一下呗
点赞 回复
分享
发布于 2016-06-14 21:51
//第一题 #include <iostream> #include <cstring> #include <vector> using namespace std; void dfs(vector<string> strs, int index, int len, int &count, vector<int> &ans) { if(index==len) { int a[6]={0}; for(int j=0;j<ans.size();j++) { a[ans[j]]++; } for(int k=0;k<=5;k++) { if(a[k]>1) { return; } } count++; return; } for(int m=0;m<strs[index].length();m++) { ans.push_back(strs[index][m]-'0'); dfs(strs,index+1,len,count,ans); ans.pop_back(); } return; } int main() { int n; while(cin>>n) { vector<string> strs; for(int i=0;i<n;i++) { string tmp; cin>>tmp; strs.push_back(tmp); } int len; int count=0; len=strs.size(); vector<int> ans; dfs(strs,0,len,count,ans); cout<<count<<endl; } return 0; }
点赞 回复
分享
发布于 2016-06-14 22:58
//第一题 //当时一直没看懂题意,后来没时间了,反正就是水,受不了自己 #include<stdio.h> #include<string.h> #include<math.h> #include<iostream> #include<string> using namespace std; typedef long long ll; int n, res, vis[1000]; string s[100]; void dfs(int k, char c) { if(k == n) {res++; return ;} int ok = 0; for(int j=0; j<s[k].size(); j++) { if(!vis[s[k][j]]) { vis[s[k][j]] = 1; dfs(k+1,s[k][j]); vis[s[k][j]] = 0; ok = 1; } } if(ok == 0) return ; } int main() { while(cin >> n) { memset(vis, 0, sizeof(vis)); for(int i=0; i<n; i++) cin >> s[i]; res = 0; for(int j=0; j<s[0].size(); j++) { vis[s[0][j]] = 1; dfs(1,s[0][j]); vis[s[0][j]] = 0; } printf("%d\n", res); } return 0; }
点赞 回复
分享
发布于 2016-06-14 22:59
请问用js怎么写啊=_=
点赞 回复
分享
发布于 2016-06-15 10:33
做了两个题,第一个不会,哭瞎
点赞 回复
分享
发布于 2016-06-15 10:36
不会第一题是最难的吧,我看完第一题后面就都没看~ 悲剧了
点赞 回复
分享
发布于 2016-06-15 10:39
虽然全部写完了,但是写的很乱
点赞 回复
分享
发布于 2016-06-15 11:49
//最后一题 这要是复杂度,通过一层循环,每次一个横坐标,总会有一个纵坐标对应,然后就是检测了 import java.util.Scanner; public class Main { private static int count = 0; private static int S = 0; public static void main(String[] args) { Scanner in = new Scanner(System.in); while(in.hasNext()){ S = in.nextInt(); for(int i = 0;i <= (int)Math.sqrt(S);i++){ int j = (int)Math.sqrt(S - i * i); if(j * j + i * i == S && j != 0){ count++; } } count *= 4; if(S == 0){ count = 1; } System.out.println(count); count = 0; } } }
点赞 回复
分享
发布于 2016-06-15 15:34
贴个第一题的吧,深度优先搜索一下 public class Main { static int cnt=0; public static void main(String[] args){ Scanner s=new Scanner(System.in); String str=""; int n; while(s.hasNext()){ cnt=0; n=s.nextInt(); str=s.nextLine(); str=s.nextLine(); String[] strs=str.split(" "); int i ; int len=strs.length; int a[]=new int[6]; F(strs,0,n,a); System.out.println(cnt); } } public static void F(String[] strs,int t,int n,int a[]){ if(t>=n){ cnt++; return ; }else{ int i ; for(i=0;i<strs[t].length();i++){ if(a[strs[t].charAt(i)-'0']==0){ a[strs[t].charAt(i)-'0']=1; F(strs,t+1,n,a); a[strs[t].charAt(i)-'0']=0; } } return ; } } }
点赞 回复
分享
发布于 2016-06-15 16:11
人工阅卷吗?
点赞 回复
分享
发布于 2016-06-15 18:31
表示一个也没有写出来。、、、哎!!!!
点赞 回复
分享
发布于 2016-06-16 18:29
第一题: import java.util.Scanner; public class Main { static int count=0; public static void main(String args[]){ Scanner input=new Scanner(System.in); int num[]=new int[1000]; int index=0; while(input.hasNext()){ int n=input.nextInt(); String s[]=new String[n]; for(int i=0;i<n;i++){ s[i]=input.next(); } boolean flag[]=new boolean[6]; backtrack(0,n,flag,s); num[index]=count; count=0; index++; } for(int i=0;i<index;i++){ System.out.println(num[i]); } } public static void backtrack(int i,int n,boolean flag[],String s[]){ if(i==n){ count++; }else{ for(int j=0;j<s[i].length();j++){ int p=Integer.parseInt((String)(s[i].charAt(j)+"")); if(!flag[p]){ flag[p]=!flag[p]; backtrack(i+1,n,flag,s); flag[p]=!flag[p]; } } } } } 第二题: import java.util.Scanner; public class Main{ static int count=0; public static void main(String args[]){ Scanner input=new Scanner(System.in); int num[]=new int[10000]; int index=0; while(input.hasNext()){ int A=input.nextInt(); int B=input.nextInt();    int begin=A/19;             if(begin*19<A)                 begin++;             while(((begin+A)%5!=0)&&(begin+A)<=B){                 begin++;             }    int end=A/9;    for(int i=begin;i<=end;i=i+5){    if((A+i<=B)&&((A+i)%5==0)){    count++;    }    }    num[index]=count;    count=0;    index++; } for(int i=0;i<index;i++){ System.out.println(num[i]); } } } 第三题: import java.util.Scanner; public class Main { public static void main(String args[]){ Scanner input=new Scanner(System.in); int num[]=new int[10000]; int index=0; while(input.hasNext()){ int count=0; int S=input.nextInt(); int r=(int)Math.sqrt((double)S); for(int i=0;i<=r;i++){ int p=(int)Math.sqrt(S-i*i); int sum=i*i+p*p; if(sum==S){ if(i==0||p==0){ count+=2; }else{ count+=4; } } } num[index]=count; index++; } for(int i=0;i<index;i++){ System.out.println(num[i]); } } }
点赞 回复
分享
发布于 2016-06-18 18:16

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务