石油大學c語言上機答案
⑴ c語言關於strlen函數的
設有定義語句:char str[][20]={"Beijing","123456"},*p=str; /*把逗號去掉,把中文變數字*/
則printf("%d\n",strlen(p+20)); 輸出結果是
A)10 B) 6 C) 0 D) 20
選擇B
希望回答對你有幫助
⑵ 求C語言上機實驗題答案!!!
//1.將字元串中ASCII碼最小的字元放在第一個字元位置,其餘字元依次往後移
void func(char *str)
{
int loc=0,cloc=0;
char c;
c = *str;
while(*(str+loc) !='\0')
{
if( c>*(str+loc))
{
c = *(str+loc);
cloc = loc;
}
loc++;
}
while(cloc !=0) //移到最前
{
//左移
loc =0;
while(*(str+loc+1) !='\0')
{
c = *(str+loc);
*(str+loc) = *(str+loc+1);
*(str+loc+1) = c;
loc++;
}
cloc--;
}
}
//2.略去非數字字元,將字元串s轉換為一個整數
long func(char *s);
{
int loc=0;
char c;
long ret=0L;
......
......
//假設現在已經略去非數字字元,並取得+-,放於*str
while(*(str+loc) !='\0')
{
ret = ret*10 + (*(str+loc)-'0');
loc++;
}
if( c=='-')
ret *= -1;
return ret;
}
//3.返回在s中相鄰三個數的和中的最小值
int min3adj(int s[], int n)
{
int i,j;
long sum;
sum = s[0] + s[1] + s[2];
for( i=0; i <n-2 ;i++)
{
if( s[i] + s[i+1] + s[i+2] < sum )
sum = s[i] + s[i+1] + s[i+2];
}
return sum;
}
⑶ 求:中國石油大學(華東)現代遠程教育 C語言 在線考試答案
1.從鍵盤輸入10個整數,求其和。
#include <stdio.h>
main()
{
int i,x,s=0;
printf("請輸入10個整數:");
for(i=0;i<10;i++)
{
scanf("%d",&x);
s=s+x;
}
printf("s=%d\n",s);
}
2.計算s=1!+2!+…+10!
方法1:
#include <stdio.h>
main()
{
int i,j;
long s=0,t;
for(i=1;i<=10;i++)
{
t=1;
for(j=1;j<=i;j++)
t=t*j;
s=s+t;
}
printf("1!+2!+...+10!=%ld\n",s);
}
方法2:
#include <stdio.h>
main()
{
int i;
long s=0,t=1;
for(i=1;i<=10;i++)
{
t=t*i;
s=s+t;
}
printf("1!+2!+...+10!=%ld\n",s);
}
3.求100-999中的水仙花數。所謂水仙花數是指一個數的各位數字的立方和等於該數自身的數。如:153=1*1*1+5*5*5+3*3*3 。
方法1:
#include <stdio.h>
main()
{
int n,g,s,b;
for(n=100;n<1000;n++)
{
g=n%10;
s=n/10%10;
b=n/100;
if(n==b*b*b+s*s*s+g*g*g)
printf("%d=%d%d%d\n",n,b,s,g);
}
printf("\n");
}
方法2:
#include <stdio.h>
main()
{
int n,g,s,b;
for(b=1;b<=9;b++)
for(s=0;s<=9;s++)
for(g=0;g<=9;g++)
{
n=100*b+10*s+g;
if(n==b*b*b+s*s*s+g*g*g)
printf("%d%d%d=%d\n",b,s,g,n);
}
printf("\n");
}