用于自己温习的代码,不排版、注释、回复,有需要的可以自取。
一、函数题
斐波那契数列递归法二
int fib( int n ){
int m=1,h=1,s;
if(n==1||n==2){
return 1;
}
else{
for(int i=1;i<=n-2;i++){
s = m+h;
m = h;
h = s;
}
return s;
}
}
使用函数输出水仙花数
#include<math.h>
int narcissistic( int number ){
int len=0,sum=0,t=0,n=number;
while(n!=0){
n=n/10;
len++;
}
n = number;
while(n!=0){
t = n%10;
sum+=pow(t,len);
n = n/10;
}
if(sum==number){
return 1;
}
else{
return 0;
}
}
void PrintN( int m, int n ){
for(int i=m+1;i<n;i++){
if(narcissistic(i)){
printf("%d\n",i);
}
}
}
数组循环右移
void ArrayShift( int a[], int n, int m ){
int b[n];
int temp;
for(int i=0;i<n;i++){
temp=(i+m)%n;
b[temp]=a[i];
}
for(int i=0;i<n;i++){
a[i] = b[i];
}
}
删除字符
void delchar( char *str, char c )
{
int n=strlen(str);
int i,j;
for(i=0,j=0;i<n;i++)
{
if(str[i]==c){
continue;
}
else{
str[j]=str[i];
j++;
}
}
str[j]='\0';
}
链表逆置
struct ListNode *reverse( struct ListNode *head ){
struct ListNode *p,*q;
while(head){
q = head;
head =head->next;
q->next = p;
p=q;
}
return p;
}
长整数转化为16进制字符串
void f(long int x,char *p)
{
int t;
if(x<0) //如果输入的长整数为负数,让p指向的字符数组首元素为负号
{
x=-x;
*p='-';
p++;
}
if(x==0) //若输入的长整数为0,那就是0
*p='0';
for(int i=0;i<10;i++)
{
t=x%16;
x=x/16; //这一句和上一句提取长整数的每一位
if(t<10)
*(p+9-i)=t+'0';
else
*(p+9-i)=t+'A'-10; //以上句根据十六进制和十进制转换规则进行相应转换,存入p指向的数组中,注意要逆向存储,因为提取时先提取长整数后面的数
if(x==0)
{
t=9-i;
break; //当所有长整数每一位数都提取完了,用10-i-1算出p指向的数组前面还有几个空着的
}
}
for(int i=0;i<10-t;i++) //把后面的往前推,补上前面的空,然后后面的赋值'\0',便于输出
{
*(p+i)=*(p+t+i);
*(p+t+i)='\0';
}
}
指定位置输出字符串
char *match( char *s, char ch1, char ch2 ){
int i=0;
while(*s!='\0'&&*s!=ch1){
s++;
}
if(*s!='\0'){
for(*s;*s!='\0'&&*s!=ch2;s++){
if(*s==ch2){
break;
}
printf("%c",*s);
i++;
}
if(*s!='\0'){
printf("%c",ch2);
}
}
printf("\n");
return s-i;
}
学生成绩链表处理
struct stud_node *createlist(){
struct stud_node *p;
struct stud_node *head=NULL,*tail=NULL;
int x;
scanf("%d",&x);
while(x){
p = (struct stud_node*)malloc(sizeof(struct stud_node));
p->num = x;
scanf("%s %d",p->name,&p->score);
p ->next = NULL;
if(head==NULL){
head=p;
}
else{
tail->next=p;
}
tail = p;
scanf("%d",&x);
}
return head;
}
struct stud_node *deletelist( struct stud_node *head, int min_score )
{ struct stud_node *ptr=NULL,*ptr1=NULL;
if(head==NULL) return NULL;
for(ptr=head;ptr!=NULL;ptr=ptr->next){
if(ptr->score<min_score){
if(ptr==head)head=head->next;
else ptr1->next=ptr->next;
}
else ptr1=ptr;
}
return head;
}
单链表节点删除
struct ListNode *readlist(){
struct ListNode *p=NULL;
int x;
struct ListNode *head=NULL,*tail=NULL;
scanf("%d",&x);
while(x!=-1){
p = (struct ListNode*)malloc(sizeof(struct ListNode));
p->data = x;
p->next=NULL;
if(head==NULL){
head=p;
}
else{
tail->next=p;
}
tail = p;
scanf("%d",&x);
}
return head;
}
struct ListNode *deletem( struct ListNode *L, int m ){
struct ListNode *p,*p1;
while(L){
if(L->data==m){
p = L;
L = L->next;
free(p);
}
else{break;}
}
if(L==NULL){
return NULL;
}
p = L;
p1=p->next;
while(p1){
if(p1->data==m){
p->next=p1->next;
}
else{
p = p1;
}
p1 = p1->next;
}
return L;
}
删除单链表偶数节点
struct ListNode *createlist(){
struct ListNode *p=NULL,*head=NULL,*tail=NULL;
int x;
scanf("%d",&x);
while(x!=-1){
p = (struct ListNode*)malloc(sizeof(struct ListNode));
p->data = x;
p->next = NULL;
if(head==NULL){
head = p;
}
else{
tail->next = p;
}
tail = p;
scanf("%d",&x);
}
return head;
}
struct ListNode *deleteeven( struct ListNode *head ){
struct ListNode *p,*ptr;
while(head!=NULL){
if(head->data%2==0){
p = head;
head = head->next;
free(p);
}
else{
break;
}
}
if(head==NULL){return NULL;}
p=head;//此时的p->data一定不为偶数
ptr = p->next;
while(ptr!=NULL){
if(ptr->data%2==0){
p->next = ptr->next;
free(ptr);
}
else{
p = ptr;
}
ptr = p->next;
}
return head;
}
二、编程题
求一元二次方程的根
#include<stdio.h>
#include<math.h>
float F(float a,float b,float c){
float delta,temp;
delta = (b*b)-(4*a*c);
float x1,x2;
if(a==0&&b==0&&c==0){
printf("Zero Equation");
}
else if(a==0&&b==0&&c!=0){
printf("Not An Equation");
}
else if(a==0&&b!=0){
x1 = -c/b;
printf("%.2f",x1);
}
else if(delta==0){
x1 = -b/(2*a);
printf("%.2f",x1);
}
else if(delta>0){
temp = sqrt(delta);
x1 = (-b+temp)/(2*a);
x2 = (-b-temp)/(2*a);
printf("%.2f\n",x1);
printf("%.2f",x2);
}
else if(delta<0){
temp = sqrt(-delta);
x1 = -b/(2*a);
temp = temp/(2*a);
if(x1==0){
printf("0.00+%.2fi\n",temp);
printf("0.00-%.2fi",temp);
}
else{
printf("%.2f+%.2fi\n",x1,temp);
printf("%.2f-%.2fi",x1,temp);
}
}
}
int main(){
float a,b,c;
scanf("%f %f %f",&a,&b,&c);
F(a,b,c);
return 0;
}
统计数字字符和空格
#include<stdio.h>
int main(){
char n;
int a=0,b=0,c=0;
while((n=getchar())!='\n'){
switch(n){
case' ':
a+=1;
break;
case '0':case'1':case'2':case'3':case'4':case'5':case'6':case'7':case'8':case'9':
b+=1;
break;
default:
c+=1;
break;
}
}
printf("blank = %d, digit = %d, other = %d",a,b,c);
return 0;
}
黑洞数
#include<stdio.h>
int main(){
int n,temp,a,b,c;
int cnt=0;
scanf("%d",&n);
if(n==495){
printf("1: 954 - 459 = 495");
}
else{
while(n!=495){
c = n%10;
b = (n/10)%10;
a = n/100;
cnt++;
if(a>b){temp=b;b=a;a=temp;}
if(a>c){temp=c;c=a;a=temp;}
if(b>c){temp=c;c=b;b=temp;}
n = (c*100+b*10+a)-(a*100+b*10+c);
printf("%d: %d - %d = %d\n",cnt,(c*100+b*10+a),(a*100+b*10+c),n);
}
}
return 0;
}
单词的首字母大写
#include<stdio.h>
#include<string.h>
int main(){
char A[2000];
gets(A);
if(A[0]>='a'&&A[0]<='z'){
A[0] = A[0] -32;
}
int l = strlen(A);
for(int i=1;i<l;i++){
if(A[i-1]==' '&&A[i]>='a'&&A[i]<='z'){
A[i] = A[i]-32;
}
}
puts(A);
return 0;
}
英文字母替换加密(大小写转换+后移1位)
#include<stdio.h>
int main()
{
char ch;
while((ch = getchar())!='\n')
{
if(ch>='A'&&ch<'Z') //判断大写字母
{
ch = ch+32+1; //转换并且后移一位
}
else if(ch=='Z') //大写'Z'单独处理
{
ch = 'a';
}
else if(ch>='a'&&ch<'z') //判断小写字母
{
ch = ch-32+1;
}
else if(ch=='z') //小写'z'单独处理
{
ch = 'A';
}
printf("%c",ch);
}
return 0;
}
统计单词长度
#include<stdio.h>
#include<string.h>
int main(){
char A[1000];
gets(A);
int l = strlen(A);
int i=0,s=0,j=0;
A[l]=' ';
while(i<l+1){
if(A[i]==' '){
if(i!=0&&s!=0){
printf("%d ",s);//s用来计单词字母数
j++;//用来记录单词数目
}
s=0;//记完一个归零
}
else{
s++;
}
i++;
}
if(j==0){
printf("0 ");
}
return 0;
}
通讯录排序
#include<stdio.h>
#include<string.h>
struct friend{
char name[11];
int birthday;
char num[18];
};
int main(){
int n;
scanf("%d",&n);
struct friend a[n];
for(int i=0;i<n;i++){
scanf("%s %d %s",&a[i].name,&a[i].birthday,&a[i].num);
scanf("\n");
}
struct friend temp;
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
if(a[i].birthday>a[j].birthday){
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
for(int i=0;i<n;i++){
printf("%s %d %s\n",a[i].name,a[i].birthday,a[i].num);
}
return 0;
}
英文单词排序
#include<stdio.h>
#include<string.h>
int main(){
char s[21][11];
int i=0;
while(1){
scanf("%s",&s[i]);
if(s[i][0]=='#'){
break;
}
i++;
}
char temp[11]={0};
for(int j=0;j<i;j++){
for(int k=j+1;k<i;k++){
if(strlen(s[j])>strlen(s[k])){
strcpy(temp,s[k]);
strcpy(s[k],s[j]);
strcpy(s[j],temp);
}
}
}
for(int j=0;j<i;j++){
printf("%s ",s[j]);
}
return 0;
}