跳一跳
#include <iostream>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int a[1000000]={0};
int main(int argc, char** argv) {
int s,tag=0,sum=0;
do
{
cin>>s;
if(s==1)
{
sum+=1;
tag=0;
}
else if(s==2)
{
sum+=tag*2+2;
tag++;
}
else if(s==0)
break;
//cout<<sum;
}while(1);//为什么while(s==0第一次循环就退出?)
cout<<sum;
return 0;
}
碰撞的小球
拿到题思路比较乱时,可以用样例画图,一步步走着看看
疑点:
例如,last中位置9向右,位置10向左不会算作碰撞;last中位置8向右,位置10向左,下一秒两者位置都在9,会算作碰撞。
但是满分通过了
#include <iostream>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
struct group{
int last;
int now;
int direct;
}a[100];
int main(int argc, char** argv) {
int n,l,t,i,j,k;//right=1,left=-1
cin>>n>>l>>t;
for(i=0;i<n;i++)
{
cin>>a[i].last;
a[i].direct=1;
}
for(k=0;k<t;k++){//移动次数
for(i=0;i<n;i++)//两球相撞换方向
{
for(j=0;j<n;j++)
{
if(i!=j&&a[i].last==a[j].last){
a[i].direct=-a[i].direct;
//cout<<"两球相撞换方向"<<endl;
break;
}
}
}
for(j=0;j<n;j++)
{
if(a[j].last==l||a[j].last==0)//撞两头换方向
{
a[j].direct=-a[j].direct;
}
if(a[j].direct==1)//依次更新位置
{
a[j].now=a[j].last+1;
}
else if(a[j].direct==-1)
{
a[j].now=a[j].last-1;
}
}
for(i=0;i<n;i++)//更新小球位置
{
a[i].last=a[i].now;
}
}
for(i=0;i<n;i++)
{
cout<<a[i].last<<" ";
}
return 0;
}
最小差值
各个数与其他数的最小差 应该可以简化
#include <iostream>
#include<math.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
struct group{
int num;
int mincha;
}a[1000];
int main(int argc, char** argv) {
int n,i,k,cha,min=10000;
cin>>n;
for(i=0;i<n;i++)
{
cin>>a[i].num;
a[i].mincha=10000;
}
for(k=0;k<n;k++)//各个数与其他数的最小差
{
for(i=0;i<n;i++)
{
cha=abs(a[k].num-a[i].num);
if(k!=i&&cha<a[k].mincha)
a[k].mincha=cha;
}
//cout<<a[k].mincha<<" ";
}
for(i=0;i<n;i++)//求最最小差
{
if(min>a[i].mincha)
min=a[i].mincha;
}
cout<<min;
return 0;
}
游戏
环形链表
start指针从尾部开始
1 下一个节点淘汰:检查下一个的下一个节点
2 下一个节点没被淘汰
错误示例:
list a[1000];
for(i=0;i<n;i++)
a[i]=(list)malloc(sizeof(group));
*不能先申明数组指针结构体,后依次申请空间 ,指向next无法a[1]->a[2]->a[3]->…
答案:
#include <iostream>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
typedef struct group{
int num;
struct group* next;
};
typedef struct group* list;
int main(int argc, char** argv) {
int n,k,i,count=1;
cin>>n>>k;
//不能先申明数组指针结构体,后依次申请空间
list head,p1,p2,start;
p1=(list)malloc(sizeof(group)); //空间1
head=p1;
for(i=1;i<n;i++)
{
p2=(list)malloc(sizeof(group));//空间2-n
p1->next=p2;
p1->num=i;
p1=p1->next;
}
p1->next=head;//尾头相连
p1->num=n;
start=p1;//即第n个数
while(start->next!=start)
{
while(start->next!=start&&(count%k==0||count%10==k))//检查下个节点/个位数是否被淘汰
{
start->next=start->next->next;
count++;
}
if(start->next==start)
break;
count++;
start=start->next;
}
cout<<start->num;
return 0;
}
打酱油
全买五瓶->全买三瓶->单买
#include <iostream>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int main(int argc, char** argv) {
int n,p,all=0;
cin>>n;
p=n/10;
if(p>=5)
{
all+=p/5*7;
p=p%5;//p/5 自动舍去余数,再乘5 = p%5
}
if(p>=3)
{
all+=p/3*4;
p=p%3;
}
all+=p;
cout<<all;
return 0;
}
公共钥匙盒
暴力解法,应该有更简便的方法
给盒子、记录输入、时间(每一秒钟)开辟数组空间
盒子[1000]:空-0 (NULL不要给int型,会警告) 不空-盒中的钥匙的编号
记录[1000]:钥匙编号 开始上课时间 上课时长
时间[10100]:入盒链表头 出盒链表头 (struct group*类型)
#include <iostream>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
typedef struct group* listnode;
struct group{
int a;
listnode next;
};
struct Record{
int w;//钥匙编号
int s;//开始上课时间
int c;//上课时长
}record[1000];
struct Time{//某一秒入盒、出盒的钥匙的编号
listnode in;//一个时间点有多个入盒子怎么办? 链表? in为链表的头结点指针
listnode out;
}time[10100]={NULL,NULL};
void insert_by_order( group *h, group *p )//h-头结点 p-要插入的结点
{
group *q;
q=h;
while(q->next!=NULL&&q->next->a<p->a)
{
q=q->next;
}
p->next = q->next;
q->next = p;
}
void print(int n)//time[n]
{
cout<<"时间"<<n<<":";//<<"入盒:";
group *q=time[n].in;
//cout<<"q:"<<q->a<<" q->next:"<<q->next<<endl;
if(q!=NULL){
cout<<"入盒:";
while(q->next!=NULL)
{
cout<<q->next->a<<" ";
q=q->next;
}
}
q=time[n].out;
if(q!=NULL){
cout<<"出盒:";
while(q->next!=NULL)
{
cout<<q->next->a<<" ";
q=q->next;
}
}
cout<<endl;
}
int main(int argc, char** argv) {
int n,k,w,s,c;//n-钥匙数 k-拿几次
int box[1000];//n>=1可能不止1000个 用结构体?
int i,j,maxt=0;//maxt-最后一个钥匙归还的时间
cin>>n>>k;
for(i=1;i<=n;i++)//初始化盒子, 1 2...
{
box[i]=i;
}
listnode p1,p2,q1,q2;
for(i=0;i<k;i++)
{
cin>>record[i].w>>record[i].s>>record[i].c;
//cout<<record[i].w<<record[i].s<<record[i].c;
if(maxt<record[i].s+record[i].c)
{
maxt=record[i].s+record[i].c;
}
//cout<<maxt;
//开辟空间,记录时间s进入盒子的钥匙的编号 记录时间s+c出盒子的钥匙的编号
if(time[record[i].s+record[i].c].in==NULL&&time[record[i].s].out==NULL)
{
//cout<<""
p2=(listnode)malloc(sizeof(group));//开辟新的 入盒 结点空间
p2->next=NULL;
p2->a=0;
time[record[i].s+record[i].c].in=p1=p2; //给头结点定位
q2=(listnode)malloc(sizeof(group)); //开辟新的 出盒 结点空间
q2->next=NULL;
q2->a=0;
time[record[i].s].out=q1=q2;
}
else if(time[record[i].s+record[i].c].in==NULL&&time[record[i].s].out!=NULL)
{
p2=(listnode)malloc(sizeof(group));//开辟新的 入盒 结点空间
p2->next=NULL;
p2->a=0;
time[record[i].s+record[i].c].in=p1=p2; //给头结点定位
}
else if(time[record[i].s+record[i].c].in!=NULL&&time[record[i].s].out==NULL)
{
q2=(listnode)malloc(sizeof(group));//开辟新的 入盒 结点空间
q2->next=NULL;
q2->a=0;
time[record[i].s].out=q1=q2; //给头结点定位
}
//第一个为空结点,第二个开始有值
p2=(listnode)malloc(sizeof(group));
p2->a=record[i].w;
p2->next=NULL;
//按从小到大的顺序插入
insert_by_order(time[record[i].s+record[i].c].in, p2);
/*p1->next=p2;
p1=p1->next;*/
q2=(listnode)malloc(sizeof(group));
q2->a=record[i].w;
q2->next=NULL;
//按从小到大的顺序插入
insert_by_order( time[record[i].s].out, q2);
//print(record[i].s);
//print(record[i].s+record[i].c);
//cout<<"jin";
/*q1->next=q2;
q1=q1->next;*/
}
//检查链表插入
for(i=0;i<=maxt;i++)//每秒发生的变化
{
//cout<<maxt<<endl;
//cout<<"时间"<<i<<endl;
listnode t,d;
t=time[i].in;
d=time[i].out;
//cout<<"jin1";
if(t!=NULL){
while(t->next!=NULL)//未提及标点的时间需要初始化?
{
//cout<<"有入盒 编号:"<<t->next->a<<" "<<endl;
for(j=1;j<=n;j++)//将编号为time[i].in的钥匙放入第一个空盒子 但是怎样使编号从小到大
{
if(box[j]==0)
{
box[j]=t->next->a;
//cout<<"入盒子"<<box[j]<<endl;
t=t->next;
break;
}
}
}
}
if(d!=NULL){
while(d->next!=NULL)//将编号为time[i].out的钥匙所对应盒子置空
{
//cout<<"有出盒 编号:"<<d->next->a<<" "<<endl;
for(j=1;j<=n;j++)//"="不能少 最后一个盒子
{
if(box[j]==d->next->a)
{
//cout<<"出盒子"<<box[j]<<endl;
box[j]=0;
d=d->next;
break;
}
}
}
}
//cout<<"jin2"<<endl;
/*for(j=1;j<=n;j++)
{
cout<<box[j]<<" ";
}
cout<<endl;*/
}
for(j=1;j<=n;j++)
{
cout<<box[j]<<" ";
}
return 0;
}
分蛋糕
此题很简单,但是第一次把k写成9,提交前要多编一些测试样例
#include <iostream>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int main(int argc, char** argv) {
int n,k,i,a,sum=0,kg=0;
cin>>n>>k;
for(i=0;i<n;i++)
{
cin>>a;
kg+=a;
if(kg>=k)
{
++sum;
kg=0;
}
}
if(kg!=0)
++sum;
cout<<sum;
return 0;
}
学生排队
正式的数据从第二个结点开始记录,这样不用考虑头结点从更替
#include <iostream>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
typedef struct group* listnode;
struct group{
int a;
listnode next;
};
int main(int argc, char** argv) {
int m,n,i,j;
int num,move;
cin>>n>>m;
//初始化队列
listnode p1,p2,head;
p2=(listnode)malloc(sizeof(group));
p2->a=0;
p2->next=NULL;
head=p1=p2;
for(i=1;i<=n;i++)
{
p2=(listnode)malloc(sizeof(group));
p2->a=i;
p2->next=NULL;
p1->next=p2;
p1=p1->next;
}
listnode s=head;
for(j=0;j<n;j++)
{
//cout<<s->next->a<<" ";
s=s->next;
}
//cout<<endl;
listnode p,q,node;
//指令
int count;//1 2 ... 3号 count=3
for(i=0;i<m;i++)
{
count=1;
s=p=q=head;
cin>>num>>move;
if(move!=0){
while(p->next->a!=num)//p:0 1 2 3...
{
//cout<<"jin1"<<endl;
++count;
p=p->next;
}
node=p->next;//要移动的结点
p->next=p->next->next;
count=(count+move)-1;//移动到第几个结点后面
for(j=0;j<count;j++)
{
q=q->next;
}
node->next=q->next;
q->next=node;
}
/*for(j=0;j<n;j++)
{
cout<<s->next->a<<" ";
s=s->next;
}*/
//cout<<endl;
}
for(j=0;j<n;j++)
{
cout<<s->next->a<<" ";
s=s->next;
}
return 0;
}
中间数
for(i=0;i<n;i++)
{
cin>>a[i];
}
for(i=0;i<n;i++)
{
bigger=0,smaller=0;
for(j=0;j<n;j++)
{
if(a[j]>a[i])
++bigger;
else if(a[j]<a[i])
++smaller;
}
if(bigger==smaller)
{
cout<<a[i];
return 0;
}
}
cout<<-1;
return 0;
工资计算
3600-100000 每间隔100,计算其应交的税,直到找到 工资-税=输入的税后工资
**计算税:**从多到少依次判断其应缴税部分金额大小是否符合档次;若符合,计算(>80000->0)
#include <iostream>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int main(int argc, char** argv) {
int i,T,S,tax=0,m,k;//T-税后所得 S-税前工资 tax-税 m-总共还剩下的要交税的钱 k-此部分要交税的钱
cin>>T;
if(T<=3500)
{
cout<<T;
return 0;
}
for(i=3600;i<100000;i=i+100)
{
//cout<<"税前:"<<i<<endl;
tax=0;
m=i-3500;
if(m>80000)
{
//cout<<"jin1"<<endl;
k=m-80000;
tax+=0.45*k;
if(tax+T==i)
{
cout<<i;
return 0;
}
m=m-k;
}
if(m>55000)
{
//cout<<"jin2"<<endl;
k=m-55000;
tax+=0.35*k;
if(tax+T==i)
{
cout<<i;
return 0;
}
m=m-k;
}
if(m>35000)
{
//cout<<"jin3"<<endl;
k=m-35000;
tax+=0.3*k;
if(tax+T==i)
{
cout<<i;
return 0;
}
m=m-k;
}
if(m>9000)
{
//cout<<"jin4"<<endl;
k=m-9000;
tax+=0.25*k;
if(tax+T==i)
{
cout<<i;
return 0;
}
m=m-k;
}
if(m>4500)
{
//cout<<"jin5"<<endl;
k=m-4500;
//cout<<"此部分要交税的钱数"<<k<<endl;
tax+=0.2*k;
if(tax+T==i)
{
cout<<i;
return 0;
}
m=m-k;
}
if(m>1500)
{
//cout<<"jin6"<<endl;
k=m-1500;
//cout<<"此部分要交税的钱数"<<k<<endl;
tax+=0.1*k;
if(tax+T==i)
{
cout<<i;
return 0;
}
m=m-k;
}
if(m>0)
{
//cout<<"jin7"<<endl;
k=m-0;
//cout<<"此部分要交税的钱数"<<k<<endl;
tax+=0.03*k;
if(tax+T==i)
{
cout<<i;
return 0;
}
m=0;
}
//cout<<"tax:"<<tax<<endl;
}
return 0;
}
最大波动
#include <iostream>
#include <math.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int main(int argc, char** argv) {
int n,a,b,max=0,i;
cin>>n;
cin>>a;
for(i=0;i<n-1;i++)
{
cin>>b;
if(abs(a-b)>max)
max=abs(a-b);
a=b;
}
cout<<max;
return 0;
}
火车购票
遍历两边,第一遍找一排连续的空位置;若无,第二遍遍历安排入散位
#include <iostream>
#include <math.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int main(int argc, char** argv) {
int n,a[100]={0},i,j,t,count,k;//t-第i人购票数 count-连续座位数计数器
int start=0;//连续座位开始编号
int twice,flag;
//0 1 2 3 4/.../95..99
cin>>n;
for(i=0;i<n;i++)
{
cin>>t;
twice=0;//是否第二次遍历,找散位
for(j=0;j<100&&count!=t;)
{
//cout<<"jin0 j="<<j<<endl;
if(twice==0) {
//cout<<"jin1"<<endl;
flag=0;
while(a[j]==0&&count!=t)//此处出错过 只是连续一排的start位置 j%5<=5-t,若将条件加在此处,其后位置也要遍历
{
//cout<<"jin11"<<endl;
if(flag==0&&j%5>5-t)
break;
if(flag==0)
{
start=j+1;
flag=1;
}
j++;
count++;
}
if(count==t)
{
for(k=start;k<start+t;k++){
cout<<k<<" ";
a[k-1]=1;//空->满
}
cout<<endl;
break;
}
count=0;
start=0;
++j;
if(j==100&&twice==0)//第一遍遍历未找到一排连续的位置
{
twice=1;
j=0;
}
}
else if(twice==1)
{
//cout<<"jin2"<<endl;
while(a[j]!=0)//遍历到一个空座位
{
++j;
}
//cout<<j+1<<" ";
a[j]=1;
++count;
++j;
if(count==t){
cout<<endl;
break;
}
}
}
}
return 0;
}
折点计数
前一个点比当前结点 大-(1) 小-(-1)
第一个,最后一个点不可能是折点
#include <iostream>
#include <math.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int main(int argc, char** argv) {
int a,b,n,i,next1,next2,count=0;//next-1 比折点大 next--1 比折点小
cin>>n;
cin>>a>>b;//判断折点前一个点
if(a>b)//a-前一个点
{
next1=1;
}
else if(a<b)
{
next1=-1;
}
a=b;
for(i=0;i<n-2;i++)
{
//cout<<"jin"<<endl;
cin>>b;
if(a>b)
{
next2=1;
}
else if(a<b)
{
next2=-1;
}
if(next1==-next2)
{
count++;
}
next1=next2;
a=b;
}
cout<<count;
return 0;
}
俄罗斯方块
**注意:**待放入的4*4区域中,左侧空白无关,下方空白要下移
#include <iostream>
#include <math.h>
/* run this program using the console pauser o r add your own getch, system("pause") or input loop */
using namespace std;
int main(int argc, char** argv) {
int a[15][10],b[4][4],i,j,k,u,n,flag,x,y;
for(i=0;i<15;i++)
{
for(j=0;j<10;j++)
{
cin>>a[i][j];
}
}
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
cin>>b[i][j];
}
}
flag=0;
for(i=3;i>=0;i--)//消除4*4的最下方空行
{
for(j=0;j<4;j++)
{
if(b[i][j]!=0){
flag=1;
break;
}
}
if(flag)
break;
if(j==4)
{
for(k=i;k>0;k--)
{
for(u=0;u<4;u++)
{
b[k][u]=b[k-1][u];
}
}
for(k=0;k<4-i;k++)
{
for(u=0;u<4;u++)
{
b[k][u]=0;
}
}
}
}
cin>>n;
for(i=0;i<11;i++)
{
flag=0;
for(j=0;j<4;j++)
{
for(k=0;k<4;k++)
{
cout<<i<<" "<<k<<","<<j<<endl;
if(k==0&&j==0)//要改的区域的左上点的坐标
{
x=11-i+j;
y=n+k-1;
}
if(b[j][k]==1&&a[11-i+j][n+k-1]==1)//14-(i+(3-j))
{
cout<<"冲突新点:"<<j<<","<<k<<endl;
cout<<"冲突旧点:"<<11-i+j<<","<<n+k-1<<endl;
flag=1;
break;
}
}
if(flag)
break;
}
if(j==4&&k==4)
break;
}
cout<<"坐标:"<<x<<","<<y<<endl;
for(i=x;i<x+4;i++)
{
for(j=y;j<y+4;j++)
{
cout<<b[i-x][j-y]<<" ";
if(b[i-x][j-y]==1){
cout<<i-x<<","<<j-y<<endl;
a[i][j]=1;
}
}
}
for(i=0;i<15;i++)
{
for(j=0;j<10;j++)
{
cout<<a[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
数位之和
#include <iostream>
#include <math.h>
/* run this program using the console pauser o r add your own getch, system("pause") or input loop */
using namespace std;
int main(int argc, char** argv) {
long long int n;
int sum=0;
cin>>n;
while(n!=0)
{
sum+=n%10;
n=n/10;
}
cout<<sum;
return 0;
}
消除游戏
为防止横竖连三都用到一个交叉的数,故将输入存储为两个
一个用来判断,一个用来消除修改
#include <iostream>
#include <math.h>
/* run this program using the console pauser o r add your own getch, system("pause") or input loop */
using namespace std;
int main(int argc, char** argv) {
int n,m,a[30][30],b[30][30],i,j,re,k,count1;
cin>>n>>m;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
cin>>a[i][j];
b[i][j]=a[i][j];
}
}
count1=0;//记录一行有多少个连续
for(i=0;i<n;i++)//横的n行
{
re=a[i][0];
count1=1;//换新的一行
for(j=1;j<m;j++)
{
if(a[i][j]==re)
{
count1++;
}
else
{
re=a[i][j];
if(count1>=3)
{
for(k=j-count1;k<j;k++)
{
b[i][k]=0;
}
}
count1=1;
}
if(a[i][j]==re&&j==m-1)//若该数和前一位相等,且是该行最后一个数,对其判断有无三个以上连续
{
if(count1>=3)
{
for(k=j-count1+1;k<=j;k++)
{
b[i][k]=0;
}
}
}
}
}
for(i=0;i<m;i++)//竖的m列
{
re=a[0][i];
count1=1;//换新的一列
for(j=1;j<n;j++)
{
if(a[j][i]==re)
{
count1++;
}
else
{
re=a[j][i];
if(count1>=3)
{
for(k=j-count1;k<j;k++)
{
b[k][i]=0;
}
}
count1=1;
}
if(a[j][i]==re&&j==n-1)//若该数和前一位相等,且是该行最后一个数,对其判断有无三个以上连续
{
if(count1>=3)
{
for(k=j-count1+1;k<=j;k++)
{
b[k][i]=0;
}
}
}
}
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
cout<<b[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
数列分段
#include <iostream>
#include <math.h>
/* run this program using the console pauser o r add your own getch, system("pause") or input loop */
using namespace std;
int main(int argc, char** argv) {
int n,i,a,b,count;
cin>>n;
cin>>a;
count=1;
for(i=0;i<n-1;i++)
{
cin>>b;
if(a!=b)
++count;
a=b;
}
cout<<count;
return 0;
}
日期计算
#include <iostream>
#include <math.h>
/* run this program using the console pauser o r add your own getch, system("pause") or input loop */
using namespace std;
int main(int argc, char** argv) {
int n,i,year,num;
int a[12]={31,28,31,30,31,30,31,31,30,31,30,31};
cin>>year>>num;
if((year%4==0&&year%100!=0)||year%400==0)
{
a[1]=29;
}
for(i=0;num!=0;i++)
{
if(num-a[i]>0)
num-=a[i];
else
break;
}
cout<<i+1<<endl<<num;
return 0;
}
模板生成系统
string line;
1 cin >> line //遇到回车键停止输入,按空格输出
但是输出不受影响:
string str="how are you";
cout<<str;
//输出 how are you
2.1 getline(cin,line);//按回车键结束输入
2.2 getline(cin,line,’#’); //输入一串字符(不管多少个回车键),只要是在‘#’号之 前的字符都会读取并保存
get与getline区别不是很大,但一个明显的区别是get遇到’\n '字符后便返回,这时 '\n '还在缓冲区中,所以下次读出来的将是 '\n ‘,而getline遇到’\n '也返回,但它会把 '\n '从缓冲区里移除掉,所以很多时候用getline方便些。
str.find (‘P’, 5);//要查找从字符 开始查找的下标
80分
#include <iostream>
#include <math.h>
#include <string>
#include <string.h>
/* run this program using the console pauser o r add your own getch, system("pause") or input loop */
using namespace std;
int main(int argc, char** argv) {
int m,n,i,j,p1,p2;
string s[100],infor[100][2];
cin>>m>>n;
cin.get(); //不知为何会有空格独占一行
for(i=0;i<m;i++)
{
getline(cin,s[i]);//遇到空格不停止输入
}
for(i=0;i<n;i++)
{
cin>>infor[i][0];
//cout<<"类别:"<<infor[i][0]<<endl;
getline(cin,infor[i][1]);
string type(infor[i][1].begin()+2,infor[i][1].end()-1);//+2的原因:getline把空格输入
infor[i][1]=type;
//cout<<"infor="<<infor[i][1]<<"TEST"<<endl;
}
for(i=0;i<m;i++)
{
p1=0;//p1!=-1 因为 string::npos=-1
while(p1!=string::npos)
{
p1=s[i].find("{{",p1+1);
//cout<<"p1="<<p1<<endl;
if(p1!=string::npos)
{
//cout<<"jin1"<<endl;
p2=s[i].find("}}",p1+1);
string type(s[i].begin()+p1+3,s[i].begin()+p2-1);
//cout<<"要替换的类型:"<<type<<"length:"<<type.length()<<endl;
for(j=0;j<n;j++)
{
//cout<<"infor type:"<<infor[j][0]<<"length:"<<infor[j][0].length()<<endl;
if(type==infor[j][0])
{
//cout<<"开始替换"<<endl;
s[i].replace(p1,p2-p1+2,infor[j][1]);//第二个参数:要替换的字符的长度
//cout<<"infor[i][1]:"<<infor[j][1]<<"TEST"<<endl;
//cout<<"s[i]:"<<s[i].length()<<" "<<s[i]<<endl;
break;
}
}
if(j==n)
s[i].replace(p1,p2-p1+2,"");//无匹配信息时,{{}}也要置空
}
}
}
for(i=0;i<m;i++)
{
cout<<s[i]<<endl;
}
return 0;
}
图像旋转
二维数组a[1000][1000]太大,要声明在main函数外
#include <iostream>
/* run this program using the console pauser o r add your own getch, system("pause") or input loop */
using namespace std;
int a[1000][1000];
int main(int argc, char** argv) {
int m,n,i,j;
cin>>n>>m;
for(i=0;i<n;i++)
for(j=0;j<m;j++)
cin>>a[i][j];
for(j=m-1;j>=0;j--){
for(i=0;i<n;i++){
cout<<a[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
数字排序
struct group a[1000]//传参的时候不传 全局变量
#include <iostream>
/* run this program using the console pauser o r add your own getch, system("pause") or input loop */
using namespace std;
struct group{
int num;
int count;
}a[1000];
int repeat(int n,int node)
{
int i;
for(i=0;i<n;i++)
{
if(a[i].num==node)
{
//cout<<"jin "<<a[i].num<<endl;
return i;
}
}
return -1;//不能NULL,会和i=0时冲突
}
int main(int argc, char** argv) {
int n,i,j,temp,len=0,f;
cin>>n;
for(i=0;i<n;i++)
{
cin>>temp;
f=repeat(i,temp);
if(f!=-1)
{
//cout<<f<<endl;
++a[f].count;
}
else{
a[len].num=temp;
a[len].count=1;
++len;
}
}
for(i=0;i<len-1;i++)
{
for(j=0;j<len-1;j++)
{
if(a[j].count<a[j+1].count)
{
group t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
else if(a[j].count==a[j+1].count)
{
if(a[j].num>a[j+1].num)
{
group t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
}
for(i=0;i<len;i++)
{
cout<<a[i].num<<" "<<a[i].count<<endl;
}
return 0;
}
节日
错
#include <iostream>
/* run this program using the console pauser o r add your own getch, system("pause") or input loop */
using namespace std;
struct group{
int y;
int m;
int d;
int a;
int b;
int c;
}now={1850,1,1,1,1,2};
int main(int argc, char** argv)
{
int i,a,b,c,y1,y2,temp;
int o[12]={31,28,31,30,31,30,31,31,30,31,30,31};
cin>>a>>b>>c>>y1>>y2;
//cout<<now.y;
if(y1>y2)
{
temp=y1;
y1=y2;
y2=temp;
}
int y[2]={y1,y2};
for(i=0;i<2;i++){
while(now.y<y[i])
{
now.m=1;
if((now.y%4==0&&now.y%100!=0)||now.y%400==0)
o[1]=29;
else
o[1]=28;
while(now.m<13)
{
now.d=1;
now.b=1;
while(now.d<=o[now.m-1])
{
//cout<<"JIN1"<<now.y<<" "<<now.m<<" "<<now.d<<";"<<now.b<<" "<<now.c<<endl;
++now.d;
if(now.c%7==0)
{
now.c=1;
++now.b;
}
else{
++now.c;
}
}
++now.m;
}
++now.y;
}
//cout<<"jin1"<<endl;
//y1那一年
now.m=1;
if((now.y%4==0&&now.y%100!=0)||now.y%400==0)
o[1]=29;
else
o[1]=28;
while(now.m<a)
{
now.d=1;
now.b=1;
while(now.d<=o[now.m-1])
{
//cout<<"JIN2"<<now.y<<" "<<now.m<<" "<<now.d<<";"<<now.b<<" "<<now.c<<endl;
++now.d;
if(now.c%7==0)
{
now.c=1;
++now.b;
}
else{
++now.c;
}
}
++now.m;
}
//cout<<"jin2"<<endl;
now.d=1;
now.b=1;
while(now.b<b)
{
//cout<<"JIN3"<<now.y<<" "<<now.m<<" "<<now.d<<";"<<now.b<<" "<<now.c<<endl;
++now.d;
if(now.c%7==0)
{
now.c=1;
++now.b;
}
else{
++now.c;
}
if(now.d>o[now.m-1])
break;
}
if(now.d>o[now.m-1]) {
cout<<"none"<<endl;
}
else if(now.d<=o[now.m-1]) {
//cout<<"jin3"<<endl;
while(now.c!=c)//!= 而不是 < ;eg. 上月末为周五,求这月周一
{
//cout<<"JIN4"<<now.y<<" "<<now.m<<" "<<now.d<<";"<<now.b<<" "<<now.c<<endl;
++now.d;
if(now.c%7==0)
{
now.c=1;
++now.b;
}
else{
++now.c;
}
if(now.d>o[now.m-1]) //不知道为什么要加在后面,反正运行后是对的
break;
}
if(now.d>o[now.m-1]) {
cout<<"none"<<endl;
}
else if(now.d<=o[now.m-1]) {
//cout<<"jin4"<<endl;
if(now.m>9&&now.d>9)
cout<<now.y<<"/"<<now.m<<"/"<<now.d<<endl;
else if(now.m>9&&now.d<=9)
cout<<now.y<<"/"<<now.m<<"/0"<<now.d<<endl;
else if(now.m<=9&&now.d>9)
cout<<now.y<<"/0"<<now.m<<"/"<<now.d<<endl;
else if(now.m<=9&&now.d<=9)
cout<<now.y<<"/0"<<now.m<<"/0"<<now.d<<endl;
}
}
if(i==0)
{
while(now.d<=o[now.m-1])
{
//cout<<"JIN5"<<now.y<<" "<<now.m<<" "<<now.d<<";"<<now.b<<" "<<now.c<<endl;
++now.d;
if(now.c%7==0)
{
now.c=1;
++now.b;
}
else{
++now.c;
}
}
++now.m;
while(now.m<13)
{
now.d=1;
now.b=1;
while(now.d<=o[now.m-1])
{
//cout<<"JIN6"<<now.y<<" "<<now.m<<" "<<now.d<<";"<<now.b<<" "<<now.c<<endl;
++now.d;
if(now.c%7==0)
{
now.c=1;
++now.b;
}
else{
++now.c;
}
}
++now.m;
}
++now.y;
}
}
return 0;
}