由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - Simplest way to iterate array from both ends alternatively?
相关主题
这段 C++ 怎么改才能编译?请教改numpy array的dtype
请教一个C++问题intel icc hash_map 求救!
julia有前途吗? (转载)STL感觉实在太变态了
请教python[菜鸟问题]类模板问题
我这样把一个2D array 的每个column的数据导入到1D array的方法对不对?c++ iterator 弱问
一个C++的概念问题c++ template question:
a simple question请问Linux底下有没有最简易的show 2D x-y curve的工具
A question about sharing data inside a C++ class用那个design pattern好?
相关话题的讨论汇总
话题: iterate话题: simplest话题: sz话题: output话题: array
进入Programming版参与讨论
1 (共1页)
w****i
发帖数: 964
1
Suppose the array is a1...a9
to iterate it in this order: a1, a9, a2, a8, a3, a7, a4, a6, a5
how to write it in a simple form, say a for loop?
N***m
发帖数: 4460
2
i=1;j=9;
while(i<=j)
{
output a[i]
if(i!=j) output a[j]
i++;
j--;
}

【在 w****i 的大作中提到】
: Suppose the array is a1...a9
: to iterate it in this order: a1, a9, a2, a8, a3, a7, a4, a6, a5
: how to write it in a simple form, say a for loop?

d****n
发帖数: 1637
3
sz=10
arr[sz]={0,1,2,3,4,5,6,7,8,9}
int i;
for(i=0;i<(int)sz/2;++i){
printf("%d %d ", arr[i],arr[sz-i-1]);
}
if(sz%2==1) printf(" %d ",arr[sz/2+1]);
d****n
发帖数: 1637
4
this is better!

【在 N***m 的大作中提到】
: i=1;j=9;
: while(i<=j)
: {
: output a[i]
: if(i!=j) output a[j]
: i++;
: j--;
: }

N***m
发帖数: 4460
5
:)

【在 d****n 的大作中提到】
: this is better!
w****i
发帖数: 964
6
loop里面需要处理一下a[i],有几行code不长不短,想偷懒只写一遍,所以有了这个问题
这里给的两个答案都要写两遍 output a[i]
后来想到这个:
for(i=0;i if(i%2==0) ptr=i/2 else ptr=len-1-i/2;
/* process a[ptr] */
}

【在 N***m 的大作中提到】
: i=1;j=9;
: while(i<=j)
: {
: output a[i]
: if(i!=j) output a[j]
: i++;
: j--;
: }

1 (共1页)
进入Programming版参与讨论
相关主题
用那个design pattern好?我这样把一个2D array 的每个column的数据导入到1D array的方法对不对?
关于inserter一个C++的概念问题
binary_search只要求forward_iterator?a simple question
deque的pointer和reference是怎么回事?A question about sharing data inside a C++ class
这段 C++ 怎么改才能编译?请教改numpy array的dtype
请教一个C++问题intel icc hash_map 求救!
julia有前途吗? (转载)STL感觉实在太变态了
请教python[菜鸟问题]类模板问题
相关话题的讨论汇总
话题: iterate话题: simplest话题: sz话题: output话题: array