1.插入排序
#include <iostream> using namespace std; int main() { //N数字<=10000,元素个数 //第二行各个元素值 //第三个表示插入的值 int *a,N; cin >>N; a = new int[N]; for(int i = 0;i < N;i++) cin >>a[i]; //do something int insertValue; cin >> insertValue; if(a[0] > insertValue) cout <<insertValue<<" "; for(int i = 0;i < N;i++) { cout <<a[i]<<" "; if(a[i] < insertValue && a[i + 1] > insertValue) cout <<insertValue<<" "; } if(a[N-1] < insertValue) cout <<insertValue<<" "; cout <<endl; system("pause"); return 0; }
2.统计字符
#include <iostream> #include <ctype.h> using namespace std; int main() { char c; int alpha = 0,number = 0,space = 0,other = 0; while((c = getchar()) != ' ') { if(isalpha(c)) alpha++; else if(isdigit(c)) number++; else if(c == 32) space++; else other++; } cout <<"letter="<<alpha<<endl; cout <<"space="<<space<<endl; cout <<"digit="<<number<<endl; cout <<"other="<<other<<endl; system("pause"); return 0; }
3.稀疏数组,数组的第一部分用于记录原数组的列数和行数及元素的使用个数,第二部分为原数组非零元素的行数、列数、元素值
#include <iostream> using namespace std; int main() { //set n*n array int row,col,s = 0; cin >>row>>col; int **a = new int*[row]; for(int i = 0;i < row;i++) a[i] = new int[col]; //set value for(int i = 0;i < row;i++) { for(int j = 0;j < col;j++) { cin >> a[i][j]; if(a[i][j] != 0) s++; } } cout <<row<<" "<<col<<" "<<s<<endl; //print for(int i = 0;i < row;i++) { for(int j = 0;j < col;j++) { if(a[i][j] != 0) cout <<i<<" "<<j<<" "<<a[i][j]<<endl; } } system("pause"); return 0; }
4.c++标识符和java标识符的转换
#include <iostream> #include <string.h> #include <ctype.h> using namespace std; int main() { char s1[100],s2[100]; char c; int r,t = 0; bool upper = false,line = false,firstA = false,isLineRepeat = false; while((c = getchar()) != ' ') { if(isupper(c)) upper = true; if(c == 95) line = true; if(islower(c) && t == 0) firstA = true; if(t > 0 && c == 95 && s1[t - 1] == 95) isLineRepeat = true; s1[t++] = c; } int s1_len = strlen(s1); if(firstA== true && line == false && upper == true) r = 1; else if(firstA == true && isLineRepeat == false && upper == false) r = 2; else { cout <<"error!"<<endl; system("pause"); return 0; } if(r == 1) { cout <<"java to c++:"<<endl; int j = 0; for(int i = 0;s1[i] != '