❶ 求大神解答c語言編程
#include <stdio.h>
#include <stdlib.h>
//定義一個結構體保存裁判id和分數
typedef struct{
unsigned char judegId;//裁判ID
unsigned char judegScore;//裁判給的分數
}judge;
int main()
{
judge jg[10];
int i;
int score;
for(i = 0;i < 10;i++)
{
printf("請輸入裁判%d打的分數\n",i+1);
scanf("%d",&score);
getchar();
if(score <= 0 || score > 100)
{
printf("輸入分數無效,重新輸入,分數要求1-100!\n");
printf("請輸入裁判%d打的分數\n",i+1);
scanf("%d",&score);
getchar();
if(score <= 0 || score > 100)
{
printf("無效輸入,程序退出!\n");
return -1;
}
}
jg[i].judegId = i;
jg[i].judegScore = score;
}
//對數組從分數低到分數高排序
for(i = 0;i < 9;i++)
{
if(jg[i].judegScore > jg[i+1].judegScore)
{
unsigned char tmpId = jg[i].judegId;
unsigned char tmpScore = jg[i].judegScore;
jg[i].judegId = jg[i+1].judegId;
jg[i].judegScore = jg[i+1].judegScore;
jg[i+1].judegScore = tmpScore;
jg[i+1].judegId = tmpId;
}
}
//列印所有得分
printf("選手得分為:\n");
for(i = 0;i < 10;i++)
{
printf("%d ",jg[i].judegScore);
}
printf("\n");
//計算評價得分
unsigned short sum = 0;
for(i = 1;i < 9;i++)
{
sum+=jg[i].judegScore;
}
unsigned char average = sum/8;
printf("去掉最高分%d,最低分%d,評價得分為%d分\n",jg[9].judegScore,jg[0].judegScore,average);
unsigned char fairIndex0 = 0;
unsigned char unfairIndex = 0;
//查找與平均值差距最小的
for(i = 1;i < 10;i++)
{
//abs為求絕對值函數
if(abs(jg[i].judegScore-average) < abs(jg[fairIndex].judegScore-average))
{
fairIndex = i;
}
}
printf("最公平的教練是%d,打分為%d\n",jg[fairIndex].judegId,jg[fairIndex].judegScore);
//查找與平均分差距最大的
for(i = 1;i < 10;i++)
{
if(abs(jg[i].judegScore-average) > abs(jg[unfairIndex].judegScore-average))
{
unfairIndex = i;
}
}
printf("最不公平的教練是%d,打分為%d\n",jg[unfairIndex].judegId,jg[unfairIndex].judegScore);
return 0;
}
//留一個問題給你,可能有兩個裁判是最公平或者最不公平的,這個自己想想怎麼做
❷ 求這幾題C語言程序設計題目的解析
14 while循環沒有加{ },所以只控制printf一條列印語句
第一次 :n-- 為6 n使用時是5 --n為4;
第二次 :n-- 為4 n使用時是3 --n為2;
第三次 :n-- 為2 n使用時是1 --n為0;
從第一次循環中我們得到列印的結果每次-2,循環三次,所以結果為420
15 b==c中的==為判斷符號,其結果只有兩種0或1,條件成立返回1,不成則立返回0
16 x為二維數組
| 0 1 2 |
| 3 4 5 |
| 6 7 8 |
其for 循環的結果為0,1,2所以其取值為x[0][2],x[1][1],x[2][0],結果就是246
❸ c語言程序設計求詳解
1、先看s型變數,它是由兩個int型變數x和y組成的一個結構體變數類型;
2、再看a變數,它是由4個s型變數組成的一個數組;
3、關於s型結構體變數數組a,它由4個元素組成(每個元素是一個結構體變數),分別是:
a[0]={x=1,y=10}
a[1]={x=2,y=20}
a[2]={x=3,y=30}
a[3]={x=4,y=40}
4、接著看main函數的執行:
第一句:struct s *p=a+1;
這一句定義了一個s型指針,並且讓p指向a+1,即指向a[1]。注意a[1]的x=2,y=20。
5、最後一句列印,列印的內容是++(p->x),p->x含義是p指針指向的s型變數中x的值,再自增1。由於p指向的是a[1],a[1]中的x=2,2自增1後是3,所以,輸出的是3。
6、選項C是正確的。
❹ C語言編程題目,求大神解析!
沒增加
p++相當於一個普通變數++運算,只不過具體加的數是指定數據類型的寬度,即sizeof(teacher).只是遞增了一塊地址,
只有new一個teacher時才會是實實在在增加一塊內存佔用.
你在快遞盒上原先的XXX路10號+1,不會實實在在創建一幢房子,是吧?,只有在隔壁造一幢新房子,然後申請編號,這個XXX路11號才有意義.new就是向政府申請造新房子並編號.這個政府就是系統.
❺ 關於一題C語言的程序設計求大神詳解。
struct stud
{ char name[20]; //前五個都是信息
long num;
int age;
char sex;
float score;
struct stud *next; //這是指針,指向下一個結點。
};
void new_record()
{ char numstr[20];
new1=(struct stud *)malloc(sizeof(struct stud)); //分配一個結點
if(head==NULL) //如果沒有結點,新建一個
head=new1;
else //否則往後尋找插入的地方
{ this1=head;
while(this1->next!=NULL)
this1=this1->next;
this1->next=new1;
}
this1=new1; //找到插入的位置了,開始插入信息
printf("please enter name:");
gets(thi
……
其他的自己琢磨吧,這是學習的過程,實在不會的話,看看數據結構的鏈表部分。
❻ c語言程序設計題 跪求大神解答 答得好繼續追加財富值
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
structstudent{
intnumber;
char*name;
int*score;
intnum_scores;
intavarage;
};
structstudent_data{
structstudent**arr;
intarr_num;
intarr_size;
intnum_scores;
};
structstudent*student_new(intnum_scores){
structstudent*stu=(structstudent*)malloc(sizeof(structstudent));
if(stu==NULL)
returnNULL;
memset(stu,0,sizeof(structstudent));
stu->score=(int*)malloc(sizeof(int)*num_scores);
if(stu->score==NULL){
free(stu);
returnNULL;
}
stu->num_scores=num_scores;
returnstu;
}
voidstudent_free(structstudent*stu){
free(stu->name);
free(stu->score);
free(stu);
}
structstudent_data*student_data_new(intnum_scores){
structstudent_data*data=(structstudent_data*)malloc(sizeof(structstudent_data));
inti;
if(data==NULL)
returnNULL;
data->arr_num=0;
data->arr_size=1024;
data->arr=(structstudent**)malloc(sizeof(void*)*data->arr_size);
if(data->arr==NULL){
free(data);
returnNULL;
}
for(i=0;i<data->arr_size;i++)
data->arr[i]=NULL;
data->num_scores=num_scores;
returndata;
}
voidstudent_data_free(structstudent_data*data){
inti;
for(i=0;i<data->arr_num;i++){
student_free(data->arr[i]);
}
free(data->arr);
free(data);
}
#defineBUF_PIECE_SIZE256
structstudent*student_parse(intnum_scores,charpiece[][BUF_PIECE_SIZE],intn){
structstudent*stu=NULL;
inti,total=0;
if(n!=num_scores+2)//number,name,score0,score1,...scoren
returnNULL;
stu=student_new(num_scores);
if(stu==NULL)
returnNULL;
stu->number=atoi(piece[0]);
stu->name=strp(piece[1]);
for(i=0;i<num_scores;i++){
stu->score[i]=atoi(piece[i+2]);
total+=stu->score[i];
}
stu->avarage=total/stu->num_scores;
returnstu;
}
voidstudent_mp(structstudent*stu){
inti;
printf("number=%-5dname=%-8savarage=%-3dscores=[",stu->number,stu->name,stu->avarage);
for(i=0;i<stu->num_scores;i++)
printf("%-3d",stu->score[i]);
printf("] ");
}
voidstudent_data_mp(structstudent_data*data){
inti;
for(i=0;i<data->arr_num;i++)
student_mp(data->arr[i]);
}
structstudent*student_data_search(structstudent_data*data,intnumber){
inti;
for(i=0;i<data->arr_num;i++){
if(data->arr[i]->number==number)
returndata->arr[i];
}
returnNULL;
}
voidstudent_data_insert(structstudent_data*data,structstudent*stu){
inti,j;
if(data->arr_num==data->arr_size){
i=data->arr_size;
data->arr_size+=1024;
data->arr=(structstudent**)realloc(data->arr,sizeof(void*)*data->arr_size);
for(;i<data->arr_size;i++)
data->arr[i]=NULL;
}
for(i=0;i<data->arr_num;i++){
if(data->arr[i]->number==stu->number){
student_free(data->arr[i]);
data->arr[i]=stu;
return;
}
if(data->arr[i]->avarage<stu->avarage){
break;
}
}
j=data->arr_num;
data->arr_num++;
for(;j>i;j--){
data->arr[j]=data->arr[j-1];
}
data->arr[j]=stu;
}
#defineM5
intmain(intargc,char**argv){
charbuf[1024];
structstudent_data*data;
structstudent*stu,*s;
char*p;
charpiece[M+2][BUF_PIECE_SIZE];
intn;
data=student_data_new(M);
if(data==NULL)
return-1;
while(1){
memset(buf,0,sizeof(buf));
memset(piece,0,sizeof(piece));
fgets(buf,sizeof(buf),stdin);
p=strtok(buf,"");
n=0;
while(p&&n<M+2){
strncpy(piece[n++],p,BUF_PIECE_SIZE);
p=strtok(NULL,"");
}
if(n>1){
if((stu=student_parse(data->num_scores,piece,n))!=NULL){
s=student_data_search(data,stu->number);
if(s!=NULL){
printf("WARNING:overwritedata ");
}
student_data_insert(data,stu);
student_data_mp(data);
}
else
printf("invaliddata ");
}
else{
stu=student_data_search(data,atoi(piece[0]));
if(stu==NULL){
printf("studentnotfound ");
}
else
student_mp(stu);
}
}
student_data_free(data);
return0;
}