字符串分隔
連續輸入字符串,請按長度為8拆分每個字符串后輸出到新的字符串數組;
長度不是8整數倍的字符串請在后面補數字0,空字符串不處理。
輸入:
abc
123456789
輸出:
abc00000
12345678
90000000
1 #include<iostream> 2 #include<string> 3 4 using namespace std; 5 int main(){ 6 string str; 7 while(cin >> str) 8 { 9 string temp(8, '0'); 10 if(str.size()<= 8 ) 11 { 12 temp.replace(0, str.size(), str); 13 cout << temp << endl; 14 } 15 else{ 16 int loop = str.size()/8; 17 int remain = str.size()%8; 18 for(int i = 0; i < loop; i++) 19 { 20 temp.replace(0, 8, str, i*8, 8 ); 21 cout << temp << endl; 22 } 23 if(remain){ 24 temp = "00000000"; 25 temp.replace(0, remain, str, loop*8, remain); 26 cout << temp << endl; 27 } 28 } 29 } 30 return 0; 31 }
進制轉換
寫出一個程序,接受一個十六進制的數值字符串,輸出該數值的十進制字符串。(多組同時輸入 )
輸入一個十六進制的數值字符串。
輸出該數值的十進制字符串。
輸入:
0xA
輸出:
10
1 #include <iostream> 2 using namespace std; 3 4 int main() 5 { 6 int a; 7 while(cin >> hex >>a){ 8 cout << a << endl; 9 } 10 }
質數因子
功能:輸入一個正整數,按照從小到大的順序輸出它的所有質數的因子(如180的質數因子為2 2 3 3 5 )
1 #include <iostream> 2 using namespace std; 3 4 int main() 5 { 6 long a; 7 while(cin >> a) 8 { 9 while(a != 1) 10 { 11 for(long j = 2; j<= a; j++ ) 12 { 13 if(a%j == 0) 14 { 15 a /= j; 16 cout << j << ' '; 17 break; 18 } 19 } 20 } 21 } 22 return 0; 23 }
坐標移動
開發一個坐標計算工具, A表示向左移動,D表示向右移動,W表示向上移動,S表示向下移動。從(0,0)點開始移動,從輸入字符串里面讀取一些坐標,並將最終輸入結果輸出到輸出文件里面。
輸入:
合法坐標為A(或者D或者W或者S) + 數字(兩位以內)
坐標之間以;分隔。
非法坐標點需要進行丟棄。如AA10; A1A; $%$; YAD; 等。
下面是一個簡單的例子 如:
A10;S20;W10;D30;X;A1A;B10A11;;A10;
處理過程:
起點(0,0)
+ A10 = (-10,0)
+ S20 = (-10,-20)
+ W10 = (-10,-10)
+ D30 = (20,-10)
+ x = 無效
+ A1A = 無效
+ B10A11 = 無效
+ 一個空 不影響
+ A10 = (10,-10)
結果 (10, -10)
輸入:
一行字符串
輸出:
最終坐標,以,分隔
#include <iostream> #include <string> #include <vector> #include <algorithm> #include <sstream> using namespace std; int main() { string input; char separator = ';'; string valid = "0123456789"; int value = 0; while(getline(cin, input)) { vector<int> points(2, 0); size_t new_start = 0; size_t posSeparator = input.find(separator); while(posSeparator != string::npos) { string current_command = input.substr(new_start, posSeparator - new_start); new_start = posSeparator + 1; posSeparator = input.find(separator, new_start); if(current_command.size() <=1 || current_command.size() >3) continue; if(current_command[0] == 'A' ||current_command[0] == 'S'||current_command[0] == 'D'||current_command[0] == 'W' ) { string small = current_command.substr(1); if(small.find_first_not_of(valid) == string::npos ) { value = stoi(small, nullptr, 10); if(current_command[0] == 'A') points[0] -= value; else if(current_command[0] == 'D') points[0] += value; else if(current_command[0] == 'W') points[1] += value; else if(current_command[0] == 'S') points[1] -= value; } } } cout << points[0] << ',' << points[1] << endl; } return 0; }
汽水瓶換飲料
有這樣一道智力題:“某商店規定:三個空汽水瓶可以換一瓶汽水。小張手上有十個空汽水瓶,她最多可以換多少瓶汽水喝?”答案是5瓶,方法如下:先用9個空瓶子換3瓶汽水,喝掉3瓶滿的,喝完以后4個空瓶子,用3個再換一瓶,喝掉這瓶滿的,這時候剩2個空瓶子。然后你讓老板先借給你一瓶汽水,喝掉這瓶滿的,喝完以后用3個空瓶子換一瓶滿的還給老板。如果小張手上有n個空汽水瓶,最多可以換多少瓶汽水喝?
對於每組測試數據,輸出一行,表示最多可以喝的汽水瓶數。如果一瓶也喝不到,輸出0。
輸入:
3
10
81
0
輸出:
1
5
40
#include<iostream> using namespace std; int main() { int input; while(cin >> input) { int drink = 0; if(input == 0) break; if(input == 1) cout << drink << endl; int empty = input; int full = empty; int remain = 0; while(full){ full = empty / 3; drink += full; remain = empty % 3; empty = full + remain; } if(remain == 2) drink++; cout << drink << endl; } return 0; }
最小公倍數
輸入兩個正整數A和B
輸出A和B的最小公倍數
最小公倍數 = A x B / 最大公約數
#include<iostream> #include<algorithm> using namespace std; int gcd(int a, int b); int main(){ int a, b; int minMax; while(cin >> a >> b){ minMax = a*b/gcd(a,b); cout << minMax << endl; } } int gcd(int a, int b) { if(b == 0) return a; return gcd(b, a%b); }
求解立方根
輸入:double 待求解參數
返回值:double 輸入參數的立方根,保留一位小數
牛頓迭代法
#include<iostream> #include<iomanip> #include<algorithm> using namespace std; int main() { double a=0.0, t = 0.0; double e = 0.0001; while(cin >> a){ t = a; while(abs(t*t*t - a) > e){ t = t - ( t*t*t - a )* 1.0 / (3 * t*t); } cout << setiosflags(ios::fixed)<< setprecision(1) << t << endl; } return 0; }