<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<atom:link href="http://gentoo-zh.org/extern.php?action=feed&amp;tid=377&amp;type=rss" rel="self" type="application/rss+xml" />
		<title><![CDATA[Gentoo-zh / C 练习实例68]]></title>
		<link>http://www.gentoo-zh.org/viewtopic.php?id=377</link>
		<description><![CDATA[C 练习实例68 最近发表的帖子。]]></description>
		<lastBuildDate>Mon, 29 Aug 2022 11:40:46 +0000</lastBuildDate>
		<generator>FluxBB</generator>
		<item>
			<title><![CDATA[C 练习实例68]]></title>
			<link>http://www.gentoo-zh.org/viewtopic.php?pid=383#p383</link>
			<description><![CDATA[<p>题目：有 n个整数，使其前面各数顺序向后移 m 个位置，最后m个数变成最前面的 m 个数。</p><p>程序分析：无。</p><div class="codebox"><pre class="vscroll"><code>#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
int main()
{
    int arr[20];
    int i,n,offset;
    //输入数组大小和数组内容
    printf(&quot;Total numbers?\n&quot;);
    scanf(&quot;%d&quot;,&amp;n);
    printf(&quot;Input %d numbers.\n&quot;,n);
    for(i=0;i&lt;n;i++)
        scanf(&quot;%d&quot;,&amp;arr[i]);
    //输入滚动偏移量
    printf(&quot;Set your offset.\n&quot;);
    scanf(&quot;%d&quot;,&amp;offset);
    printf(&quot;Offset is %d.\n&quot;,offset);
    //打印滚动前数组
    print_arr(arr,n);
    //滚动数组并打印
    move(arr,n,offset);
    print_arr(arr,n);
}
 
//打印数组
void print_arr(int array[],int n)
{
    int i;
    for(i=0;i&lt;n;++i)
        printf(&quot;%4d&quot;,array[i]);
    printf(&quot;\n&quot;);
}
//滚动数组
void move(int array[],int n,int offset)
{
    int *p,*arr_end;
    arr_end=array+n;      //数组最后一个元素的下一个位置
    int last;
    
    //滚动直到偏移量为0
    while(offset)
    {
        last=*(arr_end-1);
        for(p=arr_end-1;p!=array;--p)   //向右滚动一位
            *p=*(p-1);
        *array=last;
        --offset;
    }
}</code></pre></div>]]></description>
			<author><![CDATA[dummy@example.com (batsom)]]></author>
			<pubDate>Mon, 29 Aug 2022 11:40:46 +0000</pubDate>
			<guid>http://www.gentoo-zh.org/viewtopic.php?pid=383#p383</guid>
		</item>
	</channel>
</rss>
