赞
踩
#include <stdio.h> #define MAX_N 100 struct Student{ char name[20]; char id[10]; float ma,ch,total; } stu[MAX_N]; void main(){ int n=4,i,j; printf("请输入学生人数n=\n"); scanf("%d",&n); printf("请输入学生信息:\n"); for(i=0;i<n;i++){ scanf("%s %s %f %f", &stu[i].name,&stu[i].id,&stu[i].ma,&stu[i].ch); stu[i].total=(stu[i].ma+stu[i].ch)/2; } for(i=0;i<n-1;i++){ for(j=0;j<n-1-i;j++){ if(stu[j].total>stu[j+1].total){ struct Student mid; mid=stu[j]; stu[j]=stu[j+1]; stu[j+1]=mid; } } } for(i=0;i<n;i++) printf("%s %s %.2f\n", stu[i].name,stu[i].id,stu[i].total); }
#include <stdio.h>
void main() {
int year;
printf("请输入年份(正整数):");
scanf("%d", &year);
(year%4==0&&year%100!=0||year%400==0)?printf("%d年是闰年", year):printf("%年是平年", year);
}
#include <stdio.h> void main() { int year,month,day; scanf("%d %d", &year, &month); switch(month){ case 1: case 3: case 5: case 7: case 8: case 10: case 12: day = 31; break; case 4: case 6: case 9: case 11: day = 30; break; case 2: day=(year%4==0&&year%100!=0||year%400==0)?29:28; } printf("%d年%d月有%d天", year, month, day); }
#include <stdio.h> #define MAX_N 100 void main() { float nums[MAX_N]; int n,i; float max,avg; printf("请输入数组个数n="); scanf("%d",&n); for (i=0;i<n;i++) { scanf("%f",&nums[i]); avg+=nums[i]; } max=nums[0]; for(i=0;i<n;i++) (max<nums[i])?(max=nums[i]):max; printf("max=%.2f, avg=%.2f",max,avg/n); }
#include <stdio.h>
#define MAX_N 100
int Mylen(char *s){
return (*s=='\0')?0:1+Mylen(++s);
}
void main() {
char strs[MAX_N];
printf("请输入字符串:");
gets(strs);
printf("len%s=%d",strs,Mylen(strs));
}
函数传递
#include <stdio.h> #include <math.h> int total=0; int prime(int num) { // 6n- 6n+1 6n+2- 6n+3- 6n+4- 6n+5 if (num==2||num==3) return ++total; else if(num%6!=1&&num%6!=5) return 0; int i,sq=sqrt(num); for (i=5;i<sq+1;i+=6) { if (num%i==0||num%(i+2)==0) return 0; } return ++total; } void main() { int i; for (i=2;i<=100;i++) { if (prime(i)){ printf("%d\t", i); total%5==0?printf("\n"):0; } } }
#include <stdio.h> #include <math.h> void main() { int i,j,isPrime=1; for (i=2;i<=100;i++){ if (i==2||i==3) isPrime=1; else if (i%6!=1&&i%6!=5) isPrime=0; else { for (j=5;j<sqrt(i)+1;j+=6){ if (i%j==0||i%(j+2)==0){ isPrime=0; break; } } } (isPrime==1)?printf("%d\t", i):(isPrime=1); } }
#include <stdio.h>
#include <math.h>
void main() {
int i;
for (i=100;i<1000;i++) (i==pow(i/100,3)+pow(i%10,3)+pow(i/10%10,3))?printf("%d\t", i):0;
}
#include <stdio.h> #define MAX_N 100 void main() { int n,i,j,a[MAX_N]; printf("请输入数组长度n="); scanf("%d",&n); for (i=0;i<n;i++) scanf("%d", &a[i]); for (i=0;i<n-1;i++) { for (j=0;j<n-1-i;j++){ if (a[j]>a[j+1]){ a[j]=a[j]^a[j+1]; a[j+1]=a[j+1]^a[j]; a[j]=a[j]^a[j+1]; } } printf("%d\t",a[n-1-i]); } printf("%d",a[n-1-i]); }
字符串仅通过赋值修改,否则使用strcpy()函数,指针数组不能修改字符串
指针数组字符串不可变,只能修改指针指向,不能修改字符串交换
#include <stdio.h> #include <string.h> void sort(char *m[], int n){ char *t; int i,j; for(i=0;i<n-1;i++){ for(j=0;j<n-1-i;j++){ if(strcmp(m[j],m[j+1])>0){ t=m[j]; m[j]=m[j+1]; m[j+1]=t; } } } } void main() { char *f[]={"peach", "grape", "pear", "banana", "apple"}; int n=5,i; sort(f,n); for(i=0;i<n;i++) printf("%s\n",f[i]); }
二维自定义输入字符串
#include <stdio.h> #include <string.h> #define MAX_N 100 void sort(char strs[][10], int n){ int i,j; char mid[10]; for(i=0;i<n-1;i++){ for(j=0;j<n-i-1;j++){ if(strcmp(strs[j],strs[j+1])>0){ strcpy(mid,strs[j]); strcpy(strs[j],strs[j+1]); strcpy(strs[j+1],mid); } } } } void main() { char strs[MAX_N][10]; int n,i; printf("请输入字符串数组个数n="); scanf("%d",&n); for(i=0;i<n;i++) scanf("%s",strs[i]); sort(strs,n); for(i=0;i<n;i++) printf("%s\t",strs[i]); }
void main(){
int i,j;
for (i=1;i<=9;i++){
for (j=1;j<=i;j++) {
printf("%d*%d=%-2d\t",j,i,i*j);
}
printf("\n");
}
}
#include <stdio.h> #include <string.h> #define MAX_N 100 void reverse(char *left, char *right){ char mid; while(left<right){ mid = *left; *left = *right; *right = mid; left++; right--; } } void main() { char strs[MAX_N]; printf("请输入字符串strs="); // welcome to the world of c! // scanf()录入会导致空格判定结束只能录进一个字符串 gets(strs); int len=strlen(strs); reverse(strs,strs+len-1); // !c fo dlrow eht ot emoclew printf("%s\n",strs); char *start=strs,*end=strs; while(*start!='\0'){ while(*end!=32&&*end!='\0') end++; reverse(start,end-1); end++; start=end; } // c! of world the to welcome printf("%s",strs); }
#include <stdio.h>
int fib(int n){
// n为项数
return (n==1||n==2)?1:fib(n/2 + 1)*fib(n-n/2)+fib(n/2)*fib(n-n/2 - 1);
}
void main(){
int n,i=1;
printf("请输入n=");
scanf("%d", &n);
while(fib(i)<=n) printf("%d\t",fib(i++));
}
// 该递归属于多分支递归,会造成栈溢出。
int Fib(int x)
{
if (x >= 2) return Fib(x - 1) + Fib(x - 2);
else if (x == 1) return 1;
else return 0;
}
#include <stdio.h>
#define MAX_N 100
void main(){
char strs[MAX_N];
int i=0;
printf("请输入字符串");
gets(strs);
while(strs[i]!='\0') {
if(strs[i]>=65&&strs[i]<=90) strs[i]+=32;
printf("%c",strs[i++]);
}
}
// 打表统计 #include <stdio.h> #define MAX_N 100 void main() { char s[MAX_N]; int total[MAX_N]={0}; gets(s); int i=0; while(s[i]!='\0'){ if (s[i]>=65&&s[i]<=90){ total[((int)s[i])-65]++; } else if (s[i]>=97&&s[i]<=122){ total[((int)s[i])-71]++; } else if (s[i]>=48&&s[i]<=57){ total[((int)s[i])+4]++; } else total[62]++; i++; } for(i=0;i<=62;i++) { if(total[i]!=0) { if(i<=25)printf("%-2c有%-2d个\n",i+65,total[i]); else if(i<=51)printf("%-2c有%-2d个\n",i+71,total[i]); else if(i<=61)printf("%-2c有%-2d个\n",i-4,total[i]); else printf("其它字符串有%-2d个\n",total[i]); } } }
#include <stdio.h>
int Tri(int r, int c) { //杨辉三角算法函数
return (c==1||c==r)?1:Tri(r-1,c-1)+Tri(r-1, c);
}
void main() {
int i, j, n;
printf("请输入杨辉三角形的行数(1 ~ 20):");
scanf("%d", &n);
for(i=1;i<=n;i++) { // 输出n行
for(j=0;j<n-i;j++) printf("%c%c%c",32,32,32); //每行前面补空格,显示成等腰三角形
for(j=1;j<=i;j++) printf("%-6d", Tri(i, j)); //计算并输出杨辉三角形
printf("\n");
}
}
n = 0; n! = 1;
n > 0; n! = n*(n-1);
#include <stdio.h>
int kn(int n){
return (n==0)?1:n*kn(n-1);
}
void main(){
int n,i;
printf("请输入阶乘数n=");
scanf("%d", &n);
printf("%d!=",n);
for(i=n;i>0;i--) (i!=1)?printf("%d*",i):printf("%d",i);
printf("=%d",kn(n));
}
#include<stdio.h> void move(char A, char C, int n){ printf("把第%d个圆盘从%c--->%c\n", n, A, C); } void HanoiTower(char A, char B, char C, int n){ if (n == 1) move(A, C, n); else{ //将n-1个圆盘从A柱借助于C柱移动到B柱上 HanoiTower(A, C, B, n - 1); //将A柱子最后一个圆盘移动到C柱上 move(A, C, n); //将n-1个圆盘从B柱借助于A柱移动到C柱上 HanoiTower(B, A, C, n - 1); } } void main(){ int n = 0; printf("输入A柱子上的圆盘个数:"); scanf("%d", &n); //将n个圆盘从A柱借助于B柱移动到C柱上 HanoiTower('A', 'B', 'C', n); }
#include <stdio.h> #include <string.h> #define MAX_N 100 void main() { char n[MAX_N]; printf("请输入数字n="); gets(n); int len=strlen(n),i,flag=1; for(i=0;i<len/2+1;i++){ if(n[i]!=n[len-1-i]) { flag=0; break; } } (flag==1)?printf("%s是回文数",n):printf("%s不是回文数",n); }
#include <stdio.h>
void main() {
char *month[]={"Month:","January","February","March","April",
"May","June","July","August",
"September","October","November","December",};
int mon;
scanf("%d", &mon);
printf("%s",month[mon]);
}
#include <stdio.h>
void main(){
int day=10,total=1;
while(day>0){
printf("第%-2d天有%4d个桃子\n",day--,total);
total=(total+1)*2;
}
}
#include <stdio.h>
void main(){
int n;
scanf("%d",&n);
while(n>0){
(n%10!=0)?printf("%d",n%10):0;
n/=10;
}
}
|data0|next0|->|data1|next1|->NULL
单链表操作:
>|data0|next0|->|data2|next2|===>|data1|next1|->NULL
>|data0|next0|->|data1|next1|===>|data2|next2|===>NULL
|data0|next0|->->|data2|next2|->NULL
|data1|next1|===free
头节点(node0):保存链表元素个数,实际从第二链开始
#include <stdio.h> #include <stdlib.h> typedef struct Node { int data; struct Node* next; } Node; Node* initList() { Node* list = (Node*)malloc(sizeof(Node)); list->data = 0; list->next = NULL; return list; } void headInsert(Node* list, int data) { Node* node = (Node*)malloc(sizeof(Node)); node->data = data; node->next = list -> next; list->next = node; list->data++; } void tailInsert(Node* list, int data) { Node* head = list; Node* node = (Node*)malloc(sizeof(Node)); node->data = data; node->next = NULL; while (list->next) { list = list->next; } list->next = node; head->data++; } void Insert(Node* list) { int s, i; scanf("%d", &s); for ( i=0;i<s;i++ ) { int mid; scanf("%d", &mid); headInsert(list, mid); } } void deleteNode(Node* list, int data) { Node* current = list->next; Node* pre = list; while ( current ) { if ( current->data==data ) { pre->next = current->next; free(current); break; } pre = current; current = current->next; } list->data--; } void printfList(Node* list) { list = list->next; while ( list ) { printf("%d ", list->data); list = list->next; } printf("\n"); } int main() { Node* list = initList(); tailInsert(list, 1); tailInsert(list, 2); tailInsert(list, 3); printfList(list); // Insert(list); // printfList(list); return 0; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。