重要知识点
闰年的判别条件是:该年年份能被4整除但不能被100整除,或者能被400整除。闰年的2月有29天。
素数(质数)的定义:大于 1 的自然数,除了 1 和它本身,不能被其他自然数整除。
关于数组:
三种声明方式
- 数据类型
数组名称[长度n] = {元素1,元素2…元素n};
- 数据类型
数组名称[长度n];
- 数据类型
数组名称[] = {元素1,元素2…元素n};
关于指针:
int* arr 和 int *arr 是等价写法。
1 2 3 4 5 6 7
| int arr[5] = {1,2,3,4,5};
int *p = arr;
|
返回指针类型的函数,return p而不是return *p,如果是后者,那么函数返回类型应该是int。
简单题
最大公约数和最小公倍数
1 2 3 4 5 6 7 8 9 10 11 12 13
| #include "stdio.h" void main(){ int m,n,zdgys,zxgbs,i; scanf("%d%d",&m,&n); for(i=m;i>=1;i--){ if(m%i==0 && n%i==0){ zdgys=i; break; } } zxgbs=m*n/zdgys; }
|
计算天数
题目:在一行输出日期是该年中的第几天。
输入:2009/03/02
输出:61
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| #include "stdio.h" int main() { int year,month,day; int dayinmonth[]={31,28,31,30,31,30,31,31,30,31,30,31}; int totaldays=0; int i=0; scanf("%d/%d/%d",&year,&month,&day); if((year%4==0&&year%100!=0)||(year%400==0)){ dayinmonth[1]=29; for(int i=0;i<month-1;i++){ totaldays=totaldays+dayinmonth[i]; } totaldays+=day; printf("%d\n",totaldays); }else{ for(int i=0;i<month-1;i++){ totaldays=totaldays+dayinmonth[i]; } totaldays+=day; printf("%d\n",totaldays); } return 0; }
|
输出整数各位数字
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| #include "stdio.h" int main(){ long N; int t,i=0,y=0; int a[99]; scanf("%ld",&N); t=N; if(N==0) printf("0 "); else{ while (N!=0) { a[i]=N%10; N/=10; i++; } for(y=i-1;y>=0;y--){ printf("%d ",a[y]); }}return 0; }
|
同类题目:将整型数x中每一位能被3整除的数依次取出。例如,当x中的数为:97653140时,px中的数为:9630;如果没有满足要求的数则输出x。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| void fun(int x, int *px) { int new_num=0,digit,a=1; int t=x; while(t>0){ digit=t%10; t/= 10; if(digit%3==0){ new_num+=digit*a; a=a*10; } } if(new_num==0){ *px = x; } else { *px=new_num; } }
|
同类题目:求一批整数中出现最多的个位数字
输入:3
1234 2345 3456
输出:3: 3 4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| #include "stdio.h" int main() { int n,i,M; scanf("%d", &n); int arr[n]; int count[10]; for(i=0;i<10;i++){ count[i]=0; } for(i=0;i<n;i++){ scanf("%d",&arr[i]); } for(i=0;i<n;i++){ if(arr[i]==0)count[0]++; while(arr[i]!=0){ count[arr[i]%10]++; arr[i]/=10; }} M=0; for(i=0;i<10;i++){ if(count[i]>M){ M=count[i]; } } printf("%d:",M); for (i=0;i<10;i++){ if(count[i]==M){ printf(" %d", i); } } printf("\n"); return 0; }
|
同类题目:输出一个整数的逆序数
1 2 3 4 5 6 7 8 9
| int reverse(int number) { int a=0; while(number!=0) { int b=number%10; a=a*10+b; number=number/10; } return a; }
|
找质数
假设 p 不是素数,那么它一定能分解为两个因数的乘积:p = a × b。
- 如果
a > sqrt(p) 且 b > sqrt(p),那么 a×b > sqrt(p)×sqrt(p) = p,与 a×b=p 矛盾;
- 因此,
a 和 b 中至少有一个≤sqrt (p)。