* 1008. Right shifting of array element Loops

Source: Internet
Author: User
1/* 2 * Main. C 3*1008. array Element loop right shift problem Ver.2 4 * created on: August 29, 2014 5 * Author: boomkeeper 6 ********* passed the test ******** 7 */8 # include <stdio. h> 9 10 int main (void) {11 12 INT array [101]; 13 int n, m; // n, m14 int I, j In the question; 15 16 scanf ("% I", & N, & M); 17 getchar (); 18 19 for (I = 0; I <n; I ++) {20 scanf ("% I", & array [I]); 21} 22 // limit m to 0 ~ The range of N is 23 // while (M> N) {24 // M/= N; 25 //} 26 // shift right 27 int temp; 28 for (I = 0; I <m; I ++) {29 temp = array [n-1]; 30 for (j = n-2; j> = 0; j --) {31 array [J + 1] = array [J]; 32} 33 array [0] = temp; 34} 35 // output 36 for (I = 0; I <n; I ++) {37 if (I = 0) 38 printf ("% I", array [I]); 39 else40 printf ("% I ", array [I]); 41} 42 printf ("\ n"); 43 44 return 0; 45}

 

If the value of M is 0 ~ N range, an error will be reported when the question is submitted. In this case, if M> N, the loop is actually invalid for many times... it should be multiple loops (M/N) * n times...

1/* 2 * Main. C 3*1008. array Element loop right shift problem ver.1 4 * created on: August 29, 2014 5 * Author: in the boomkeeper 6 ********* section, ******** 7 */8 # include <stdio. h> 9 10 int main (void) {11 12 INT array [101]; 13 int n, m; // n and m14 int I in the question; 15 16 scanf ("% I", & N, & M); 17 getchar (); 18 19 for (I = 0; I <n; I ++) {20 scanf ("% I", & array [I]); 21} 22 // limit m to 0 ~ The range of N is 23 while (M> N) {24 m/= N; 25} 26 // output 27 for (I = N-m; I <n; I ++) {28 if (I = N-m) 29 printf ("% I", array [I]); 30 else31 printf ("% I", array [I]); 32} 33 for (I = 0; I <n-m; I ++) 34 printf ("% I", array [I]); 35 printf ("\ n"); 36 37 return 0; 38}

 

This algorithm is not completely passed. If the m range is not restricted, the array will be out of bounds.

 

Question link:

Http://pat.zju.edu.cn/contests/pat-b-practise/1008

 

 

.

* 1008. Right shifting of array element Loops

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.