编程题小练
1. 编程,统计在所输入的50个实数中有多少个正数、多少个负数、多少个零。
| #include <stdio.h> |
| #include <time.h> |
| void srand(unsigned seed); |
| int rand(void); |
| void getArr(int *a,int l){ |
| int i=0; |
| srand(time(NULL)); |
| printf("\n你生成的数组为:"); |
| while (i < l) printf("%d,",a[i++] = rand()%(2*l+1)-l); |
| } |
| |
| int main(){ |
| int a,b,c,i; |
| int arr[50]; |
| getArr(arr,50); |
| a=b=c=0; |
| for(i=0;i<50;i++) |
| if(arr[i]>0) a++; |
| else if(arr[i]<0) b++; |
| else c++; |
| printf("\n正数:%d\n负数:%d\n0:%d\n",a,b,c); |
| return 0; |
| } |

2. 编程,计算并输出方程X2+Y2=1989的所有整数解。
| #include <stdio.h> |
| #include <math.h> |
| #define NUM 1989.0 |
| |
| int main(){ |
| int x,y; |
| for(x=0;x<sqrt(NUM);x++) |
| for(y=0;y<sqrt(NUM);y++) |
| if(y*y+x*x==NUM) |
| printf("x:%d , y:%d\n",x,y); |
| return 0; |
| } |

3. 编程,输入一个10进制正整数,然后输出它所对应的八进制、十六进制数。
| #include <stdio.h> |
| |
| int main(){ |
| int num; |
| scanf("%d",&num); |
| printf("八进制:%o , 十六进制:%x",num,num); |
| return 0; |
| } |

4. 一个数如恰好等于它的因子之和,这个数就称为“完数”。编程序找出1000以内的所有完数,并输出其因子(6是一个"完数",它的因子是1,2,3)。
| #include <stdio.h> |
| int ispnum(int n){ |
| int i=1,sum=0; |
| for(;i<n;i++) |
| if(n%i==0) sum+=i; |
| return sum==n?1:0; |
| } |
| void print(int n){ |
| int i=1; |
| printf("%d是一个'完数',它的因子是:",n); |
| for(;i<n;i++) |
| if(n%i==0) printf("%d, ",i); |
| putchar(10); |
| } |
| int main(){ |
| int i; |
| for(i=1;i<1000;i++) |
| if(ispnum(i)) print(i); |
| return 0; |
| } |

5. 输入一个正整数,输出它的所有质数因子(如180的质数因子为 2、2、3、3、5)。
| #include <stdio.h> |
| int ispnum(int n){ |
| int i; |
| for(i=2;i<n;i++) |
| if(n%i==0) return 0; |
| return 1; |
| } |
| |
| void print(int n){ |
| int i; |
| for(i=2;i<n;i++) |
| if(n%i==0 && ispnum(i)) printf("%d, ",i); |
| } |
| |
| int main(){ |
| int num; |
| scanf("%d",&num); |
| print(num); |
| return 0; |
| } |

6. 输入20个整数存入一数组,输出其中能被数组中其它元素整除的那些数组元素。
7. 输入两个数组(数组元素个数自定),输出在两个数组中都出现的元素(如a[5]={2,3,4,5,6},b[6]={3,5,7,9,10,-1},则输出3、5)。
| #include <stdio.h> |
| #include <time.h> |
| #define M 5 |
| #define N 8 |
| void srand(unsigned seed); |
| int rand(void); |
| void getArr(int *a,int l){ |
| int i=0; |
| srand(time(NULL)); |
| printf("\n你生成的数组为:"); |
| while (i < l) printf("%d,",a[i++] = rand()%(2*l+1)-l); |
| } |
| |
| int find(int *a,int n,int num){ |
| int i; |
| for(i=0;i<n;i++) |
| if(a[i]==num) return 1; |
| return 0; |
| } |
| int main(){ |
| int a[M],b[N],i; |
| getArr(a,M); |
| getArr(b,N); |
| printf("\n共同存在的元素有:"); |
| for(i=0;i<N;i++) |
| if(find(a,M,b[i])) printf("%d, ",b[i]); |
| return 0; |
| } |

8. 输入两个数组(数组元素个数自定),输出在两个数组中都不出现的元素(如a[5]={2,3,4,5,6},b[6]={3,5,7,9,10,-1},则输出2、4、6、7、9、10、-1)。
| #include <stdio.h> |
| #include <time.h> |
| #define M 5 |
| #define N 8 |
| void srand(unsigned seed); |
| int rand(void); |
| void getArr(int *a,int l){ |
| int i=0; |
| srand(time(NULL)); |
| printf("\n你生成的数组为:"); |
| while (i < l) printf("%d,",a[i++] = rand()%(2*l+1)-l); |
| } |
| |
| int find(int *a,int n,int num){ |
| int i; |
| for(i=0;i<n;i++) |
| if(a[i]==num) return 1; |
| return 0; |
| } |
| int main(){ |
| int a[M]={2,3,4,5,6},b[N]={3,5,7,9,10,-1},i; |
| |
| |
| |
| printf("\n不共同存在的元素有:"); |
| for(i=0;i<N;i++) |
| if(!find(a,M,b[i])) printf("%d, ",b[i]); |
| for(i=0;i<M;i++) |
| if(!find(b,N,a[i])) printf("%d, ",a[i]); |
| return 0; |
| } |

9.编程,将字符数组S2中的全部字符拷贝到字符数组S1中(不用strcpy函数)。
| #include <stdio.h> |
| |
| void strcopy(char *s1,char *s2){ |
| while(*s1++=*s2++); |
| } |
| |
| int main(){ |
| char str[80]; |
| strcopy(str,"outstanding"); |
| puts(str); |
| return 0; |
| } |

10.给定年份year,判别该年份是否闰年(定义一个宏以判别该年份是否闰年)。
| #include <stdio.h> |
| #define ISLEAP(year) (year%400==0||year%4==0&&year%100?1:0) |
| |
| int main(){ |
| int year; |
| for(year=1896;year<2010;year+=4) |
| if(ISLEAP(year)) printf("%d 年是闰年\n",year); |
| return 0; |
| } |

11.输入一行小写字母后,将字母变成其下一字母(a变成b、b变成c、c变成d、…、x变成y、y变成z、z变成a)输出。
| #include <stdio.h> |
| |
| int main(){ |
| char str[80]; |
| int i; |
| for(i=0;(str[i]=getchar())!='\n';i++) |
| str[i]=='z'?str[i]='a':str[i]++; |
| str[i]='\0'; |
| puts(str); |
| return 0; |
| } |

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· Vue3状态管理终极指南:Pinia保姆级教程