合并排序(非递归)
#include <iostream>
using namespace std;
void Mergepass(int x[],int y[],int s,int n);
void Merge(int c[],int d[],int l,int m,int r){
int i=l,j=m+1,k=l;
while(i<=m&&j<=r){
if(c[i]>c[j])d[k++]=c[j++];
else d[k++]=c[i++];
}
while(i<=m){
d[k++]=c[i++];
}
while(j<=r){
d[k++]=c[j++];
}
}
void Mergesort(int a[],int n){
int b[n];
int s=1;
while(s<n){
Mergepass(a,b,s,n);
s+=s;
Mergepass(b,a,s,n);
s+=s;
}
}
void Mergepass(int x[],int y[],int s,int n){
int i=0;
while(i<=n-2*s){
Merge(x,y,i,i+s-1,i+2*s-1);
i=i+2*s;
}
if(i+s<n){
Merge(x,y,i,i+s-1,n-1);
}
else{
for(int j=i;j<=n-1;j++){
y[j]=x[j];
}
}
}
int main(){
int i,n;
int a[10000];
cin>>n;
for(i=0;i<n;i++){
cin>>a[i];
}
Mergesort(a,n);
for(i=0;i<n;i++){
cout<<a[i]<<endl;
}
}
快速排序(随机产生基准)
#include <iostream>
#include <stdlib.h>
using namespace std;
int a[10000];
int randompartion(int a[],int l,int r){
int temp,t;
temp=rand()%(r-l)+l;
t=a[temp];
a[temp]=a[l];
a[l]=t;
int x=a[l];
int i=l,j=r;
while(i<j){
while(a[j]>x&&i<j)j--;
a[i]=a[j];
while(a[i]<=x&&i<j)i++;
a[j]=a[i];
}
a[i]=x;
return i;
}
void quick(int a[],int l,int r){
if(l<r){
int q=randompartion(a,l,r);
quick(a,l,q-1);
quick(a,q+1,r);
}
}
int main(){
int i,j,n;
cin>>n;
for(i=0;i<n;i++){
cin>>a[i];
}
quick(a,0,n-1);
for(i=0;i<n;i++){
cout<<a[i]<<" ";
}
cout<<endl;
}
大数乘法
#include<iostream>
#include<string>
#include<algorithm>
#include<sstream>
using namespace std;
int strint(string s) {
int num;
stringstream ss(s);
ss >> num;
return num;
}
string intstr(int num) {
string s;
stringstream ss;
ss << num;
ss >> s;
return s;
}
void removePrezero(string &s) {
if (s.length() == 1)return;
int k = 0;
for (int i = 0; i < s.length(); i++) {
if (s[i] == '0')k++;
else break;
}
if (k == s.length())s = "0";
else s = s.substr(k);
}
string add(string s1, string s2) {
string s = "";
removePrezero(s1);
removePrezero(s2);
reverse(s1.begin(), s1.end());
reverse(s2.begin(), s2.end());
int c = 0;
for (int i = 0;c|| i < max(s1.length(), s2.length()); i++) {
int t = c;
if (i < s1.length())t += s1[i] - '0';
if (i < s2.length())t += s2[i] - '0';
int d = t % 10;
s.insert(0, intstr(d));
c = t / 10;
}
return s;
}
string subtra(string &s1, string &s2) {
string s = "";
string flag;
removePrezero(s1);
removePrezero(s2);
int len1 = s1.length();
int len2 = s2.length();
int len = len1>len2 ? len1 : len2;
if (len1 < len2)flag = "-";
else if (len1 > len2)flag = "+";
else {
int i;
for (i = 0; i < len1; i++) {
if (s1.at(i) > s2.at(i)) {
flag = "+"; break;
}
else if (s1.at(i) < s2.at(i)) {
flag = "-"; break;
}
}
if (i == len1)s == "0";
}
int* num = (int*)malloc(sizeof(int)*len);
reverse(s1.begin(), s1.end());
reverse(s2.begin(), s2.end());
int c = 0;
for (int j = 0; j < len; j++) {
int n1 = j < len1 ? s1[j] - '0' : 0;
int n2 = j < len2 ? s2[j] - '0' : 0;
if (flag == "+")num[c++] = n1 - n2;
else num[c++] = n2 - n1;
}
for (int j = 0; j < c; j++) {
if (num[j] < 0) {
num[j] += 10; num[j + 1] -= 1;
}
}
c--;
while (num[c] == 0)c--;
for (int j = 0; j <=c; j++){
s.insert(0, intstr(num[j]));
}
if (flag == "-")return flag + s;
else return s;
}
void addPrezero(string &s, int L) {
for (int i = 0; i < L; i++)
s = s.insert(0, "0");
}
string addLastzero(string s, int L) {
string s1 = s;
for (int i = 0; i < L; i++)
s1 += "0";
return s1;
}
string multi(string &s1, string &s2) {
bool flag1 = false, flag2 = false;
string sign;
if (s1.at(0) == '-') {
flag1 = true; s1 = s1.substr(1);
}
if (s1.at(0) == '-') {
flag2 = true; s2 = s2.substr(1);
}
if (flag1&&flag2)sign = "+";
else if (flag1 || flag2) sign = "-";
else sign = "+";
int L = 4;
if (s1.length() > 2 || s2.length() > 2) {
if (s1.length() >= s2.length()) {
while (L < s1.length())L *= 2;
if (L != s1.length())
addPrezero(s1, L - s1.length());
addPrezero(s2, L - s2.length());
}else {
while (L < s2.length())L*=2;
if (L != s2.length())
addPrezero(s2, L - s2.length());
addPrezero(s1, L - s1.length());
}
}
if (s1.length() == 1)addPrezero(s1, 1);
if (s2.length() == 1)addPrezero(s2, 1);
int n = s1.length();
string result, a0, a1, b0, b1;
if (n > 1) {
a1 = s1.substr(0, n / 2);
a0 = s1.substr(n / 2, n);
b1 = s2.substr(0, n / 2);
b0 = s2.substr(n / 2, n);
}
if (n == 2) {
int x1 = strint(a1);
int x2 = strint(a0);
int y1 = strint(b1);
int y2 = strint(b0);
int num = (x1 * 10 + x2)*(y1 * 10 + y2);
result = intstr(num);
}else {
string c2 = multi(a1, b1);
string c0 = multi(a0, b0);
string temp1 = add(a0, a1);
string temp2 = add(b1, b0);
string temp3 = add(c2, c0);
string temp_c1 = multi(temp1, temp2);
string c1 = subtra(temp_c1, temp3);
string s1 = addLastzero(c1, n / 2);
string s2 =addLastzero(c2, n);
result = add(add(s1, s2), c0);
}
if (sign == "-")result.insert(0, sign);
return result;
}
int main() {
string s1, s2;
cin>>s1;
cin>>s2;
cout << multi(s1, s2);
}
循环赛日程表
#include <iostream>
#include <algorithm>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
using namespace std;
int a[1000][1000];
int n;
void copy(int x1,int y1,int x2,int y2,int n){
int i,j;
for(i=1;i<=n;i++){
for(j=1;j<=n;j++){
a[x2+i][y2+j]=a[x1+i][y1+j];
}
}
}
void table(int x,int y,int n){
if(1==n)return ;
table(x,y,n/2);
table(x,y+n/2,n/2);
copy(x,y,x+n/2,y+n/2,n/2);
copy(x,y+n/2,x+n/2,y,n/2);
}
int main(){
int k;
int i,j;
cin>>k;
n=pow(2,k);
for(i=1;i<=n;i++){
a[1][i]=i;
}
for(i=1;i<=n;i++){
a[i][1]=i;
}
table(0,0,n);
for(i=1;i<=n;i++){
for(j=1;j<=n;j++){
cout<<a[i][j]<<" ";
}
cout<<endl;
}
}