2015.1.28
3. Given an array of characters with 5 elements, the members of the array have Arabic characters, try to set the number
The group is converted to an integer, such as the contents of the character array: {' 1 ', ' 2 ', ' 3 ', ' 3 ', ' 2 '} will be converted to 12332.
Working with shaped arrays
/*int Main (int argc, const char * argv[]) {
int a[100];
int cnt=0;
int sum=0;
for (int i=0; i<100; i++) {
scanf ("%d", &a[i]);
cnt++;
if (getchar () = = ' \ n ') {
Break
}
}
for (int i=0; i<cnt; i++) {
sum = Sum*10+a[i];
}
printf ("sum =%d\n", sum);
return 0;
}*/
Working with character arrays
#if 0
int main (int argc, const char * argv[]) {
Char ch[100];
int cnt=0;
int sum=0;
for (int i=0; i<100; i++) {
scanf ("%c", &ch[i]);
if (ch[i]== ' \ n ') {
ch[i]= ' + ';
Break
}
cnt++;
}
for (int i=0; i<cnt; i++) {
sum = sum*10+ (ch[i]-' 0 ');
}
printf ("sum =%d\n", sum);
return 0;
}
#endif
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, the punctuation mark width is 0.8cm.
8. After the question, if the width of the specified line is 10cm, a character length of more than 50 of the string truncation, just so that the width of the 10cm line can accommodate. Output this truncated sub-array.
#include <string.h>
float Judgechar (Char ch)
{
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;
}
}
#if 0
int main (int argc, const char *argv[])
{
Char str[100];
float sum = 0.00;
scanf ("%s", str);
int len = (int) strlen (str);//The number of valid characters for a string, not including ' n ';
for (int i=0; i<len; i++) {
Sum+=judgechar (Str[i]);
}
printf ("sum =%.2f\n", sum);
return 0;
}
#endif
#if 0
int main (int argc,const char *argv[])
{
Char str[100];
scanf ("%s", str);
int len = (int) strlen (str);
float sum = 0;
for (int i=0; i<len; i++) {
Sum+=judgechar (Str[i]);
if (sum>10) {
str[i]= ' + ';
Break
}
if (sum==10) {
str[i+1]= ' + ';
Break
}
}
printf ("str =%s\n", str);
return 0;
}
#endif
Given an array of integers, the length of the array is N (n>3), looking for a contiguous subarray of length 3 from the array, requiring the and maximum of that subarray.
#if 0
int main (int argc, const char *argv[])
{
int a[100];
int cnt=0;
for (int i=0; i<100; i++) {
scanf ("%d", &a[i]);
cnt++;
if (getchar () = = ' \ n ') {
Break
}
}
int max = a[0]+a[1]+a[2];
for (int i=1; i<cnt-2; i++) {
if (max<a[i]+a[i+1]+a[i+2]) {
max = a[i]+a[i+1]+a[i+2];
}
}
printf ("max =%d\n", max);
return 0;
}
#endif
41. Write a function to transpose a n*n matrix, for example: (* * *)
1 2 3 4 1 5 3 4
5 6 7 8-2 6 2 7
3 2 5 9 3 7 5 2
4 7 2 3 4 8 9 3
void Reversearr (int *a[], int n)
//{
//
return 0;
//}
#if 0
int main (int argc,const char *argv[])
{
int a[4][4]={{1,2,3,4},
{5,6,7,8},
{3,2,5,9},
{4,7,2,3}};
for (int i=0; i<4-1; i++) {
for (int j=i+1; j<4; J + +) {
int temp = A[i][j];
A[i][j]=a[j][i];
A[j][i]=temp;
}
}
for (int i=0; i<4; i++) {
for (int j=0; J <4; J + +) {
printf ("%d", a[i][j]);
}
printf ("\ n");
}
return 0;
}
#endif
Monkey Eat Peach Problem: The first day the monkey picked up a number of peaches, ate half, not addicted, and ate a second morning and the rest of the peach eaten half, and eat 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.
#if 0
int main (int argc,const char *argv[])
{
int a[10]={};
A[9]=1;
for (int i=8; i>=0; i--) {
a[i]= (a[i+1]+1) * *;
}
printf ("%d", a[0]);
return 0;
}
#endif
Circle Count
There are m individuals in a circle, in order automatic arranging. From the first person began to count (from 1 to N), where the person who reported m out of the circle, ask the last left is the original number of which.
5 3--4
6 2--5
0 0 0) 4 0
//
#if 0
int main (int argc, const char *argv[])
{
int a[100];
int m, n;
int cnt = 0;//statistics Number of people exiting
scanf ("%d%d", &m, &n);
if (m<1| | m>100 | | n<1) {
return-1;
}
int i=0;
for (i=0; i<m; i++) {//Assign values to array
a[i]=i+1;
}
int k=0;//count off
i = 0;
while (cnt<m-1) {
if (A[i])
{
k++;
if (k==n) {
a[i]=0;
k=0;
cnt++;
}
}
i++;
if (i==m) {//Boundary processing
i=0;
}
}
for (int i=0; i<m; i++) {
if (A[i]) {
printf ("%d", i+1);
}
}
printf ("\ n");
return 0;
}
#endif
3. Enter 10 numbers, any adjacent two numbers, output all increments, descending sequence
Like what:
Input: 1 5 9 8 12 21 3 0-1 9
Output:
1 5 9
9 8
8 12 21
21 3 0-1
-1 9
//
Input: 3 9 8-11 4 21 8-3 0 2
Output:
3 9
9 8-11
-11 4 21
21 8-3
-3 0 2
#if 0
int main (int argc,const char *argv[])
{
int a[10]={};
for (int i=0; i<10; i++) {
scanf ("%d", &a[i]);
}
printf ("%d", a[0]);
for (int i=1; i<9; i++) {
printf ("%d", a[i]);
if ((A[i]>a[i-1] && a[i]>a[i+1]) | | (A[i]<a[i-1] && a[i]<a[i+1])) {
Putchar (' \ n ');
printf ("%d", a[i]);
}
}
printf ("%d\n", a[9]);
return 0;
}
#endif
2. Enter two numbers, the first number determines the matrix of a nxn, the second number determines the assignment from 1, and the upper bound of the value
Like what:
Input: 5 18
Output:
1 2 3) 4 5
16 17 18) 0 6
15 0 0) 0 7
14 0 0) 0 8
13 12 11) 10 9
//
Input: 4 12
Output:
1 2 3 4
12 0 0 5
11 0 0 6
10 9 8 7
#if 0
int main (int argc, const char *argv[])
{
int a[20][20]={};
int m,n;
scanf ("%d%d", &n,&m);
if (n<1 | | m>n*n| | m<0) {
return-1;
}
int s1=n-1, s2=n-1, s3=0, s4=1;//control boundary
int i=0,j=0;//Control the subscript of two-dimensional array
int TYPE=1;//1: Assignment from left to right, 2: assignment from top to bottom, 3: assignment from right to Left 4: Assign value from bottom to top
int cnt=1;//Indicates the number of assignments
while (cnt<=m) {
a[i][j]=cnt;//Assignment Value
Switch (type) {
Case 1://assignment from left to right
if (J==S1) {
type=2;
s1--;
i++;
}
Else
{
j + +;
}
Break
Case 2://value from top down
if (I==S2) {
Type = 3;
s2--;
j--;
}
Else
{
i++;
}
Break
Case 3://assignment from right to left
if (J==S3) {
Type = 4;
s3++;
i--;
}
Else
{
j--;
}
Break
Case 4://assignment from bottom to top
if (I==S4) {
Type = 1;
s4++;
j + +;
}
Else
{
i--;
}
Break
Default
Break
}
cnt++;
}
for (int i=0; i<n; i++) {
for (int j=0; j<n; J + +) {
printf ("%4d", A[i][j]);
}
printf ("\ n");
}
return 0;
}
#endif
4. Enter 10 numbers to find the most frequently occurring number (if multiple side-by-side, output separately in the order of numbers)
Like what:
Input: 1 2 2 3 2 5 5 7 8 9
Output: 2 5
1 3 0 1 0 2 0 1 1 1
max = 2;
2 5
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 above algorithm in the same number of two elements is the same, the optimization is as follows, when comparing two elements, if the two are equal, then the number of elements +1, the number of elements after the set to 0, compared to determine whether this element has been compared
//
Pseudo code:
//
Defines the number of times each element is saved in an array
//
Iterating through an array using a double loop
If the elements behind the current element are equal, and the number of elements following it is not 0
Current number of elements +1, followed by the number of elements set to 0
//
Find the maximum value from the array that holds each element count
#if 0
int main (int argc,const char *argv[])
{
int a[10]={};
int count[10];
for (int i=0; i<10; i++) {
scanf ("%d", &a[i]);
}
for (int i=0; i<10; i++) {
Count[i]=1;
}
for (int i=0; i<10; i++) {
if (Count[i]) {
for (int j=i+1; j<10; J + +) {
if (A[i]==a[j] && count[j]) {
count[i]++;
count[j]=0;
}
}
}
}
for (int i=0; i<10; i++) {
printf ("%d", count[i]);
// }
Find Count Max
int max = count[0];
for (int i=1; i<10; i++) {
if (Max<count[i]) {
max = Count[i];
}
}
The maximum number of times the output has occurred
for (int i=0; i<10; i++) {
if (max = = Count[i]) {
printf ("%d", a[i]);
}
}
printf ("\ n");
return 0;
}
#endif
5. The Rubik's Cube is an ancient intellectual problem that requires the number of 1~m^2 to be filled in a m*m matrix (M is odd), so that each row, each column, each diagonal is summed and equal. Please program the input m, output m*m the magic matrix out.
Like what:
Input: 3
Output:
6 1 8
7 5 3
2 9 4
(1) Placing 1 in the middle column of the first row;
(2) Starting from 2 until the number of NXN the following rules are deposited; each number of rows is less than the number of rows in the previous number minus 1, and the number of columns minus 1 (for example, the third-order magic Phalanx above, 5 on the previous row of 4);
(3) If the number of rows in the previous number is 1, the number of rows for the next number is n (the bottom line), for example 1 in the first row, 2 should be placed on the bottom row, and the number of columns is also added 1;
(4) When the number of columns in the previous number is 1 o'clock, the next number of columns should be n, and the number of rows minus 1. For example 2 in the first column of line 3rd, 3 should be placed in the last column of the second row;
(5) If the number is already in the position determined by the above rule, or if the previous number is the first row of the nth column, the next number is placed below the previous number. For example, according to the above provisions, 4 should be placed in the 1th row 2nd column, but the position has been occupied, so 4 is placed under 3;
int main (int argc,const char *argv[])
{
int a[19][19]={};
int m;
scanf ("%d", &m);
if (! ( m%2) | | m<3 | | M>19) {
return-1;
}
int I=0,J=M/2;
int cnt = 1;
int TEMI,TEMJ;
while (cnt<=m*m) {
a[i][j]=cnt;
Temi = i;//holds the subscript of the previous number
TEMJ = j;
i--;//Transformation Subscript
j--;
if (temi==0)//Transform row label
{
i = m-1;
}
if (temj==0)//Transform column label
{
j = m-1;
}
if (A[i][j])
{
i = temi+1;
j = TEMJ;
}
cnt++;
}
for (int i=0; i<m; i++) {
for (int j=0; j<m; J + +) {
printf ("%4d", A[i][j]);
}
printf ("\ n");
}
return 0;
}
ios-c_day8___ Array Exercises