棋盘最大价值

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <map>
#include <set>
#include <unordered_map>
#include "limits.h"
#include "math.h"
using namespace std;
int main(int argc, const char * argv[]) {
int nrows,ncols;
string str;
cin >> str;
string word = "";
for(int i=0; i<str.length(); i++){
if( str[i] == ',' ){
nrows = stoi(word);
word = "";
}else{
word += str[i];
}
}
ncols = stoi(word);
int map[nrows][ncols];
for(int i=0; i<nrows; i++){
for(int j=0; j<ncols; j++){
cin >> map[i][j];
}
}
int dp[nrows][ncols];
int sum = 0;
for(int i=0; i<nrows; i++){
sum += map[i][0];
dp[i][0] = sum;
}
sum = 0;
for(int j=0; j<ncols; j++){
sum += map[0][j];
dp[0][j] = sum;
}
for(int i=1; i<nrows; i++){
for(int j=1; j<ncols; j++){
dp[i][j] = map[i][j] + max(dp[i-1][j], dp[i][j-1]);
}
}
cout << dp[nrows-1][ncols-1] << endl;
return 0;
}
判断排序算法

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <map>
#include <set>
#include <unordered_map>
#include "limits.h"
#include "math.h"
using namespace std;
int main(int argc, const char * argv[]) {
int n;
cin >> n;
string str;
cin >> str;
vector<int> nums;
string word = "";
for(int i=0; i<str.length(); i++){
if( str[i] == ',' ){
nums.push_back(stoi(word));
word = "";
}else{
word += str[i];
}
}
nums.push_back(stoi(word));
for(int i=0; i<nums.size()-1; i++){
int min = i;
for(int j=i+1; j<nums.size(); j++){
if( nums[j] < nums[min] ){
min = j;
}
}
swap(nums[i], nums[min]);
}
for(int i=0; i<n-1; i++){
cout << nums[i] << ",";
}
cout << nums[n-1] << endl;
return 0;
}
消除多余的下划线(不开辟新空间)

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <map>
#include <set>
#include <unordered_map>
#include "limits.h"
#include "math.h"
using namespace std;
int main(int argc, const char * argv[]) {
string str;
cin >> str;
int start;
for(start=0; start<str.length(); start++){
if( str[start] != '_' ){
break;
}
}
if( start == str.length() ){
cout << "" << endl;
return 0;
}
int end;
for( end=str.length()-1; end >= 0; end-- ){
if( str[end] != '_' ){
break;
}
}
if( start==end ){
cout << str[start] << endl;
return 0;
}
cout << str[start];
bool flag = true;
for(int i=start+1; i<end; i++){
if( str[i] != '_' ){
cout << str[i];
flag = true;
}else{
if( flag ){
cout << '_';
flag = false;
}
}
}
cout << str[end] << endl;
return 0;
}
分解质因数

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <map>
#include <set>
#include <unordered_map>
#include "limits.h"
#include "math.h"
using namespace std;
string result;
void factor(int n){
for(int i=2; i<=n; ++i){
if( !(n%i) ){
result += to_string(i);
result += "*";
factor( n/i );
break;
}
}
}
int main(int argc, const char * argv[]) {
int num;
cin >> num;
factor(num);
if( result[result.size()-1] == '*' ){
result.erase( result.end() - 1 );
}
cout << result << endl;
return 0;
}