编程高手请进!(C语言)

(一)基本要求题目1、猜数游戏(掌握程序结构及随机函数应用)程序说明:游戏的开始由机器产生一个随机数(1~100之间,用库函数random(),用法见后面的说明),然后游... (一)基本要求题目
1、猜数游戏(掌握程序结构及随机函数应用)
程序说明:游戏的开始由机器产生一个随机数(1~100之间,用库函数random(),用法见后面的说明),然后游戏者在程序的提示下猜数,若输入的数比这个数大,程序提示:Your answer is HIGH,try again. ,否则,程序提示:Your answer is LOW,try again.,直到猜对为止。程序可实现连续猜数,直到游戏者退出。
程序输出要求:累计游戏者猜对一个数所需次数n,当n<=7时,给出“Congratulation”字样;当7<n<15时,给出“I can bet you can do it better.”;当n>=15时,用exit(0)库函数退出程序。
2、百钱百鸡(穷举算法)
我国古代数学家张丘键在《算经》中出了一道题“鸡翁一,值钱五;鸡母一,值钱三;鸡雏三,值钱一。百钱买百鸡,问鸡翁、鸡母、鸡雏各几何?
注:穷举法是最简单、最常见的一种程序设计方法。它充分利用了计算机处理的高速特性。使用穷举法的关键是确定正确的穷举范围,即不能过分扩大、也不能过分缩小穷举的范围。
3、简单计算器
请编写一个程序计算表达式:data1 op data2的值。其中,op为运算符+、-、*、/。
4、打印出所有的“水仙花数”
所谓“水仙花数”是指一个3位数,其各位数字立方和等于该数本身。例如,153是一水仙花数,因为:153=13+53+33 。
5、判断一个数是否是数组中的成员(用二分法查找)
已知数据a中一共有10个已排序的整数(由小到大排列)。现在从键盘上输入一个数,判断这个数是否是数组a中的数,如果是的话,打印出此数在数组a中的位置来,否则打印“找不到“。
(提示:①设待查找的数为x,设三个位置变量l、m、h分别代表查找范围的顶部、中间位置和底部,m=(l+h)/2,把数分成以a[m]为中点的两段范围。②判断x 是否等于a[m],若是,则找到。③若大于a[m],则x必在后半段范围,即在a[m+1]至a[h]。则舍弃前半段,再在后半段重新划分两段范围,定出l、m和h,重复以上步骤,逐步缩小查找范围。)
8、位数大小排序(掌握数值排序算法)
程序说明:输入一个五位整数,对此整数中的五个数值进行从大到小的顺序排序,形成一个新的五位整数,并输出这个整数。
要求:用函数调用。
9、请编写函数fun,函数的功能是求出二维数组周边元素之和,作为函数值返回。二维数组中的值在主函数中赋予。
10、取子串函数
编写求子串函数substr(s,n1,n2),在串s中从n1位置开始取n2个字符的子串
展开
 我来答
FrankGaocy
推荐于2016-06-15 · TA获得超过778个赞
知道小有建树答主
回答量:160
采纳率:0%
帮助的人:120万
展开全部
1.
#include <stdio.h>
#include<stdlib.h>
#include<ctype.h>
main()
{
int count;/*猜数字的次数*/
int number;/*系统产生的随机数字*/
int guess;/*程序员输入数字*/
char yes='Y';
clrscr();
printf("\nNow let us play the game.\n Guess the number:");
while (toupper(yes)=='Y')
{
count=0;
randomize();
number=random(100)+1;
do
{
do
{
printf("\nInput an integer number(1~100):");
scanf("%d",&guess);
}while(!(guess>=1&&guess<=100));/*结束第二层DO~WHILE循环*/
if (guess<number)
printf("\n Your answer is low,try again!");/*如果用户输入的数字小于系统随机数,则输出数字太小的提示信息*/

if (guess>number)
printf("\n Your answer is high,try again!");/*如果用户输入的数字大于系统随机数,则输出数字太小的提示信息*/

count++;/*猜测次数加一*/
if (count==15)
{
printf("\n This is the %d times! Think it hard next!",count);
exit(0);/*如猜测15次还没猜对,则退出游戏*/
}
}while (!(guess==number));
if (count<=7)/*猜测的次数小于7次*/
{
printf("\n You have got it in %d times.\n",count);
printf("\n you guess right,Congretulations!");/*游戏成功则提示祝贺信息*/
}
else
{
printf("\n You got it in %d times.\n",count);
printf("\n I bet you can do it better!");/*游戏失败则提示鼓励信息*/
}
printf("\n NEXT?(Y/N):");/*选择是否重新游戏*/
scanf("%c",&yes);
}
}

