石油大学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");
}