c语言数组那些骚事儿
找出最大和第二大值
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int mynum[] = {43, 88, 56, 24, 78, 12, 8,29,57,80};
int max=0;
printf("%d\n", max);
int smax=0;
printf("%d\n", smax);
if (mynum[0] > mynum[1])
{
max = mynum[0];
smax = mynum[1];
}
else
{
max = mynum[1];
smax = mynum[0];
}
for (int i = 2; i < 10; i++)
{
if (max < mynum[i])
{
smax = max;
max = mynum[i];
}
else if ((mynum[i]<max) && (mynum[i]>smax))
{
smax = mynum[i];
}
}
printf("%d\n",max);
printf("%d\n", smax);
system("pause");
}
- 数组的逆序----思想是第一个和最后一个,第二个和倒数第二个
#include <stdio.h> #include <stdlib.h> #include <string.h> int main() { int mynum[] = { 43, 88, 56, 24, 78, 12, 8, 29, 57, 80 }; int cnt = sizeof(mynum) / 4; printf("%d\n",cnt); for (int i = 0; i < cnt/2; i++) { int temp = mynum[i]; mynum[i] = mynum[cnt - i - 1]; mynum[cnt - i - 1] = temp; } for (int i = 0; i < 10; i++) { printf("%d,", mynum[i]); } system("pause"); return EXIT_SUCCESS; }
- 字符串逆序,------目前只能应用于中间带空格的英文字符串
#include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char mywords[] = "hello world"; int cnt; cnt = sizeof(mywords)-1; printf("%d\n",cnt); for (int i = 0; i < cnt/2; i++) { if (i == 6) { } else { char temp = mywords[i]; mywords[i] = mywords[cnt - i - 1]; mywords[cnt - i - 1] = temp; } } for (int i = 0; i < sizeof(mywords); i++) { printf("%c", mywords[i]); } system("pause"); return EXIT_SUCCESS; }