08年11月份北大高級語言程序(c)上機源碼

 

一:輸入一個整數x,和正整數n,計算並輸出x*(x-1)*(x-2)......(x-n+1)的值。

#include "stdio.h"

 

main() {
int x,n,i;

long xx;

/*輸入兩個整數*/
scanf("%d,%d",&x,&n);

 

xx=x;

for(i=0;i<n;i++)

xx*=(x-i); /*關鍵,累乘*/

printf("sum=%ld/n",xx);
}

 

 

二:輸入n(n<100)個數,計算出從第1個......n-1個數中比n大,小和相等的數分別有多少個,並輸出。

 

#include "stdio.h"

 

main() {
int a[100],max=0,min=0,equal=0,i=0,d,x;

 

/*輸入n個數,已-1爲結束標記*/
for(;;) {
scanf("%d",&d);
if(d==-1) break;
a[i] = d;
i++;
}

/*比較*/
for(x=0;x<i;x++) {
if(a[x]>i) max++;
else if(a[x]<i) min++;
else equal++;
}

printf("max=%d,min=%d,equal=%d",max,min,equal);

}

 

三:輸入n(n<100)個學生成績,包括學號(int),姓名(字符串)及兩門課的成績(float),以-1作爲結束標記,

然後輸入學號查詢,如果存在,輸出這個學生的姓名和兩門課的成績,否則,輸出No

 

#include <stdio.h>

 

/*定義學生相關信息*/
struct student{
int num;
char name[30];
double yscore;
double sscore;
} stu[100];

 

 

main() {
double yuwen,shuxue; /*中間變量*/
int i=0,j,stuId,tmp;

 

/*輸入n<100個學生信息,這有個地方得注意,就是輸入浮點數的時候,

應該定義兩個臨時的浮點數,把這兩個浮點數作爲一箇中間變量,

先把讀入的數放入中間變量,然後再賦值給yscore,sscore,否則,

會出現scanf floating point formats not linked......類似錯誤

*/
for(;;) {
scanf("%d",&stu[i].num);
if(stu[i].num==-1) break;
scanf("%s %lf %lf",stu[i].name,&yuwen,&shuxue);
stu[i].yscore=yuwen;
stu[i].sscore=shuxue;
i++; /*統計輸入的個數*/
}

 

printf("please enter student id/n");

 

/*輸入查詢的學生id號*/
scanf("%d",&stuId);

 

/*查詢並輸出*/
for(j=0;j<i;j++) {
if(stuId==stu[j].num) {
printf("name=%syuwen=%lfshuxue=%lf/n",stu[j].name,stu[j].yscore,stu[j].sscore);
} else {
printf("no");
}
}
}

 

 

四:執行e2.exe x y,其中x,y爲兩個無符號整型,參數間以空格隔開,輸出它們的差(x-y);
如:e2.exe 12 82,輸出-70

 

#include "stdio.h"
#include "stdlib.h"

 

main(int argc,char *argv[]) {
long result ;

 

/*關鍵strtoul*/

unsigned long int a = strtoul(argv[1],'/0',10);
unsigned long int b = strtoul(argv[2],'/0',10);

 

result = a-b;
printf("result is=%d/n,count is=%d",result,argc);
}

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章