c语言学习笔记
1.时间差
#include <stdio.h>
int main()
{
int hour1,minute1;
int hour2,minute2;
scanf("%d %d",&hour1,&minute1);
scanf("%d %d",&hour2,&minute2);
int ih = hour2 - hour1;
int im = minute2 - minute1;
if (im<0){
im=60 + im;
ih --;
}
printf("时间差是%d小时%d分。\n",ih,im);
return 0;
}
2,找零计算器
#include<stdio.h>
int main()
{
int price = 0;
int bill = 0;
printf("请输入金额:");
scanf("%d",&price) ;
printf("请输入票面:");
scanf("%d",&bill);
if(bill>=price){
printf("应该找您: %d\n",bill-price);
}else{
printf("你的钱不够\n");
}
return 0;
}
3,成绩转换
#include<stdio.h>
int main()
{
printf("请输入成绩:");
int grade;
scanf("%d",&grade) ;
grade/=10;
switch(grade){
case 10:
case 9:
printf("A\n");
break;
case 8:
printf("B\n");
break;
case 7:
printf("C\n");
break;
case 6:
printf("D\n");
break;
default:
printf("F\n");
break;
}
return 0;
}
4,1000以内猜数游戏
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
srand(time(0));
int number =rand()%1000+1;
int count = 0;
int a = 0;
printf("我已经想好了一个1到1000之间的数。");
do {
printf("请猜这个1到1000之间的数:");
scanf("%d",&a);
count++;
if(a>number){
printf("你猜的数大了。");
}else if(a<number){
printf("你猜的数小了");
}
}while(a != number);
printf("太好了,你用了%d次就猜到了答案。\n",count);
return 0;
}
5.整数求逆
#include<stdio.h>
int main()
{
int x;
scanf("%d",&x);
int digit;
int ret = 0;
while(x>0){
digit = x%10;
printf("%d",digit);
ret = ret*10 + digit;
x/=10;
}printf("%d\n",ret);
return 0;
}
6.判断素数
#include<stdio.h>
int main()
{
int x;
scanf("%d",&x);
int i;
int a = 1;// x是素数
for(i=2;i<x;i++){
if(x % i == 0){
a=1;
}
}
if( a == 1 ){
printf("这个数是素数\n");
}else{
printf("这个数不是素数\n");
}
return 0;
}
7.输出前100个素数
#include<stdio.h>
int main()
{
int x;
int cnt = 0;
for ( x=2;cnt<100;x++)
{
int i;
int a = 1;
for ( i=2; i<x; i++){
if ( x % i == 0){
a = 0;
break;
}
}
if ( a = 1){
printf("%d",x);
cnt ++;
}
}
printf("\n");
return 0;
}
8.给定不超过6的正整数A,考虑从A开始的连续4个数字,请输出所有它们组成的无重复数字的3位数。
#include<stdio.h>
int main()
{
int a;
scanf("%d",&a);
int i,j,k;
int cnt =0;
i = a;
while( i<=a+3){
j = a;
while( j<=a+3){
k = a;
while( k<=a+3){
if( i!=j && i!=k && j!=k){
cnt++;
printf("%d%d%d",i,j,k);
if( cnt == 6){
printf("\n");
cnt = 0;
}else {
printf(" ");
}
}
k++;
}
j++;
}
i++;
}
return 0;
}
9.水仙花数
#include<stdio.h>
int main()
{
int n;
scanf("%d",&n);
int first = 1;
int i =1;
while( i<n){
first *=10;
i++;
}
//printf("first=%d\n",first);(找位数)
i = first;
while( i<first*10){
int t = i;
int sum = 0;
do {
int d = t %10;
t/=10;
int p = d;
int j = 1;
while (j<n){
p *= d;
j++;
}
sum += p;
} while( t>0);
if( sum == i){
printf("%d\n",i);
}
i++;
}
return 0;
}
10.九九乘法表
#include<stdio.h>
int main()
{
int n;
scanf("%d",&n);
int i,j;
i = 1;
while ( i<= n){
j = 1;
while ( j<=i){
printf("%d*%d=%d",j,i,i*j);
if (i*j<10){
printf(" ");
}else {
printf(" ");
}
j ++;
}
printf("\n");
i ++;
}
return 0;
}