Spring學習筆記(2)
阿新 • • 發佈:2022-04-20
#include <stdio.h> #define N 4 int main() { int a[N]={2,0,2,2}; char b[N]={'2','0','2','2'}; int i; printf("sizeof(int)=%d\n",sizeof(int)); printf("sizeof(char)=%d\n",sizeof(char)); printf("\n"); for(i=0;i<N;++i) printf("%p:%d\n",&a[i],a[i]); printf("\n"); for(i=0;i<N;++i) printf("%p:%c\n",&b[i],b[i]); printf("\n"); printf("a=%p\n",a); printf("b=%p\n",b); return 0; }
1.是連續的,4個位元組
2.是連續的,一個位元組
3。一樣
#include <stdio.h > #define N 2 #define M 3 int main() { int a[N][M]={{1,2,3},{4,5,6}}; char b[N][M]={{'1','2','3'},{'4','5','6'}}; int i,j; for(i=0;i<N;++i) for(j=0;j<M;++j) printf("%p:%d\n",&a[i][j],a[i][j]); printf("\n"); for(i=0;i<N;++i) for(j=0;j<M;++j) printf("%p:%c\n",&b[i][j],b[i][j]); return 0; }
1.是,4位元組
2.是 1位元組
#include <stdio.h> int days_of_year(int year,int month,int day); int main() { int year,month,day; int days; while(scanf("%d%d%d",&year,&month,&day)!=EOF) { days=days_of_year(year,month,day); printf("%4d-%02d-%02d是這一年的第%d天.\n\n",year,month,day,days); } return 0; } int days_of_year(int year,int month,int day) { int i,t=0; if(year%4==0&&year%100!=0 || year%400==0) { int a[12]={31,29,31,30,31,30,31,31,30,31,30,31}; for(i=0;i<month-1;i++) t=t+a[i]; t=t+day; } else { int a[12]={31,30,31,30,31,30,31,31,30,31,30,31}; for(i=0;i<month-1;i++) t=t+a[i]; t=t+day; } return t; }
#include <stdio.h> #define N 5 void input(int x[],int n); void output(int x[],int n); double average(int x[],int n); void sort(int x[],int n); int main() { int scores[N]; double ave; printf("錄入%d個分數:\n",N); input(scores, N); printf("錄入%d個分數:\n",N); output(scores, N); printf("\n輸出課程分數處理:計算均分、排序...\n"); ave=average(scores, N); sort(scores, N); printf("\n輸出課程均分:%.2f\n",ave); printf("\n輸出課程分數(高->低):\n"); output(scores, N); return 0; } void input(int x[],int n) { int i; for(i=0;i<n;i++) scanf("%d",&x[i]); } void output(int x[],int n) { int i; for(i=0;i<n;i++) printf("%d",x[i]); printf("\n"); } double average(int x[],int n) { int sum=0,i; double ave; for(i=0;i<n;i++) { sum=sum+x[i]; } ave=sum*1.0/N; return ave; } void sort(int x[],int n) { int i,j,t; for(j=1;j<n;j++) { for(i=0;i<n;i++) { if(x[i]<x[i+1]) { t=x[i]; x[i]=x[i+1]; x[i+1]=t; } } } }
#include <stdio.h> void dec2n(int x,int n); int main() { int x; printf("輸入一個十進位制整數:"); scanf("%d",&x); dec2n(x,2); dec2n(x,8); dec2n(x,16); return 0; } void dec2n(int x,int n) { char a[100]; int i,k,j=0; for(i=0;x!=0;i++,j++) { k=x%n; if(k>=10) { a[i]='A'+k-10; } else { a[i]='0'+k; } x=x/n; } for(i=j-1;i>=0;i--) { printf("%c",a[i]); } printf("\n"); }
#include <stdio.h> int main() { int n,i,j; printf("Enter n:"); while(scanf("%d",&n)!=0) { for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { if(i==1) { printf("%d",i); } else { if(j<=i) { printf("%d",j); } else { printf("%d",i); } } } printf("\n"); } } return 0; }
#include <stdio.h> #define N 80 int main() { int i,j; char t; char views1[N]="hey,c,i hate u."; char views2[N]="hey,c,i love u."; printf("otiginal views:\n"); printf("views1:"); for(i=0;i<N;i++) printf("%c",views1[i]); printf("\nviews2:"); for(j=0;j<N;j++) printf("%c",views2[j]); printf("\n"); printf("swapping...\n"); for(i=0;i<N;i++) for(j=0;j<N;j++) if(i=j) { t=views1[i]; views1[i]=views2[i]; views2[i]=t; } printf("views1:"); for(i=0;i<N;i++) printf("%c",views1[i]); printf("\nviews2:"); for(j=0;j<N;j++) printf("%c",views2[j]); printf("\n"); return 0; }
#include <stdio.h> #include <string.h> #define N 5 #define M 20 void bubble_sort(char str[][M],int n); int main() { char name[][M]={"Bob","Bill","Joseph","Taylor","George"}; int i; printf("輸出初始名單:\n"); for(i=0;i<N;i++) printf("%s\n",name[i]); printf("\n排序中...\n"); bubble_sort(name,N); printf("\n按字典序輸出名單:\n"); for(i=0;i<N;i++) printf("%s\n",name[i]); return 0; } void bubble_sort(char str[][M],int n) { int j,i; char t[M]; for(j=1;j<=n;j++) { for(i=0;i<n;i++) { if(strcmp(str[i],str[i+1])>0) { strcpy(t,str[i]); strcpy(str[i],str[i+1]); strcpy(str[i+1],t); } } } }