C语言——基础编程例题9道
(都是比较简单的,就不加注释了,不懂评论区问我,别忘了关注点赞欧)
1.最大公约数:
#include<stdio.h>
int main(){
int x, y,number;
scanf("%d%d",&x,&y);
if (x > y){
x = x^y;
y = x^y;
x = x^y;
}
for (int i = 1; i <= x;i++){
if (x%i==0&&y%i==0){
number = i;
}
}
printf("%d\n",number);
return 0;
}
2.打印1000年到2000年之间的闰年:
#include<stdio.h>
int main(){
int year=1000;
while (year<= 2000){
if (year%400==0||(year%4==0&&year%100!=0)){
printf("%d ",year);
}
year++;
}
printf("\n");
return 0;
}
3.写一个代码:打印100~200之间的素数:
#include<stdio.h>
int main(){
int i, j;
for (i = 100; i <= 200;++i){
for (j = 2; j<i&&(i%j!=0);++j){}
if (j==i){
printf("%d ",i);
}
}
printf("\n");
return 0;
}
4.编写程序数一下 1到 100 的所有整数中出现多少个数字9:
#include<stdio.h>
int main(){
int num=0;
for (int i = 1; i <= 100;++i){
if ((i+1)%10==0){
num++;
}
}
printf("%d\n",num);
return 0;
}
5.计算1/1-1/2+1/3-1/4+1/5 …… + 1/99 - 1/100 的值,打印出结果:
#include<stdio.h>
int main(){
int i = 1;
float sum = 0;
for (float j = 1; j <= 100;++j){
sum = sum + i / j;
i = (-i);
}
printf("%.2f\n",sum);
system("pause");
return 0;
}
6.求10 个整数中最大值:
#include<stdio.h>
int main(){
int num[] = {10,20,0,8,9,5,99,100,1000,12};
int Max=num[0];
for (int i = 1; i < 10;++i){
if (Max<num[i]){
Max = num[i];
}
}
printf("%d\n",Max);
return 0;
7.在屏幕上输出9*9乘法口诀表:
#include<stdio.h>
int main(){
for (int i = 1; i < 10;++i){
for (int j = 1; j < 10;++j){
printf("%d*%d=%d ",i,j,i*j);
}
printf("\n");
}
return 0;
}
8.猜数字游戏:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main(){
int number[100],i,num;
srand(time(NULL));//生成种子
for (i = 0; i < 100;++i){
number[i] = rand()%10+10;//生成20以内随机数
}
i = rand() % 100;//随机抽取100以内的下标
printf("请输入所猜的一位20以内的数:");
scanf("%d",&num);
if (number[i]==num){
printf("找到了,为%d\n",number[i]);
}
else {
printf("没找到!\n");
}
system("pause");
return 0;
}
9.编写代码在一个整形有序数组中查找具体的某个数:
#include<stdio.h>
int main(){
int number;
int num[] = {1, 8 ,11,20,60,100,1211};
int len = sizeof(num) / sizeof(num[0]);
int left=0, right=len-1,mid;
scanf("%d",&number);
while (left<=right){
mid = (left + right) / 2;
if (number>num[mid]){
left = mid + 1;
}
else if (number<num[mid]){
right = mid - 1;
}
else{
printf("找到了,在数组中的下标为%d\n",mid);
break;
}
if (left>right){
printf("没找到!\n");
}
}
return 0;
}
以上是我自己写的,全部可执行,仅供参考,不喜勿喷,喜欢的话,关注,点赞一下呦。