////MAIN.C//Array exercises////Created by zhangxueming on 15/6/2.//Copyright (c) 2015 zhangxueming. All rights reserved.//#include <stdio.h>//#include <stdbool.h>//7. Calculates the total width of an array of English, numerals, and punctuation, where the width of the English character is//1cm, the digital width is 0.5cm and the punctuation width is 0.8cm. //8. After the question, if the width of the specified line is 10cm, a string with a character longer than 50 is truncated, which makes the 10cm wide row fit. Output this truncated sub-array. //RTYUIOPGFDFGJ\0FGHJKLKJHfloatGetcharacterwidth (CharCH) {if((ch>=' A '&& ch<=' Z ') || (ch>=' A '&& ch<=' Z ')) {return 1.0; }Else if(ch>=' 0 '&& ch<=' 9 ') {return 0.5; }Else{return 0.8; }}//int Main (int argc, const char * argv[]) {//Char str[100]={};//int cnt =0;//Float length = 0.0;// //for (int i=0; i<100; i++) {//scanf ("%c", &str[i]);//if (str[i]== ' \ n ') {//str[i]= ' + ';//break;// }//cnt++;// }//int i=0;//for (; i<cnt; i++) {//length+= getcharacterwidth (Str[i]);//if (length>10.0) {//str[i]= ' + ';//break;// }// }// ////for (int j=0; j<i; J + +) {////printf ("%c", Str[j]);//// }//printf ("%s\n", str);//return 0;//}//13. Given an integer array of 5 elements, each element has a value between 0-9 and is composed of a 5-digit number by location and output, such as int a[5] = {1,2,2,3,7}; then output 73221. //num = 7//num*10+3 =//num*10+2 = 732//num*10+2 = 7322//num*10+1 = 73221//int Main (int argc,const char *argv[])//{//int a[5]={};//int num = 0;//for (int i=0; i<5; i++) {//scanf ("%d", &a[i]);// }//for (int i=4; i>=0; i--) {//num = Num*10+a[i];// }//printf ("num =%d\n", num);//return 0;//}//18. Determines whether an integer array is a symmetric array, such as a[6]={1,2,3,3,2,1} and {1,6,8,1,8,6,1} are symmetric arrays. typedef enum{false,//0 true //1}BOOL;BOOLIssymmetryarray (intA[],intLen) { for(intI=0; i<len/2; i++) {if(a[i]!=a[len-i-1]) {return false; } }return true;}//int Main (int argc,const char *argv[])//{//int a[6]={1,2,3,3,2,1};//printf ("%d\n", Issymmetryarray (a,6));//return 0;//}//30. Given an English sentence, divide the word by 1 spaces and find the offset of the 2nd word. For exampleThe offset position of//"Professor du comes from Korea" is 10. //int Main (int argc,const char *argv[])//{//Char str[100]={};//scanf ("%[^\n]", str);////SCANF ("%[a-z,a-z,0-9]", str);////printf ("%s", str);//int i=0;//while (Str[i]) {//if (str[i]== ') {//break;// }//i++;// }//printf ("%d", i+1);////return 0;//}//43. Circle Count//There are n individuals in a circle, order automatic arranging. From the first person began to count (from 1 to M off), where the person reporting m out of the circle, ask the last left is the original number of which.//0 0 0 1 0 3//int Main (int argc,const char *argv[])//{//int a[100]={};//int n,m;//INT cnt=0;//Statistics out of the number of people voted out//int k=0;// count off//int i=0;//scanf ("%d%d", &n, &m);//if (n<0| | n>100) {//return-1;// }// //for (i=0; i<n; i++) {//a[i]=1;// }//i=0;//while (cnt<n-1) {//if (A[i]) {//k++;//if (k==m)// {//a[i]=0;//k=0;//cnt++;// }// }//i++;//if (i==n) {//Judgment boundary//i=0;// }// }//for (i=0; i<n; i++) {//if (A[i]) {//printf ("%d", i+1);//break;// }// }//return 0;//}//Monkey Eat peach problem: The first day the monkey picked up a number of peaches, ate half, not addicted, and ate one more the next morning and the rest of the peach eaten half, and ate one more. Every morning after eating the rest of the day half of the zero one. When I want to eat again in the morning of the 10th day, I see only one peach left. For the first day to pick a total number. //10 1//9 (4)//8 (4+1)intFuncintN) {if(n==Ten) {return 1; }Else if(n>Ten) {return 0; }return(Func (n+1)+1)*2;}//int Main (int argc, const char *argv[])//{////printf ("%d\n", func (8));// //return 0;//}//4: Enter 10 numbers to find the most frequently occurring number (if multiple side-by-side, output separately in the order of numbers)//For example: a[10]=//Input: 1 2 2 3 4 5 6 7 8 9//output: 2///Data structure: Defines an integer array int count[10] stores the number of each element, starting with the number of elements initialized to 1////algorithm://(1) using a double loop, each element is compared to a subsequent element, and if the two are the same, the number of elements is +1,//(2) The number of the same elements in the above algorithm is the same, the optimization is as follows, when comparing two elements, if the two are equal, then the element number +1, the number of elements after the set to 0, compared to determine whether the element has been compared////Pseudo-code:////define array to save each element number of times////Use a double loop to iterate through an array//If the elements behind the current element are equal, and the number of subsequent elements is not 0//Current element number +1, followed by the number of elements set to 0////Find the maximum value from the array that holds each element count//a[10]=//1 2 2 5 4 5 2 7 5 9//count[10]//1 3 0 2 1 0 0 1 1 1intMainintargcConst Char*argv[]) {inta[Ten]={};intcnt[Ten]={}; for(intI=0; i<Ten; i++) {scanf("%d", &a[i]); } for(intI=0;i<Ten; i++) {cnt[i]=1; } for(intI=0; i<Ten; i++) {if(Cnt[i]) { for(intj=i+1; j<Ten; J + +) {if(A[i]==a[j]) {cnt[i]++; cnt[j]=0; } } } }//for (int i=0; i<10; i++) {//printf ("%d", Cnt[i]);// } //Find the maximum value in a CNT array intmax = cnt[0]; for(intI=1; i<Ten; i++) {if(Max<cnt[i]) {max = cnt[i]; } }//Output value for(intI=0; i<Ten; i++) {if(Max==cnt[i]) {printf("%d", A[i]); } }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Array Exercise 1