1、 输出3个数的最大数。
#include <stdio.h>
int main(int argc,const char * argv[]){
int a,b,c,max;
printf("请输入三个数字:\n");
scanf("%d%d%d",&a,&b,&c);
if(a>b) max=a;
else max=b;
if(max<c)max=c;
printf("%d\n",max);
}
2、打印10个数的最大值和最小值。
#include <stdio.h>
int main(int argc,const char * argv[]){
int a[10],i,max,min;
printf("请输入十个数字:\n");
for (i=0; i<10; i++)
scanf("%d",&a[i]);
max=a[0];
min=a[0];
for(i=0;i<9;i++)
if(a[i]>max)max=a[i];
else min=a[i];
printf("最大数是%d,最小数是%d\n",max,min);
return 0;
}
3、找出数组中最小的数和它的下标,然后把它和数组中最前面的元素对换位置。
#include<stdio.h>
int main(int argc,char * argv[]){
int i ,a[10];
printf("请输入十个数字:");
for (i=0;i<10;i++)
scanf("%d",&a[i]);
int t=0;//用t来记录最小值的下标
for (i=0;i<10;i++){
if(a[i]<a[t]) t=i;}
printf("最小数值是%d,下标是%d\n",a[t],t);
//最小数和第一个元素互换位置
int temp;
temp=a[0];
a[0]=a[t];
a[t]=temp;
}
4、二维整型数组中,每一行都有一个最大值,编程求出这些最大值以及它们的和。
#include <stdio.h>
int main(int argc,const char * argv[]){
int sum;
int array[][3] = {1,2,3,4,5,6,7,8,9};
for(int i = 0; i < 3; i++){
for(int j = 0; j<3; j++){
if(array[i][j] > array[i][0]){
array[i][0] = array[i][j];
}
}
}
sum = array[0][0] + array[1][0] + array[2][0];
printf("第一行的最大值是%d\n第二行的最大值是%d\n第三行的最大值是%d\n最大值的和是%d\n",array[0][0],array[1][0],array[2][0],sum);
return 0;
}
- switch语句打印周一至周日。
#include <stdio.h>
int main(int argc, const char * argv[]) {
int number;
printf("请输入1~7之间的任意一个数字:\n");
scanf("%d",&number);
while ((number>7||number<1)) {
printf("输入数据不符合要求,请重新输入:\n");
scanf("%d",&number);
}
switch (number) {
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
break;
case 4:
printf("Thursday\n");
break;
case 5:
printf("Friday\n");
break;
case 6:
printf("Saturday\n");
break;
case 7:
printf("Sunday\n");
}
return 0;
}
- 求n的阶乘
int main(int argc,char * argv[]){
int n,i;
printf("请输入一个整数\n");
scanf("%d",&n);
for(i=n-1;i>0;--i)
n=n*i;
printf("此数的阶乘是%d\n",n);
return 0;
}
- 3025这个数具有一种独特的性质:将它平分为两段,即30和25,使之相加后求平方,即(30+25),恰好等于3025本身。请求出具有这样性质的全部四位数。
#include<stdio.h>
int main(int argc,char * argv[]){
int num,num2,num3;
for(num=1000;num<10000;num++){
num2=num/100;
num3=num%100;
int sum=num3+num2;
sum*=sum;
if(sum==num)
printf("%d是这样的数字\n",num);}
return 0;}
8、从键盘输入一个数字,用循环语句判断该数的位数:
#include <stdio.h>
int main(int argc,const char * argv[]){
int num,count = 1;
printf("请输入一个数字:\n");
scanf("%d",&num);
while(num / 10 != 0){
num = num / 10;
count ++;
}
printf("该数的位数是%d\n",count);
return 0;
}
- 10位评委打分。
#include<stdio.h>
int main(int argc,char * argv[]){
printf("请10位评委打分:\n");
int array[10],max,min,sum,i;
for(i=0;i<10;i++)
{
scanf("%d",&array[i]);
sum+=array[i];
}
max=array[0];
min=array[0];
for(i=0;i<10;i++)
{
if(array[i]>max) max=array[i];
if(array[i]<min) min=array[i];
}
sum-=max+min;
float average=sum/10.0f;
printf("除掉一个最高分和最低分,其平均分是:%f\n",average);
return 0;
}
9、输入某年某月某日,判断这一天是这一年的第几天。
#include<stdio.h>
int main(int argc,char * argv[]){
int year,month,day;
int sum=0;
int flog;
printf("请输入年月日(yyyy-mm-dd)\n");
scanf("%4d-%2d-%2d",&year,&month,&day);
if(( year%4==0 && year%100!=0 ) || year%400==0)
flog=1;
else
flog=0;
while((year<0)||(month>12||month<0)||(day<0||day>31)||((month%2==0)&&(day==31))||((flog==1)&&(month==2)&&(day>28)))
{
printf("input errro!\n");
printf("请输入年月日(yyyy-mm-dd)\n");
scanf("%4d-%2d-%2d",&year,&month,&day);
}
switch (month-1)
{
case 12: sum+=31;
case 11: sum+=30;
case 10: sum+=31;
case 9: sum+=30;
case 8: sum+=31;
case 7: sum+=31;
case 6: sum+=30;
case 5: sum+=31;
case 4: sum+=30;
case 3: sum+=31;
case 2:
if(flog==1)
sum+=28;
else
sum+=30;
case 1: sum+=31;break;
default:printf("非法月份\n");
break;
}
sum=sum+day;
if(flog==1)
printf("%d is leap year!\n",year);
printf("这一天是这年的第%d天",sum);
}
10、水花仙数。
#include <stdio.h>
int main(int argc, const char * argv[]) {
int num;
for(num=100;num<1000;num++)
{
int firstNum,secondNum,thirdNum;
firstNum=num/100;
secondNum=(num%100)/10;
thirdNum=num%10; if(num==firstNum*firstNum*firstNum+secondNum*secondNum*secondNum+thirdNum*thirdNum*thirdNum)
printf("%d是水花仙数\n",num);
}
return 0;
}
11、输出字符串中字母,数字,空格和其他字符的个数。
#include <stdio.h>
int main(int argc, const char * argv[]) {
int num_digital = 0;
int num_char = 0;
int num_space = 0;
int i;
char c[100] = "faj f afhka123";
//gets(c);
for (i=0; i<100; i++) {
if (c[i] == ' ') {
num_space ++;
}
if ((c[i] >= 'A' && c[i] <= 'Z')|| (c[i] >= 'a' && c[i] <= 'z')) {
num_char ++;
}
if (c[i] >= '0' && c[i] <= '9') {
num_digital ++;
}
}
printf("数字个数是%d\n字母个数是%d\n空格个数是%d\n",num_digital,num_char,num_space);
return 0;
}
- 逆序输出。
#include<stdio.h>
int main(int argc,const char * argv[]){
int num,count;
printf("请输入一个不多于5位的数:\n");
scanf("%d",&num);
while (num > 0) {
printf("%d\n",num % 10);
num = num / 10;
count++;
}
printf("该数是%d位\n",count);
return 0;
}
13、求s=a+aa+aaa+aaaa+aa…a的值,其中a是一个数字。例如2+22+222+2222+22222
#include<stdio.h>
int main(int argc ,char * argv[]){
int num1,num2,sum;
printf("请输入要计算的数字:\n");
scanf("%d",&num1);
printf("请输入要计算的最大个数:\n");
scanf("%d",&num2);
int num3 = num1;
for (int i=1; i<=num2; i++) {
sum += num1;
num1 = num1 * 10 + num3;
}
printf("sum = %d\n",sum);
return 0;
}
14、“完数”,一个数如果恰好等于它的因子之和
#include<stdio.h>
int main(int argc , char * argv[]){
int i,j;
int sum ;
for (i=2; i<=1000; i++) {
sum = 0;
for (j=1; j<i; j++) {
if (i%j == 0) {
sum += j;
}
}
if (sum == i) {
printf("%d \n",i);
}
}
return 0;
}
15、12321是回文数,个位与万位相同,十位与千位相同。
#include <stdio.h>
int main(int argc, const char * argv[]) {
int ge,shi,bai,qian,wan;
int i;
for (i=10000; i<100000; i++) {
ge = i%10;
shi = (i%100)/10;
bai = (i%1000)/100;
qian = (i%10000)/1000;
wan = i/10000;
if (ge==wan && shi==qian) {
printf("%d是回文数\n",i);
}
}
return 0;
}
16、无重复数字的三位数?有1、2、3、4个数字
#include<stdio.h>
int main(int argc ,char * argv[]){
int count;
for (int i=1; i<=4; i++) {
for (int j=1; j<=4; j++) {
for (int k=1; k<=4; k++) {
if (k != j && k != i && i !=j) {
printf("%d%d%d\n",i,j,k);
count++;
}
}
}
}
printf("符合要求的总过有%d个\n",count);
return 0;
}
17、利用循环打印菱形和空心菱形
* *
*** * *
***** * *
******* * *
***** * *
*** * *
* *
*/
#include<stdio.h>
int main(int argc ,char * argv[]){
int h,l,t,max;//h代表行,l代表要打印*,t代表要打印空格
printf("请输入一个数字:\n");
scanf("%d",&max);
for (h=1; h<=max; h++) {
for (t=max; t>h; t--) {
printf(" ");
}
for (l=1; l<=h; l++) {
printf(" *");
}
printf("\n");
}
for (h=1; h<=max-1; h++) {
for (t=1; t<=h; t++) {
printf(" ");
}
for (l=max-1; l>=h; l--) {
printf(" *");
}
printf("\n");
}
return 0;
}
18、利用函数传指针,来排序三个数字。
#include <stdio.h>
void input(int *num ){
printf("请输入三个数字:\n");
for(int i=0;i<3;i++){
scanf("%d",(num+i));
}
}
int* deal(int *p){
int a ;
int *temp = &a;
if(*p > *(p+1)){
*temp = *p;
*p = *(p+1);
*(p+1) = *temp;
}
if(*p > *(p+2)){
*temp = *p;
*p = *(p+2);
*(p+2) = *temp;
}
if(*(p+1) > *(p+2)){
*temp = *(p+1);
*(p+1) = *(p+2);
*(p+2) = *temp;
}
return p;
}
void print(int *p){
for(int i=0;i<3;i++){
printf("%d ",*(p+i));
}
}
int main(){
int arr[3];
input(arr);
int *a = deal(arr);
print(a);
return 0;
}
19、求最大公因子
//辗转相除法
#include<stdio.h>
int main(int argc,const char * argv[]){
int num1,num2,yu = 0;
printf("输入\n");
scanf("%d%d",&num1,&num2);
while (num2 != 0)
{
yu = num1 % num2;
num1 = num2;
num2 = yu;
}
printf("%d\n",num1);
return 0;
}
20、计算字符串长度。
#include<stdio.h>
#include<stdlib.h>
int length(char *str){
int length;
while (*str != '\0') {
length ++;
str++;
}
return length;
}
int main(int argc,const char * argv[]){
printf("请输入长度不多于20的字符串:\n");
char *str = (char *) malloc(20);
scanf("%s",str);
int len = length(str);
free(str);
str = NULL;
printf("该字符串的有效长度是:%d字节\n",len);
return 0;
}
21、字符串回文:
第一种方式:
int fun( char str[] )
{
long i,j;
long n = strlen(str);
for(i=0,j=n-1; i<=(n-1)/2; i++,j--){
if(str[i] != str[j]){
break;
}
}
if(i>j) {
return 1;
}
return 0;
}
int main(int argc,const char * argv[]){
printf("输入\n");
char a[80];
scanf("%s",a);
if ( fun(a) == 1 ) {
printf( "回文\n");
}else{
printf("非回文\n");
}
return 0;
}
第二种方式:
#include<stdio.h>
int main(int argc,const char * argv[]){
printf("输入任意长度的字符串\n");
char a[100];
scanf("%s",a);
unsigned long length = strlen(a);
int i;
for (i=0; i<length/2; i++) {
if (a[i] == a[length-1-i]) {
continue;
}else{
break;
}
}
if (i == length/2) {
printf("是回文...\n");
}else{
printf("非回文...\n");
}
return 0;
}
22、连接任意两个字符串,不使用strcat。
#include<stdio.h>
#include<string.h>
#include <stdlib.h>
//链接任意两个字符串
char * Lianjie(char *str1,char *str2){
char *str3 = str1;
while (*str1++ != '\0');//找到str1末尾的‘\0’
str1--; // 指向前一个地址,用来连接
while (*str2 != '\0') {
*str1++ = *str2 ++;
}
str1 = '\0';
return str3;
}
int main(int argc,const char * argv[]){
printf("输入第一个字符串\n");
char *str1 = (char*)malloc(30);
scanf("%s",str1);
printf("输入第二个字符串\n");
char *str2 = (char*)malloc(15);
scanf("%s",str2);
printf("链接之后的字符串是:\n");
printf("%s\n",Lianjie(str1,str2));
return 0;
}
23、删除所有非字母元素
#include<stdio.h>
#include <stdlib.h>
void dele(char *str){
for(int i = 0;str[i]!='\0';i++){
if(!((str[i]>='a'&&str[i]<='z')||(str[i]>='A'&&str[i]<='Z'))){
for(int j = i;str[j]!='\0';j++){
str[j] = str[j+1];
}
i--;
}
}
}
int main(int argc, const char * argv[]) {
char *str = (char*)malloc(50);
printf("请输入一个字符串:\n");
scanf("%s",str);
dele(str);
printf("%s\n",str);
return 0;
}
24、删除所有指定的字符
#include<stdio.h>
#include <stdlib.h>
void dele(char *ach,char *str){
for(int i = 0;str[i]!='\0';i++){
if(str[i] == *ach){
for(int j = i;str[j]!='\0';j++){
str[j] = str[j+1];
}
i--;
}
}
}
int main(int argc, const char * argv[]) {
char *str = (char*)malloc(50);
char *ach = (char*)malloc(2);
printf("请输入一个字符串:\n");
scanf("%s",str);
printf("要删除哪一个字符:\n");
scanf("%s",ach);
dele(ach,str);
printf("删除该字符后为:\n");
printf("%s\n",str);
free(str);
free(ach);
str = NULL;
ach = NULL;
return 0;
}
24、一年中的第几天
#include<stdio.h>
int main(int argc,char * argv[]){
int year,month,day;
int sum=0;
int flog;
printf("请输入年月日(yyyy-mm-dd)\n");
scanf("%4d-%2d-%2d",&year,&month,&day);
if(( year%4 == 0 && year % 100 != 0 ) || year % 400 == 0)
flog = 1;
else
flog = 0;
while((year < 0)||(month > 12||month < 0)||(day < 0||day > 31)||((month % 2 == 0)&&(day == 31))||((flog == 1)&&(month == 2)&&(day > 28))){
printf("input errro!\n");
printf("请输入年月日(yyyy-mm-dd)\n");
scanf("%4d-%2d-%2d",&year,&month,&day);
}
switch (month-1)
{
case 12: sum += 31;
case 11: sum += 30;
case 10: sum += 31;
case 9: sum += 30;
case 8: sum += 31;
case 7: sum += 31;
case 6: sum += 30;
case 5: sum += 31;
case 4: sum += 30;
case 3: sum += 31;
case 2:
if(flog == 1){
sum += 28;
} else{
sum += 30;
}
case 1: sum += 31; break;
default:printf("非法月份\n");
break;
}
sum = sum + day;
if(flog == 1)
printf("%d is leap year!\n",year);
printf("这一天是这年的第%d天",sum);
}