数组与二维数组
數組與函數
//1.給定某個字符數組,統計數組中所有英文字符的個數,比如“123fdd”中有 3 個。
//數組作為函數的接口參數, 數組的長度丟失
#include <string.h>
int numberOfCharacter(char src[], int len)
{
? ? int cnt=0;
? ? for (int i=0; i<len; i++) {
? ? ? ? if ((src[i]>='A' && src[i]<='Z') || (src[i]>='a' && src[i]<='z')) {
? ? ? ? ? ? cnt++;
? ? ? ? }
? ? }
? ? return cnt;
}
?
/*int main(int argc, const char * argv[]) {
? ? char str[200];
? ? int len=0;
? ? for (int i=0; i<200; i++) {
? ? ? ? scanf("%c", &str[i]);
? ? ? ? if (str[i]=='\n') {
? ? ? ? ? ? str[i]='\0';
? ? ? ? ? ? break;
? ? ? ? }
? ? ? ? len++;
? ? }
//? ? scanf("%s", str);
//? ? int len = (int)strlen(str);//求字符串中有效字符個數
? ? printf("%d\n", numberOfCharacter(str,len));
?? ?
? ? return 0;
}*/
?
?
//判斷一個整型數組是否是對稱數組,例如{1,2,3,3,2,1}和{1,6,8,1,8,6,1}都是對稱數組。
#include <stdbool.h>
?
//bool isSymmetricArray(int src[], int len)
//{
//? ? int i=0;
//? ? for (i=0; i<len/2; i++) {
//? ? ? ? if (src[i]!=src[len-i-1]) {
//? ? ? ? ? ? break;
//? ? ? ? }
//? ? }
//? ? if (i==len/2) {
//? ? ? ? return true;
//? ? }
//? ? return false;
//}
//int main(int argc,const char *argv[])
//{
//? ? int a[]={1,6,8,1,8,6,1};
//? ? printf("%d\n", isSymmetricArray(a,7));
//
//? ? return 0;
//}
?
?
//數組排序
//冒泡排序? 從小到大
//9 7 8 5 6 ? --> 5 6 7 8 9
//第一次排序
//7 9 8 5 6
//7 8 9 5 6
//7 8 5 9 6
//7 8 5 6 9
?
//第二次排序
//7 8 5 6 9
//7 5 8 6 9
//7 5 6 8 9
?
//第三次排序
//5 7 6 8 9
//5 6 7 8 9
?
//第四次排序
//5 6 7 8 9
?
/*int main(int argc,const char *argv[])
{
? ? int a[5]={9,7,8,5,6};
? ? for (int i=0; i<5-1; i++) {//控制排序的次數
//? ? ? ? for (int j=0; j<5-1-i; j++) {//控制交換的次數
//? ? ? ? ? ? if (a[j]>a[j+1]) {
//? ? ? ? ? ? ? ? int temp = a[j];
//? ? ? ? ? ? ? ? a[j]=a[j+1];
//? ? ? ? ? ? ? ? a[j+1]=temp;
//? ? ? ? ? ? }
//? ? ? ? }
? ? ? ? for (int j=1; j<5-i; j++)
? ? ? ? {
? ? ? ? ? ? if (a[j-1]>a[j]) {
? ? ? ? ? ? ? ? int temp = a[j-1];
? ? ? ? ? ? ? ? a[j-1]=a[j];
? ? ? ? ? ? ? ? a[j]=temp;
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? for (int i=0; i<5; i++) {
? ? ? ? printf("%d ", a[i]);
? ? }
? ? printf("\n");
}
*/
?
//選擇排序
//9 7 8 5 6
//找最大值
//第一次排序
//6 7 8 5 9
//第二次排序
//6 7 5 8 9
//第三次排序
//6 5 7 8 9
//第四次排序
//5 6 7 8 9
//int main(int argc,const char *argv[])
//{
//? ? int a[5]={9,7,8,5,6};
// ? ?
//? ? for (int i=5-1; i>0; i--) {
//? ? ? ? int k = i;
//? ? ? ? for (int j=i-1; j>=0; j--) {
//? ? ? ? ? ? if (a[k]<a[j]) {
//? ? ? ? ? ? ? ? k=j;
//? ? ? ? ? ? }
//? ? ? ? }
//? ? ? ? if (k!=i) {
//? ? ? ? ? ? int temp = a[k];
//? ? ? ? ? ? a[k]=a[i];
//? ? ? ? ? ? a[i]=temp;
//? ? ? ? }
//? ? }
//? ? for (int i=0; i<5; i++) {
//? ? ? ? printf("%d ", a[i]);
//? ? }
//? ? putchar('\n');
// ? ?
//? ? return 0;
//}
?
//
//9 7 8 5 6
//找最小值
//k = i;
//第一次排序
//5 7 8 9 6
//第二次排序
//5 6 8 9 7
//第三次排序
//5 6 7 9 8
//第四次排序
//5 6 7 8 9
?
//int main(int argc,const char *argv[])
//{
//? ? int a[5]={9,7,8,5,6};
// ? ?
//? ? for (int i=0; i<5-1; i++) {
//? ? ? ? int k=i;
//? ? ? ? for (int j=i+1; j<5; j++) {
//? ? ? ? ? ? if (a[k]>a[j]) {
//? ? ? ? ? ? ? ? k=j;
//? ? ? ? ? ? }
//? ? ? ? }
//? ? ? ? if (k!=i) {
//? ? ? ? ? ? int temp = a[k];
//? ? ? ? ? ? a[k]=a[i];
//? ? ? ? ? ? a[i]=temp;
//? ? ? ? }
//? ? }
//
//? ? for (int i=0; i<5; i++) {
//? ? ? ? printf("%d ", a[i]);
//? ? }
//? ? putchar('\n');
//? ? return 0;
//}
?
//插入排序
//9 7 8 5 6
//第一次排序
//9 9 8 5 6
//7 9 8 5 6
//第二次排序
//7 9 9 5 6
//7 8 9 5 6
//第三次排序
//7 8 9 9 6
//7 8 8 9 6
//7 7 8 9 6
//5 7 8 9 6
//第四次排序
//5 7 8 9 9
//5 7 8 8 9
//5 7 7 8 9
//5 6 7 8 9
//
//int main(int argc,const char *argv[])
//{
//? ? int a[5]={9,7,8,5,6};
//? ? for (int i=1; i<5; i++) {
//? ? ? ? int temp = a[i];
//? ? ? ? int j=i-1;
//? ? ? ? while (j>=0 && a[j]>temp) {
//? ? ? ? ? ? a[j+1]=a[j];
//? ? ? ? ? ? j--;
//? ? ? ? }
//? ? ? ? a[j+1]=temp;
//? ? }
//? ? for (int i=0; i<5; i++) {
//? ? ? ? printf("%d ", a[i]);
//? ? }
//? ? putchar('\n');
// ? ?
//? ? return 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;
? ? ? ? }
//? ? ? ? //方法二
//? ? ? ? char ch;
//? ? ? ? if (scanf("%c", &ch), ch=='\n') {
//? ? ? ? ? ? break;
//? ? ? ? }
//? ? }
//? ? for (int i=0; i<cnt; i++) {
//? ? ? ? printf("%d ", a[i]);
//? ? }
//
//? ? return 0;
//}
?
//二維數組可以看成由一維數組元素組成的數組
//int a[3][2];
//
?
//二維數組的初始化
//方法一
//int main(int argc,const char *argv[])
//{
//? ? int a[3][2]={1,2,3,4,5,6};
// ? ?
//? ? for (int i=0; i<3; i++) {
//? ? ? ? for (int j=0; j<2; j++) {
//? ? ? ? ? ? printf("%d ", a[i][j]);
//? ? ? ? }
//? ? ? ? printf("\n");
//? ? }
//? ? return 0;
//}
?
//方法二
?
//int main(int argc,const char *argv[])
//{
//? ? int a[3][2]={{1,2},
// ? ? ? ? ? ? ? ? {3,4},
// ? ? ? ? ? ? ? ? {5,6}};
//
//? ? for (int i=0; i<3; i++) {
//? ? ? ? for (int j=0; j<2; j++) {
//? ? ? ? ? ? printf("%d ", a[i][j]);
//? ? ? ? }
//? ? ? ? printf("\n");
//? ? }
//? ? return 0;
//}
?
//打印楊輝三角
//1
//1 1
//1 2 1
//1 3 3 1
//1 4 6 4 1
//1 5 10 10 5 1
?
/*int main(int argc,const char *argv[])
{
? ? int a[10][10]={};
? ? for (int i=0; i<10; i++) {
?? ? ? ?
? ? ? ? for (int j=0; j<i; j++) {
? ? ? ? ? ? if (j==0 || j==i) {
? ? ? ? ? ? ? ? a[i][j]=1;
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? a[i][j]=a[i-1][j]+a[i-1][j-1];
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? for (int i=0; i<10; i++) {
? ? ? ? for (int j=0; j<i; j++) {
? ? ? ? ? ? printf("%4d", a[i][j]);
? ? ? ? }
? ? ? ? printf("\n");
? ? }
? ? return 0;
}*/
int main(int argc,const char *argv[])
{
? ? int a[10][10]={};
? ? for (int i=0; i<10; i++) {
?? ? ? ?
? ? ? ? for (int j=0; j<=i; j++) {
? ? ? ? ? ? if (j==0 || j==i) {
? ? ? ? ? ? ? ? a[i][j]=1;
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? a[i][j]=a[i-1][j]+a[i-1][j-1];
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? for (int i=0; i<10; i++) {
? ? ? ? printf("%*d", 26-2*i,a[i][0]);
? ? ? ? for (int j=1; j<=i; j++) {
? ? ? ? ? ? printf("%4d", a[i][j]);
? ? ? ? }
? ? ? ? printf("\n");
? ? }
? ? return 0;
}
?
轉載于:https://www.cnblogs.com/-yun/p/4256782.html
總結
- 上一篇: 后台服务器控件点击跳转另一页面显示本页面
- 下一篇: ibatis提示Unable to lo