C编程题 精选

//2、编程实现将任意的十进制整数转换成R进制数(R在2-16之间)。
#include<stdio.h> 
#include<conio.h> 
void main()
{ 
int n,R,i=0,a[16]={0}; 
char str[16]="0123456789ABCDEF"; 
clrscr();
printf("please input an integer number:\n"); 
scanf("%d",&n); 
printf("please input R:\n"); 
scanf("%d",&R); 
if(n<0) 
{ 
printf("%c",'-'); 
n=-n; 
} 
while(n!=0) 
{ 
a[i++]=n%R; 
n=n/R; 
} 
while(i-->0) 
printf("%c",str[a[i]]); 
getche();
} 

7.任何一个自然数m的立方均可写成m个连续奇数之和。例如: 
1^3=1 
2^3=3+5 
3^3=7+9+11 
4^3=13+15+17+19 
编程实现:输入一自然数n,求组成n3的n个连续奇数。 
#include<conio.h> 
#include<stdio.h> 
#include<math.h> // 
void main() 
{ 
int i,j,n,sum,count; 
clrscr(); 
printf("\n an integer here please:"); 
scanf("%d",&n); 
printf("\n"); 
// i=(int)floor(sqrt((float)n)); 
// if(i%2==0) 
// i++; 
i=1; // 
for(;i<=n*n*n;i+=2) 
{ 
sum=0; 
count=0; 
for(j=i;j<=n*n*n;j+=2) 
{ 
sum=sum+j; 
count++; 
if((n*n*n==sum)&&(count==n)) 
break; 
} 
if((sum==n*n*n)&&(count==n)) 
break; 
} 
printf("i=%d,j=%d\n\n",i,j); 
printf("%d*%d*%d=",n,n,n); 
// while(n-->1) { printf("%d+",i); i+=2; } 
for(;i<j;i+=2) // 
printf("%d+",i); // 
printf("%d",i); 
getch(); 
} 


13、	耶稣有13个门徒,其中有一个就是出卖耶稣的叛徒,请用排除法找出这位叛徒:13人围坐一圈,从第一个开始报号:1,2,3,1,2,3……,凡是报到“3”就退出圈子,最后留在圈内的人就是出卖耶稣的叛徒,请找出它原来的序号。 
/* 
// approach one 
#define N 13 
#include<stdio.h> 
#include<conio.h> 
struct person 
{ 
int number; //its order in the original circle 
int nextp; //record its next person 
}; 
struct person link[N+1]; //link[0] for no use 
void main() 
{ 
int i,count,next; //count for 12 persons,and 
//next for the person not out of circle yet 
clrscr(); 
for (i=1;i<=N;i++) 
{ 
link[i].number=i; //numbering each person 
if(i==N) 
link[i].nextp=1; 
else 
link[i].nextp=i+1; //numbering each next person 
} 
printf("\nThe sequence out of the circle is:\n"); 
for(next=1,count=1;count<N;count++) //count until 12 persons 
{ 
i=1; 
while (i!=3) //i counts 1,2,3 
{ 
do //skip the ones whose numbers are zero 
next=link[next].nextp; 
while(link[next].number==0); //end of do 
i++; 
} 
printf("%3d ",link[next].number); 
link[next].number=0; //indicate out of circle already 
do //start from the ones whose numbers are not zero next time 
next=link[next].nextp; 
while(link[next].number==0); 
} 
printf("\n\nThe betrayer of them is:"); 
for(i=1;i<=N;i++) 
if(link[i].number) 
printf("%3d\n",link[i].number); 
getch(); 
} 
*/ 
// approach two using cyclic list 
#define N 13 
#define LEN sizeof(struct person) 
#include<stdio.h> 
#include<conio.h> 
#include<alloc.h> 
#include<stdlib.h> 
// struct person //permit struct placed here// 
// { 
// int number; 
// struct person *next; 
// }; 
void main() 
{ 
int i,count; 
struct person //or permit struct placed here also// 
{ 
int number; 
struct person *next; 
}; 
struct person *head,*p1,*p2; 
clrscr(); 
head=p2=NULL; 
for(i=1;i<=N;i++) 
{ 
p1=(struct person *)malloc(LEN); 
p1->number=i; 
if(head==NULL) 
head=p1; 
else 
p2->next=p1; 
p2=p1; 
} 
p2->next=head; 
printf("\nthe sequence out of the circle is:\n"); 
for (count=1;count<N;count++) 
{ 
i=1; 
while(i!=3) 
{ 
p1=head; 
head=head->next; 
i++; 
} 
p2=head; 
printf("%3d ",p2->number); 
p1->next=head=p2->next; 
free(p2); 
} 
printf("\nThe betrayer of them is:\n%3d",head->number); 
getch(); 
} 