2.
#include <stdio.h>
void main()
{
int gj, mj, xj, t1, t2;
for (gj=1; gj<=20; gj++)
{
for (mj=1; mj<34; mj++)
{
xj=100-gj-mj;
t1=xj%3;
t2=5*gj+3*mj+xj/3;
if (t1==0&&t2==100)
printf("gj=%d,mj=%d,xj=%d\n",gj,mj,xj);
}
}

}

3.
/* (a part of parser)
simple integer arithmetic calculator

<exp> -> <term>{<addop><term>}
<addop> -> + | -
<term> -> <factor> { <mulop> <factor> }
<mulop> -> * | /
<factor> -> (<exp>) | Number

see( page 12 of textbook )
Inputs a line of text from array
Outputs "Error" or the result.
*/
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>

char token;
char array[50]="";
int pos=0;
char string[50]="";

int exp(void);
int term(void);
int factor(void);

bool isnumber(char ch)
{
if(ch>=48 && ch<=57)
return(true);
else return(false);
}

int CharToInt(char ch)
{
int temp = 0;
while((ch >= 48)&&(ch <= 57))
{
temp = temp*10+(ch-48);
ch = GetNextChar();
}
return temp;
}
void Error(char ch,int type)
{
if(type==0)
{
cout<<"Error: Missing symbol "<<ch<<endl;
getch();
}
else if(type==1)
{
cout<<"Error: "<<"'"<<ch<<"'"<<" is not number"<<endl;
getch();
}
else{printf("Error: '%c' is unwanted !\n",token);getch();}
exit(1);
}

char GetNextChar()
{
return(array[pos++]);
}

void Match(char shouldtoken)
{
if(token==shouldtoken)
token=GetNextChar();
else Error(shouldtoken,0);
}
void ReadTextFile(char str[])
{
FILE *fp;
char ch;
int i=0;
if((fp=fopen("text.txt","r"))==NULL)
{
printf("the file text.txt can not open!\n");
getch();
exit(1);
}
ch=fgetc(fp);
while(ch!=EOF)
{
str[i++]=ch;
ch=fgetc(fp);
}
strcpy(string,str);
str[i]='.';
fclose(fp);
}

//***********************************************************8
void main()
{
ReadTextFile(array);
int result;
token=GetNextChar();
result=exp();
if(token=='.')
printf("\n\n\ %s = %d\n\n",string,result);
else Error(token,2);
cout<<"input any key to exit....."<<endl;
getch();
}

int exp(void)
{
int temp=term();
while((token=='+')||(token=='-'))
switch(token)
{
case '+':
Match('+');
temp+=term();
break;
case '-':
Match('-');
temp-=term();
break;
default:
break;
}
return(temp);
}

int term(void)
{
int temp=factor();
while((token=='*')||(token=='/'))
switch(token)
{
case '*':
Match('*');
temp*=factor();
break;
case '/':
Match('/');
temp/=factor();
break;
default:break;
}
return(temp);
}

int factor(void)
{
int temp;
if(token=='(')
{
Match('(');
temp=exp();
Match(')');
}
else if(isnumber(token))
{
temp=CharToInt(token);
token=GetNextChar();
}
else Error(token,1);
return(temp);
}

4.
#include <stdio.h>
int main(void)
{
int a,b,c;
for(a=1;a<=9;a++)
for(b=0;b<=9;b++)
for(c=0;c<=9;c++)
if(100*a+10*b+c==a*a*a+b*b*b+c*c*c)
printf("%d%d%d\n",a,b,c);
return 0;
}

5.
int binarysearch(int a[],int x,int n)
{
int left,right,middle;
left=0;
right=n-1;
while(left<=right)
{
middle=(left+right)/2;
if(x==a[middle])
return middle;
if(x>a[middle])
left=middle+1;
else right=middle-1;
}
return -1;
}

8.
#include <stdio.h>
#include <stdlib.h>
int cmp(const void* a, const void* b)
{
return (int)(*(char*)b - *(char*)a);
}

int sort(int n)
{
char buf[8];
sprintf(buf, "%d", n);
qsort(buf, 5, 1, cmp);
return atoi(buf);
}
int main(void)
{
int n;
scanf("%d", &n);
if(9999 < n && n < 100000)
printf("%d\n", sort(n));
return 0;
}

9.
int fun(int p[m][N],int m,int N)//m为行数,n为列数
{
int sum = 0;
for(int i=0;i<m;i++)
sum += p[m][0];
for(int j=0;j<m;j++)
sum += p[m][n-1];
for(int k=1;k<m-1;k++)
sum += p[0][k];
for(int k=1;k<m-1;k++)
sum += p[m-1][k];
return sum;
}

10.
char[] substr(char s[],int n1,int n2){
char ret[255];int i = 0;
for(;i<n2;i++) ret[i] = s[n1+i];
ret[i++] = '\0';
return ret;
}

参考资料: 我不是C高手,不过就在百度吧里,你的问题都有现成的答案!呵呵:-)

推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式