華為2015機試題

2021-08-29 22:38:36 字數 3616 閱讀 4057

字串分解

按要求分解字串,輸入兩個數m,n;m代表輸入的m串字串,n代表輸出的每串字串的位數,不夠補0。例如:輸入2,8, 「abc」 ,「123456789」,則輸出為「abc00000」,「12345678「,」90000000」 

分析思路:

容易題 

1.獲得字串的長度length後,判斷與要輸出位數n

的大小,大於n的話,直接printf前n位字元

,然後length-=n; 

2.再次迴圈判斷與要輸出位數n的大小,如果小於n的話,輸出length個字元和n-length個0字元。

3.重複迴圈直到length<0。 

#include

using

namespace

std;

void

resolve(

char

*str

, intn)

strtemp[i] =

'\0'

;            

記得新增

\0   }

else

//小於

n,輸出

length

個字元和

n-length個0

字元 for

(i = length; i<

n; i++)

strtemp[i] =

'\0';

} cout << strtemp <<

" ";

length -= n;

} }int

main()

system(

"pause");

return0;

} 拼音轉數字

輸入是乙個只包含拼音的字串,請輸出對應的數字序列。轉換關係如下: 

描述: 拼音 yi er san si wu liu qi ba jiu 

阿拉伯數字 1 2 3 4 5 6 7 8 9 

輸入字元只包含小寫字母,所有字元都可以正好匹配

執行時間限制:無限制 

記憶體限制: 無限制 

輸入: 一行字串,長度小於1000 

輸出: 一行字元(數字)串 

樣例輸入: yiersansi 

樣例輸出: 1234 

#include

#include

using

namespace

std;

void

convert(

vector

<

char

>

input)

else

break;

case

'w':

cout <<

"5";

i += 2;

break;

case

'l':

cout <<

"6";

i += 3;

break;

case

'q':

cout <<

"7";

i += 2;

break;

case

'b':

cout <<

"8";

i += 2;

break;

case

'j':

cout <<

"9";

i += 3;

break;

} }

printf(

"\n");

} intmain()

convert(str);

system(

"pause");

return0;

} 去除重複字元並排序

執行時間限制:無限制 

內容限制: 無限制 

輸入: 字串 

輸出: 去除重複字元並排序的字串 

樣例輸入: aabcdefff 

樣例輸出: abcdef 

#include

#include

using

namespace

std;

void

fun(

char

*str)

map<

char

, int

>::iterator it;

for(it = m.begin(); it != m.end(); it++)

cout << endl; }

intmain()

return0;

} 等式轉換

輸入乙個正整數x,在下面的等式左邊的數字之間新增+號或者-號,使得等式成立。 

1 2 3 4 5 6 7 8 9 = x 

比如: 

12-34+5-67+89 = 5 

1+23+4-5+6-7-8-9 = 5 

請編寫程式,統計滿足輸入整數的所有整數個數。 

輸入: 正整數,等式右邊的數字 

輸出: 使該等式成立的個數 

樣例輸入:5 

樣例輸出:2

#include

#include

using

namespace

std;

const

char

sym[3] = ;

inttarget = 0, hitcount = 0, flag = 1;

char

opts[10]; /*

*  lastsum,curres

分別儲存上乙個符號之前的計算結果與當前的數字結果,

*     

如1+2+345

搜尋到4

的時候lastsum

儲存1+2+3

的結果,

curres

儲存34

*  flag

儲存正負號相關的計算因子 */

void

recursive_permutation(

intlastsum

, int

curres

, char

curopt

, int

curnum)

else

if(curnum

== 9)

cout << i; }

cout << endl; }

lastsum

= curres

= 0;

flag = 1; }

else

} }

intmain()

華為機試題 2015

第一題 60分 按要求分解字串,輸入兩個數m,n m代表輸入的m串字串,n代表輸出的每串字串的位數,不夠補0。例如 輸入2,8,abc 123456789 則輸出為 abc00000 12345678 90000000 第一題 60分 按要求分解字串,輸入兩個數m,n m代表輸入的m串字串,n代表輸...

2015華為機試題 洞穴逃生

下面是程式 可能有問題,但還沒發現。貪婪演算法 精靈王子.cpp 定義控制台應用程式的入口點。include stdafx.h includeusing namespace std int m,s,t 初始魔法值 據洞口距離 洞口塌陷時間 int mintime,maxdistance void d...

2015華為校招機試題

第一題 60分 按要求分解字串,輸入兩個數m,n m代表輸入的m串字串,n代表輸出的每串字串的位數,不夠補0。例如 輸入2,8,abc 123456789 則輸出為 abc00000 12345678 90000000 include include include 華為2015年第一道上機題 vo...