18、打印魔方阵。 
所谓魔方阵是指这样的的方阵: 
它的每一行、每一列和对角线之和均相等。 
输入n,要求打印由自然数1到n2的自然数构成的魔方阵(n为奇数)。 
例如,当n=3时,魔方阵为: 
  8 1 6 
  3 5 7 
  4 9 2 
魔方阵中各数排列规律为: 
① 将“1”放在第一行的中间一列; 
② 从“2”开始直到n×n为止的各数依次按下列规则存放:每一个数存放的行比前一个数的行数减1,列数同样加1; 
③ 如果上一数的行数为1,则下一个数的行数为n(最下一行),如在3×3 方阵中,1在第1行,则2应放在第3行第3列。 
④ 当上一个数的列数为n时,下一个数的列数应为1,行数减1。如2在第3行第3列,3应在第2行第1列。 
⑤如果按上面规则确定的位置上已有数,或上一个数是第1行第n列时,则把下一个数放在上一个数的下面。如按上面的规定,4应放在第1行第2列,但该位置已被1占据,所以4就放在3的下面。由于6是第1行第3列(即最后一列),故7放在6下面。 
#include<stdio.h> 
#include<conio.h> 
#define Max 15 
void main() 
{ 
int i,row,col,odd; 
int m[Max][Max]; 
clrscr(); 
printf("\nPlease input an odd:"); 
scanf("%d",&odd); 
if(odd<=0||odd%2==0) 
{ 
printf("\nInput Error!\n"); 
getch(); 
return 0; 
} 
printf("\nodd=%d\n\n",odd); 
row=0; 
col=odd/2; //1 placed in the middle of the first row 
for(i=1;i<=odd*odd;i++) 
{ 
m[row][col]=i; 
if(i%odd==0) //to the last col 
if(row==odd-1) //to the last row 
row=0; 
else 
row++; 
else //outmost else 
{ 
if(row==0) 
row=odd-1; 
else 
row--; 
if(col==odd-1) 
col=0; 
else 
col++; 
} //end of outmost else 
} //end of for 
for(row=0;row<odd;row++) 
{ 
for(col=0;col<odd;col++) printf("%4d",m[row][col]); 
printf("\n\n"); 
} 
getch(); 
return 0; 
} 
19、找出一个二维数组中的“鞍点”,即该位置上的元素在该行中最大,在该列中最小(也可能没有“鞍点”),打印出有关信息。 
#define N 20 
#define M 20 
#include<stdio.h> 
#include<conio.h> 
void main( ) 
{ 
int a[N][M]; //int a[][]; not allowed here 
int i,j,k,row,col,n,m,find=0; 
clrscr(); 
printf("\nEnter n & m:\n\n"); 
scanf("%d%d",&n,&m); 
printf("\nEnter a[0][0]--a[%d][%d]\n\n",n-1,m-1); 
for(i=0;i<n;i++) 
for(j=0;j<m;j++) 
scanf("%d",&a[i][j]); 
printf("\n\nThe array you have just entered is:\n"); 
for(i=0;i<n;i++) 
{ 
for(j=0;j<m;j++) 
printf("%5d",a[i][j]); 
printf("\n\n"); } 
//find the point 
for(i=0;i<n;i++) 
{ 
for(col=0,j=1;j<m;j++) 
if(a[i][col]<a[i][j]) //find col,select sort according to col 
col=j; 
for(row=0,k=1;k<n;k++) 
if(a[row][col]>a[k][col]) //find row,select sort according to row 
row=k; 
if(i==row) 
{ 
find=1; 
printf("The point is a[%d][%d].\n",row,col); 
} 
} 
if(!find) 
printf("\nNo solution.\n"); 
getch(); 
} 

