實(shí)現(xiàn)語言:C++ 題目簡單糖埋,沒有涉及太多的算法,就是一些輸入輸出的格式需要注意
題目一
描述
根據(jù)指定的分隔符分隔字符串咬像,并輸出指定的段沮峡。如果指定的段超過分隔的段數(shù)疚脐,輸出:NULL
舉例:
AAA?BBB?CCC??2
字符串為:AAA?BBB?CCC?
分隔符為:?
指定的段為:2
字符串分割為:AAA BBB CCC共三段邢疙,第2段字符串為:BBB
輸入輸出格式要求
輸入分隔字符串長度小于128個字符棍弄,指定的段是一個正整數(shù)。
樣例
輸入:AAA?BBB?CCC??2
輸出:BBB
實(shí)現(xiàn)代碼如下:
#include <iostream>
#include <string.h>
#include <vector>
#include <typeinfo>
#include <cctype>
using namespace std;
int getNum(string inpus)
{
int objNum = 0,t = 1;
for (long i = inpus.length() - 1; i >= 0 ; -- i) {
if (isdigit(inpus[i]) && inpus[i] != '?') {
objNum += (inpus[i]- '0') * t;
t *= 10;
} else {
break;
}
}
return objNum;
}
int main(int argc, const char * argv[]) {
string inpus;
cin >> inpus;
vector<string> objStringInfo;
string a = "";
for (int i = 0; i < inpus.length(); ++i) {
if (inpus[i] == '?' || i == inpus.length()) {
objStringInfo.push_back(a);
a = "";
}
else {
a += inpus[i];
}
}
cout << objStringInfo[getNum(inpus) - 1] << endl;
return 0;
}
運(yùn)行結(jié)果:
題目一.png
題目二
輸入一組大于0小于1000的整數(shù)疟游,且均不相同呼畸,逗號隔開,輸出其中能被這些整數(shù)中其他元素整除的那些元素乡摹。
輸入輸出格式要求
輸入要求同上述描述役耕,輸出要求整數(shù)順序按照輸入時的順序輸出采转。
樣例
輸入:2,4,6,8,10,12,3,9
輸出:4,6,8,10,12,9
代碼如下:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int getStringNum(string inpus)
{
int objNum = 0,t = 1;
for (long i = inpus.length() - 1; i >= 0 ; -- i) {
if (isdigit(inpus[i]) && inpus[i] != '?') {
objNum += (inpus[i]- '0') * t;
t *= 10;
} else {
break;
}
}
return objNum;
}
vector<int> getNum(string inputs)
{
vector<string> v;
vector<int> num;
string buff = "";
for (int i = 0;i <= inputs.length(); ++i) {
if (i == inputs.length() - 1) {
buff = inputs[inputs.length() - 1];
v.push_back(buff);
buff = "";
}
if (inputs[i] == ',') {
v.push_back(buff);
buff = "";
} else{
buff += inputs[i];
}
}
for (int i = 0;i < v.size(); ++ i) {
num.push_back(getStringNum(v[i]));
}
return num;
}
int main(int argc, const char * argv[]) {
string s;
cin >> s;
vector<int> array = getNum(s);
int k = 0,result[100] = {0};
for (int i = 0;i < array.size(); ++i) {
for (int j = 0;j < array.size(); ++j) {
if (array[i] % array[j] == 0 && array[i] != array[j])
{
result[k] = array[i];
k ++;
break;
}
}
}
for (int i = 0;i < k; ++i)
{
if (i != k-1)
cout << result[i] << ",";
else
cout << result[i] << endl;
}
return 0;
}
題目三
描述:大數(shù)求余
輸入兩個超長整型構(gòu)成的字符串聪廉,使用空格隔開,求前者除以后者的余數(shù)故慈。
輸入輸出格式要求
輸入的每個字符串最大長度為100個字符板熊,輸出為余數(shù)值,如果結(jié)果異常輸出NULL
樣例
輸入:123456789 23456789
輸出:6172844
主要涉及大數(shù)問題察绷,A對B取模干签,即除法運(yùn)算,可以利用高精度減法解決,前期想的時候細(xì)節(jié)上可能會有問題拆撼,需要注意減法過程中對符號的處理
代碼如下:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
string subtraction(string a,string b)//大數(shù)減法
{
int arr1[100] = {0},arr2[100] = {0},arr3[100] = {0};
for (long i = a.length() - 1,k = 0;i >= 0; --i) {
arr1[k++] = a[i] - '0';
}
for (long i = b.length() - 1,k = 0;i >= 0; --i) {
arr2[k++] = b[i] - '0';
}
long len = (a.length() > b.length()) ? a.length():b.length();
int k = 0;
for (int i = 0;i < len; ++i) {
arr3[k++] = arr1[i] - arr2[i];
}
for (int i = 0;i < k; ++i) {
if (arr3[k - 1] < 0) {
arr3[k] = '-';
arr3[i] = abs(arr3[i]);
} else {
arr3[i] = abs(arr3[i]);
}
}
string result = "",buff = "";
while (arr3[k] == 0) {
k--;
}
for (int i = k; i >= 0; --i) {
buff = arr3[i] + '0';
result += buff;
}
return result;
}
string aModeB(string a,string b)//大數(shù)取模
{
if (a.length() > b.length()) {
while (a.compare(b) > 0) {
a = subtraction(a, b);
}
return a;
}
else if (a.length() == b.length())
{
if(a.compare(b) > 0)
{
while (a.compare(b) > 0) {
a = subtraction(a, b);
}
return a;
}
else
return a;
}
else
return a;
}
int main(int argc, const char * argv[]) {
string a,b;
cin >> a;
cin >> b;
cout << aModeB(a, b) << endl;
return 0;
}