会员
周边
众包
新闻
博问
闪存
所有博客
当前博客
我的博客
我的园子
账号设置
简洁模式
...
退出登录
注册
登录
spring学习笔记
算法竞赛入门经典 3.1 数组
//3.1 数组 //程序3-1 逆序输出 #include<iostream> using namespace std; const int MAX=100+10; //据说这样声明,是为了保险 int a[MAX]; int main() { int i,x,n=0; while(cin>>x) a[n++]=x; //将每一个值赋给数组,下标每次增加1 for(i=n-1;i>=1;i--) //逆序输出 cout<<a[i]<<" "<<endl; cout<<a[0]<<endl; return 0; } //程序3-2 开灯问题 /* *有n盏灯,编号为1~n。第1个人把所有灯打开,第2个人按下所有编号为2的倍数的开关 *(这些灯将被关掉),第3个人按下所有编号为3的倍数的开关(其中关掉的灯被打开,开着的灯被关闭) *依次类推。一共有k个人,问最后有哪些灯是亮着的? *输入:n和k,输出开着的灯编号。k<=n<=1000 *样例输入:7 3 *样例输出:1 5 6 7 */ #include<iostream> #include<cstring> using namespace std; const int MAX=1000+10; int a[MAX]; int main() { int i,j,n,k,first=1; memset(a,0,sizeof(a)); //作用是把数组a清零 cin>>n>>k; for(i=1;i<=k;i++) for(j=1;j<=n;j++) if(j%i==0) a[j]=!a[j]; for(i=1;i<=n;i++) if(a[i]) { if(first) first=0; else cout<<" "; cout<<i; } cout<<endl; return 0; } //例题3-2 蛇形填数 /* *在n*n的方阵里填入1,2,...,n*n,要求填成蛇形。例如n=4时方阵为: *10 11 12 1 *9 16 13 2 *8 15 14 3 *7 6 5 4 */ #include<iostream> #include<cstring> using namespace std; const int MAX=10; int a[MAX][MAX]; int main() { int n,x,y,tot=0; cin>>n; memset(a,0,sizeof(a)); //清零 tot=a[x=0][y=n-1]=1; //第一个数从1开始填,位置为x=0,y=n-1 while(tot<n*n) { while(x+1<n && !a[x+1][y]) a[++x][y]=++tot; //向下填数 while(y-1>=0 && !a[x][y-1]) a[x][--y]=++tot; //向左填数 while(x-1>=0 && !a[x-1][y]) a[--x][y]=++tot; //向上填数 while(y+1<n && !a[x][y+1]) a[x][++y]=++tot; //向右填数 } for(x=0;x<n;x++) { for(y=0;y<n;y++) cout<<a[x][y]<<" "; cout<<endl; } return 0; }
posted on
2012-03-27 22:15
spring学习笔记
阅读(
324
) 评论(
1
)
编辑
收藏
举报
会员力量,点亮园子希望
刷新页面
返回顶部
导航
博客园
首页
联系
订阅
管理
公告