22、建立一个链表,每个结点包括:学号、姓名、性别、年龄,输入一个学号,如果链表中的结点包括该学号,则输出该结点内容后,并将其结点删去。 
#define LEN sizeof(struct stud_node) 
#include<conio.h> 
#include<stdio.h> 
#include<string.h> 
struct stud_record 
{ 
char StudNo[6]; 
char StudName[10]; 
char StudSex; /*M---Male F---Female*/ 
int StudAge; 
}; 
struct stud_node 
{ 
struct stud_record stud_mem; 
struct stud_node *next; 
}; 
/*Create Student Linear table*/ 
struct stud_node *create() 
{ 
struct stud_node *head,*p,*q; 
char vno[6],vname[10],vsex; 
int vage; 
head=NULL; 
while(1) 
{ 
printf("\nPlease input a student record\n\nNo\tName\tSex\tAge\n\n"); 
scanf("%s",vno); 
getchar(); 
if(strcmp(vno,"0")==0) //when vno=="0" to exit 
break; 
scanf("%s",vname); 
getchar(); 
scanf("%c%d",&vsex,&vage); 
getchar(); 
p=(struct stud_node *)malloc(LEN); //allocate space to node p 
strcpy(p->stud_mem.StudNo,vno); 
strcpy(p->stud_mem.StudName,vname); 
p->stud_mem.StudSex=vsex; 
p->stud_mem.StudAge=vage; 
if(head==NULL) 
head=p; 
else 
q->next=p; //q is the previous node of p 
q=p; 
} 
if(head!=NULL) 
q->next=NULL; //the last node has no child 
return head; 
} /*Find a student and If Found then Delete the node*/ 
struct stud_node *delete(struct stud_node *head,char no[6]) 
{ 
struct stud_node *p,*q; 
p=head; 
q=p; 
while(p) 
{ 
if(strcmp(p->stud_mem.StudNo,no)==0) /*Delete the node*/ 
{ 
if(p==head) //delete the first node 
head=p->next; 
else 
{ 
if(p->next!=NULL) //delete the middle node 
q->next=p->next; 
else //delete the last node 
q->next=NULL; 
} 
printf("\n\t\t%s\t%s\t%c\t%d\n",p->stud_mem.StudNo,p->stud_mem.StudName, 
p->stud_mem.StudSex,p->stud_mem.StudAge); 
free(p); 
break; 
} 
q=p; 
p=p->next; 
} 
return head; 
} 
/*Disp linear table content*/ 
void prn(struct stud_node *head) 
{ 
struct stud_node *p; 
int i=1; 
p=head; 
printf("\nRecord\tNo\tName\tSex\tAge\n"); 
while(p) 
{ 
printf("%3d\t%s\t%s\t%c\t%d\n",i,p->stud_mem.StudNo,p->stud_mem.StudName, 
p->stud_mem.StudSex,p->stud_mem.StudAge); 
p=p->next; 
i++; 
} 
} 
/*main program here*/ 
void main() 
{ 
struct stud_node *head; 
char no[6]; 
clrscr(); 
head=create(); 
prn(head); 
getch(); 
printf("\nPlease input a studno to Find:"); 
gets(no); 
head=delete(head,no); 
prn(head); 
getch(); 
} 

27、编写一个函数实现对两个字符串的比较。不用使用C语言提供的标准函数strcmp。要求在主函数中输入两个字符串,并输出比较的结果(相等的结果为0,不等时结果为第一个不相等字符的ASCII差值)。 
strcmp(p1,p2) 
char *p1,*p2; 
{ int i; 
i=0; 
while(*(p1+i)==*(p2+i)) 
if(*(p1+i++)=='\0') return(0); 
return(*(p1+i)-*(p2+i)); 
} 
main() 
{ int m; 
char str1[20],str2[20],*p1,*p2; 
printf("Input two strings(1 string at each line):\n"); 
scanf("%s",str1); 
scanf("%s",str2); 
p1=&str1[0]; 
p2=&str2[0]; 
m=strcmp(p1,p2); 
printf("The result of comparison :%d\n",m); 
} 



 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值