日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

华为机试 (10/6)

發布時間:2024/1/8 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 华为机试 (10/6) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

24點游戲算法

問題描述:給出4個1-10的數字,通過加減乘除,得到數字為24就算勝利

import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; public class Main {public static int []a;public static int []b;public static void init(){a = new int[4];b = new int[4];}public static boolean dfs(int x){boolean ed = true;for(int i=0;i<4;++i){if(b[i] == 0){ed = false;break;}}if(ed){if(x == 24)return true;else return false;}for(int i=0;i<4;++i){if(b[i] == 0){b[i] = 1;if(dfs(x+a[i]))return true;if(dfs(x-a[i]))return true;if(dfs(x*a[i]))return true;if((x % a[i] == 0) && (dfs(x / a[i])))return true;b[i] = 0;}}return false;}public static void main(String[] args) throws IOException {BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));String str = "";init();while((str = bf.readLine()) != null && !"".equals(str)) {String[] nums = str.split(" ");for(int i=0;i<4;++i){a[i] = Integer.parseInt(nums[i]);b[i] = 0;}boolean flag = false;for(int i=0;i<4;++i){b[i] = 1;flag = dfs(a[i]);b[i] = 0;if(flag)break;}if(flag)System.out.println("true");else System.out.println("false");}} } #include <stdio.h> #include <algorithm> using namespace std; const int N=4; int num[N]; int isSolve=0; void dfs(int index,int currentNum,int arr[]) {if(currentNum==24){isSolve=1;return;}if(isSolve||currentNum>24||index>=4)return;for(int operFlag=0;operFlag<4;operFlag++){switch(operFlag){case 0:dfs(index+1,currentNum+arr[index],arr);break;case 1:dfs(index+1,currentNum-arr[index],arr);break;case 2:dfs(index+1,currentNum*arr[index],arr);break;case 3:dfs(index+1,currentNum/arr[index],arr);break;}if(isSolve)return;} } int main() {while(scanf("%d%d%d%d",&num[0],&num[1],&num[2],&num[3])>0){isSolve=0;sort(num,num+4);do{dfs(1,num[0],num);if(isSolve)break;} while (next_permutation(num,num+4));if(isSolve)printf("true\n");elseprintf("false\n");}return 0; }

矩陣乘法

如果A是個x行y列的矩陣,B是個y行z列的矩陣,把A和B相乘,其結果將是另一個x行z列的矩陣C。這個矩陣的每個元素是由下面的公式決定的

import java.io.*;public class Main {public static void main(String[] args) throws IOException {BufferedReader br = new BufferedReader(new InputStreamReader(System.in));String str = null;while ((str = br.readLine()) != null) {if (str.equals("")) continue;int x = Integer.parseInt(str);int y = Integer.parseInt(br.readLine());int z = Integer.parseInt(br.readLine());int[][] matrix1 = new int[x][y];int[][] matrix2 = new int[y][z];for (int i = 0; i < x; i++) {String[] params = br.readLine().split(" ");for (int j = 0; j < y; j++) {matrix1[i][j] = Integer.parseInt(params[j]);}}for (int i = 0; i < y; i++) {String[] params = br.readLine().split(" ");for (int j = 0; j < z; j++) {matrix2[i][j] = Integer.parseInt(params[j]);}}StringBuilder ans = new StringBuilder();for (int i = 0; i < x; i++) {for (int j = 0; j < z; j++) {int temp = 0;for (int k = 0; k < y; k++) temp += matrix1[i][k] * matrix2[k][j];ans.append(temp).append(" ");}ans.deleteCharAt(ans.length()-1).append("\n");}System.out.print(ans.toString());}} } #include <stdio.h> int main() {int x,y,z;while(scanf("%d",&x) != EOF){scanf("%d",&y);scanf("%d",&z);int array_0[x][y];int array_1[y][z];int result[x][z] = {};// 獲取第一個矩陣int i,j;for(i = 0; i < x; i++){for(j = 0;j < y;j++){scanf("%d", &array_0[i][j]);}}// 獲取第二個矩陣for(i = 0; i < y; i++){for(j = 0;j < z;j++){scanf("%d", &array_1[i][j]);}}// 計算結果int k;int temp;for(i = 0; i < x; i++){for(j = 0;j < z;j++){for(k = 0; k < y; k++){result[i][j] += (array_0[i][k]*array_1[k][j]);}}}// 打印for(i = 0; i < x; i++){for(j = 0;j < z;j++){printf("%d ", result[i][j]);}printf("\n");}}return 0; }

矩陣乘法計算量估算

