2017.3.1
#include<iostream>
#include<algorithm rel='nofollow' onclick='return false;'>
using namespace std;
int main()
{
int a[1000],n,k;//蛋糕的重量,序号,以及判断条件k
int count=0;
int weight=0;
cin>>n>>k;
for(int i=0;i<n;i++)
{
cin>>a[i];
weight+=a[i];
if(weight>=k)//当当前重量大于k的时候,将重量置零,人数+1
{
weight=0;
count++;
}
}
if(weight>0) count++;//最后一个人没有分到k但是也分到了蛋糕
cout<<count<<endl;
return 0;
}
2017.3.2
#include<iostream>
#include<algorithm rel='nofollow' onclick='return false;'>
using namespace std;
int main()
{
int n,count,i,j,s,t,k;
int a[100][2]={0};
cin>>n>>count;
int *p;
p=(int*)malloc(sizeof(int)*n);
for(i=0;i<n;i++)
p[i]=i+1;
for(i=0;i<count;i++)
cin>>a[i][0]>>a[i][1];
for(i=0;i<count;i++)
{
j=0;
while(a[i][0]!=p[j])
{
j++;
}
if(a[i][1]<0)
{
s=-a[i][1];
t=p[j];
for(k=j;k>j-s;k--)
p[k]=p[k-1];
p[j-s]=t;
}
else if(a[i][1]>0)
{
s=a[i][1];
t=p[j];
for(k=j;k<j+s;k++)
p[k]=p[k+1];
p[j+s]=t;
}
}
for(i=0;i<n;i++)
cout<<p[i]<<' ';
cout<<endl;
return 0;
}
2017.3.3
#include <bits/stdc++.h>
using namespace std;
const int MAX=105;
string strs[MAX];
void output(string str)
{
int len=str.length();
for(int i=0;i<len;i++)
{
if(str[i]=='_')
{
int r=str.find('_',i+1);
string tmp="<em>"+str.substr(i+1,r-i-1)+"</em>";
output(tmp);
i=r;
}
else if(str[i]=='[')
{
int r=str.find(']',i+1);
string text=str.substr(i+1,r-i-1);
int l=str.find('(',r+1);
int linkr=str.find(')',r+1);
string link=str.substr(l+1,linkr-l-1);
string tmp="<a href=\""+link+"\" rel='nofollow' onclick='return false;'>"+text+"</a>";
output(tmp);
i=linkr;
}
else
printf("%c",str[i]);
}
}
void solve_h()
{
int len=strs[0].length();
int pos=len,cnt=0;
for(int i=0;i<len;i++)
{
if(strs[0][i]=='#')
cnt++;
if(strs[0][i]!='#'&&strs[0][i]!=' ')
{
pos=i;
break;
}
}
cout<<"<h"<<cnt<<">";
output(strs[0].substr(pos));
cout<<"</h"<<cnt<<">"<<endl;
}
void solve_p(int cnt)
{
cout<<"<p>";
for(int i=0;i<cnt;i++)
{
output(strs[i]);
if(i==cnt-1)
cout<<"</p>";
cout<<endl;
}
}
void solve_l(int cnt)
{
cout<<"<ul>"<<endl;
for(int i=0;i<cnt;i++)
{
int len=strs[i].length();
int pos=len,cnt=0;
for(int j=1;j<len;j++)
if(strs[i][j]!=' ')
{
pos=j;
break;
}
cout<<"<li>";
output(strs[i].substr(pos));
cout<<"</li>"<<endl;
}
cout<<"</ul>"<<endl;
}
int main()
{
//freopen("in.txt","r",stdin);
std::string line;
int cnt=0;
while(getline(std::cin,line))
{
if(line=="")
{
if(cnt==0)
continue;
if(strs[0][0]=='#')
solve_h();
else if(strs[0][0]=='*')
solve_l(cnt);
else
solve_p(cnt);
cnt=0;
continue;
}
strs[cnt++]=line;
}
if(cnt)
{
if(strs[0][0]=='#')
solve_h();
else if(strs[0][0]=='*')
solve_l(cnt);
else
solve_p(cnt);
}
return 0;
}
2017.3
(30分)
#include<iostream>
#include<algorithm rel='nofollow' onclick='return false;'>
#include<string>
#include<cstring>
#include<cstdio>
#include<vector>
using namespace std;
int main()
{
int flag=0;
string strs[100];
std::string line;
int i=0,j,k=0;
int cnt=0;
int count=0;
//cin>>count;
//cin.ignore();
while(getline(std::cin,strs[count++]));
for(i=0;i<count;i++)
{
if(strs[i][0]=='#')
{
int k=0;
int cnt=0;
while(strs[i][k++]=='#')
cnt++;
while(strs[i][k]==' ')
k++;
strs[i].erase(0,k);
cout<<"<h"<<cnt<<">"<<strs[i]<<"</h"<<cnt<<">"<<endl;
}
else if(strs[i][0]=='*')
{
cout<<"<ul>"<<endl;
while(strs[i][0]=='*')
{
cout<<"<li>";
int u=1;
while(strs[i][u]==' ')
u++;
strs[i].erase(0,u);
cout<<strs[i]<<"</li>"<<endl;
i++;
}
cout<<"</ul>"<<endl;
}
else if((((strs[i][0]<='z'&&strs[i][0]>='a')||(strs[i][0]<='Z'&&strs[i][0]>='A'))&&((strs[i+1][0]<='z'&&strs[i+1][0]>='a')||(strs[i+1][0]<='Z'&&strs[i+1][0]>='A'))))
{
cout<<"<p>"<<strs[i];
i++;
while((strs[i][0]<='z'&&strs[i][0]>='a')||(strs[i][0]<='Z'&&strs[i][0]>='A'))
{
cout<<endl;
cout<<strs[i];
i++;
}
cout<<"</p>"<<endl;
}
else if((strs[i][0]<='z'&&strs[i][0]>='a')||(strs[i][0]<='Z'&&strs[i][0]>='A'))
{
flag=0;
for(int w=0;w<strs[i].length();w++)
{
if(strs[i][w]=='_')
{
flag=1;
break;
}
else if(strs[i][w]=='[')
{
flag=2;
break;
}
}
if(flag==0)
cout<<"<p>"<<strs[i]<<"</p>"<<endl;
else if(flag==1)
{
int pos1=strs[i].find('_',0);
string s;
s=strs[i].substr(0,pos1);
cout<<"<p>"<<s<<"<em>";
int pos2=strs[i].find('_',pos1+1);
string s1=strs[i].substr(pos1+1,pos2);
cout<<s1<<"</em>."<<"</p>"<<endl;
}
else if(flag==2)
{
int pos3=strs[i].find('[',0);
int pos4=strs[i].find(']',pos3+1);
int pos5=strs[i].find(')',pos4+1);
string s2;
s2=strs[i].substr(0,pos3);
string s3;
string s4(1,'"');
s3=strs[i].substr(pos4+2,pos5-pos4-2);
cout<<"<p>";
cout<<s2;
cout<<"<a href=";
cout<<s4;
cout<<s3;
cout<<s4;
cout<<" rel='nofollow' onclick='return false;'>link</a>.</p>"<<endl;
}
}
}
}
冒泡/选择/插入/快速/归并 排序
#include<stdio.h>
#include<stdlib.h>
//冒泡排序:每次挑选最大的一个放到最后的位置
/*void BubbleSort(int* a,int n)
{
int i,j,t;
for(i=0;i<n-1;i++)
for(j=0;j<n-i-1;j++)
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
printf("%d\n",n);
}*/
//选择排序:每次挑选最小的一个放到未排序的最前面的位置
/*void SelectSort(int* a,int n)
{
int i,j,t1,t2;
for(i=0;i<n-1;i++)
{
t1=a[i];
for(j=i+1;j<n;j++)
if(a[j]<t1)
{
t2=t1;
t1=a[j];
a[j]=t2;
}
a[i]=t1;
}
}*/
/*void InsertSort(int* a,int n)
{
int i,j,t;
for(i=1;i<n;i++)
{
if(a[i]<a[i-1])
{
t=a[i];
j=i;
while(j rel='nofollow' onclick='return false;'>0&&a[j-1]>t)
{
a[j]=a[j-1];
j--;
}
a[j]=t;
}
}
}*/
/*void QuickSort(int* a, int left,int right)
{
int i=left;
int j=right;
int x=a[left];
if(left<right)
{
while(i<j&&a[j]>=x)
{
j--;
}
if(i<j)
{
a[i++]=a[j];
}
while(i<j&&a[i]<x)
{
i++;
}
if(i<j)
{
a[j--]=a[i];
}
a[i]=x;
QuickSort(a,left,i-1);
QuickSort(a,i+1,right);
}
}*/
/* int main()
{
int i;
int* a;
int n;
scanf("%d",&n);
a=(int*)malloc(sizeof(int)*n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
/*BubbleSort(a,n);*/
/*SelectSort(a,n);*/
/*InsertSort(a,n);*/
/*QuickSort(a,0,n-1);*/
/*for(i=0;i<n;i++)
printf("%d ",a[i]);
return 0;
}*/
//归并排序
void MergeArray(int* a,int first,int mid,int last,int* temp)
{
int i=first,j=mid+1;
int m=mid,n=last;
int k=0;
while(i<=mid&&j<=last)
{
if(a[i]<a[j])
temp[k++]=a[i++];
else
temp[k++]=a[j++];
}
while(i<=m)
temp[k++]=a[i++];
while(j<=n)
temp[k++]=a[j++];