矩陣乘法的運算量與矩陣乘法的順序強相關。
例如:
A是一個50×10的矩陣,B是10×20的矩陣,C是20×5的矩陣
計算ABC有兩種順序:((AB)C)或者(A(BC)),前者需要計算15000次乘法,后者只需要3500次。
編寫程序計算不同的計算順序需要進行的乘法次數

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import java.util.Stack; public class Main {public static void main(String[] args) throws IOException {BufferedReader br = new BufferedReader(new InputStreamReader(System.in));// Scanner sc = new Scanner(System.in);String str = null;while ((str = br.readLine()) != null) {int num = Integer.parseInt(str);int [][] arr = new int[num][2]; for(int i = 0;i<num;i++) {String [] sa = br.readLine().split(" ");arr[i][0] = Integer.parseInt(sa[0]);arr[i][1] = Integer.parseInt(sa[1]);} int n = arr.length -1;char [] ca = br.readLine(). toCharArray();Stack<Integer> stack = new Stack<>();int sum = 0;for(int i = ca.length - 1;i>=0;i--) {char one = ca[i];if(one == ')') {stack.push(-1);}else if(one == '(') {int n1 = stack.pop();int n2 = stack.pop();sum+= arr[n1][0]*arr[n2][0]*arr[n2][1];arr[n1][1] = arr[n2][1];stack.pop();stack.push(n1);}else {stack.push(n);n--;}}System.out.println(sum);}} } #include<stdio.h> int main(){int n;while(scanf("%d",&n)!=EOF){int a[100][2]={0};char b[100]={0},c[100]={0};int top=-1;int i,j,k;char *p;int m,s;int sum=0;k=n;for(i=0;i<n;i++)scanf("%d %d",&a[i][0],&a[i][1]);scanf("%s",b);p=b;while(*(p+1)!='\0'){if(top==-1||(*p>='A'&&*p<='Z')||(*p=='(')){top++;c[top]=*p;}else if(*p==')'){m=c[top]-'A';top--;s=c[top]-'A';top--;sum+=a[s][0]*a[s][1]*a[m][1];c[top]='A'+k;a[k][0]=a[s][0];a[k][1]=a[m][1];k++;}p++; }printf("%d\n",sum); }return 0; }

字符串通配符

問題描述:在計算機中,通配符一種特殊語法,廣泛應用于文件搜索、數據庫、正則表達式等領域?,F要求各位實現字符串通配符的算法。
要求:
實現如下2個通配符:
*:匹配0個或以上的字符(字符由英文字母和數字0-9組成,不區分大小寫。下同)
?:匹配1個字符

import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException;public class Main{public static void main(String[] args) throws IOException{BufferedReader br = new BufferedReader(new InputStreamReader(System.in));String line = "";while((line=br.readLine())!=null){String s2= br.readLine();System.out.println(help(s2, line));}}public static boolean help(String s, String p){int sr = 0;int pr = 0;int st = -1;int match = 0;while(sr < s.length()){if(pr < p.length() && (s.charAt(sr)==p.charAt(pr) || p.charAt(pr)=='?')){pr++;sr++;}else if(pr < p.length() && p.charAt(pr)=='*'){st = pr;match = sr;pr++;}else if(st != -1){pr = st + 1;sr = ++match;}else{return false;}}while(pr<p.length() && p.charAt(pr)=='*'){pr++;}return pr == p.length();} } #include<iostream> #include<string>using namespace std;int isMatch(const char* p1,const char *p2) {if(*p1=='\0'&&*p2=='\0')return 1;else if(*p1=='\0'||*p2=='\0')return 0;else if(*p1=='?')return isMatch(p1+1,p2+1);else if(*p1=='*')return isMatch(p1, p2+1)||isMatch(p1+1, p2)||isMatch(p1+1, p2+1);else if(*p1==*p2)return isMatch(p1+1, p2+1);elsereturn 0; } int main() {string str1,str2;while(cin>>str1>>str2)cout<<(isMatch(str1.c_str(), str2.c_str())==1?"true":"false")<<endl;return 0; }

超長正整數相加

請設計一個算法完成兩個超長正整數的加法

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader;public class Main {public static void main(String[] args) throws Exception {BufferedReader br = new BufferedReader(new InputStreamReader(System.in));String str1;while ((str1 = br.readLine()) != null) {String str2 = br.readLine();StringBuilder sb1 = new StringBuilder(str1);StringBuilder sb2 = new StringBuilder(str2);if (str1.length() > str2.length()) {for (int i = 0; i < str1.length() - str2.length(); i++) {sb2.insert(0, '0');}} else {for (int i = 0; i < str2.length() - str1.length(); i++) {sb1.insert(0, '0');}}StringBuilder sb = new StringBuilder();int temp = 0;for (int i = sb1.length() - 1; i >= 0; i--) {int m = sb1.charAt(i) - '0';int n = sb2.charAt(i) - '0';int sum = m + n + temp;sb.insert(0, sum % 10);temp = sum / 10;}if (temp != 0) {sb.insert(0, temp);}System.out.println(sb.toString());}} } #include<iostream> #include<stack> using namespace std;int main() {string addend,augend;while(cin>>addend>>augend){stack<long> res;long sum=0;long i=addend.size()-1,j=augend.size()-1;while(i>=0||j>=0){int add;if(i>=0)sum=sum+addend[i--]-'0';if(j>=0)sum=sum+augend[j--]-'0';if(sum/10!=0)add=sum%10;else add=sum;res.push(add);sum=sum/10;}if(sum>0)res.push(sum);while(res.size()!=0){cout<<res.top();res.pop();}cout<<endl;}return 0; }

總結

以上是生活随笔為你收集整理的华为机试 (10/6)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。