㈠ 求助各位c語言編程高手~幫我做3道題~
#include<stdio.h>
#include<string.h>
#defineN4
/*-----------------------------------------
第一題
-------------------------------------------*/
voidDiamond(constchar*s,intn,intlen)
{
printf("%*s%-s\n",len,s+n-1,s+n);
if(n>1)
Diamond(s,n-1,len);
printf("%*s%-s\n",len,s+n,n==len?s+n:s+n+1);
}
/*-----------------------------------------
第二題
-------------------------------------------*/
typedefstruct
{
intgcd;
intlcm;
}pair;
voidGCD_LCM(inta,intb,pair*p)
{
inttmp;
intproct=a*b;
while(b%a)
{
tmp=a;
a=b%a;
b=tmp;
}
p->gcd=a;
p->lcm=proct/a;
}
/*-----------------------------------------
第三題
-------------------------------------------*/
voidSwap(int*lhs,int*rhs)
{
inttmp=*lhs;
*lhs=*rhs;
*rhs=tmp;
}
voidBubbleSort(int*beg,int*end)
{
for(;beg!=end;++beg)
for(int*p=end-1;p!=beg;--p)
if(*p<*(p-1))
Swap(p,p-1);
}
voidSelectSort(int*beg,int*end)
{
for(;beg!=end;++beg)
{
int*max=beg;
for(int*p=beg+1;p!=end;++p)
if(*max<*p)
max=p;
Swap(beg,max);
}
}
voidPrint(int*beg,int*end)
{
while(beg!=end)
printf("%d",*beg++);
putchar('\n');
}
intmain()
{
/*一*/
charpt[N+1]={0};
memset(pt,'*',N);
Diamond(pt,N,N);
/*二*/
pairp;
GCD_LCM(3,6,&p);
printf("%d%d\n",p.gcd,p.lcm);
/*三*/
inta[]={32,9,45,22,15,48,47,8,55,1};
Print(a,a+10);
BubbleSort(a,a+10);
Print(a,a+10);
SelectSort(a,a+10);
Print(a,a+10);
}
㈡ 求三道編程題 用初級C語言編寫 初級的啊 謝謝0.0
/*3個問題都在下面這個小程序里解決*/
#include <stdio.h>
char cch(char c)
{
if(c>='A'&&c<='Z')
return c+('a'-'A');
if(c>='a'&&c<='z')
return c-('a'-'A');
}
int mstrl(char* str)
{
char *ps=str;
while(*(ps++)){};
return ps-str-1;
}
void sortstr(char* str)
{
char tch;
int i,j;
int len=mstrl(str);
for(i=0;i<len;i++)
{
for(j=i+1;j<len;j++)
{
if(str[i]>str[j])
{
tch=str[i];
str[i]=str[j];
str[j]=tch;
}
}
}
}
int main()
{
char c;
char str[100];
printf("輸入一個字元:");
scanf("%c",&c);
printf("轉換後:%c\n",cch(c));
printf("輸入一個字元串:");
getchar();
scanf("%s",str);
printf("字元串長度為: %d\n",mstrl(str));
sortstr(str);
printf("升序排列:%s\n",str);
return 0;
}
輸入一個字元:A
轉換後:a
輸入一個字元串:bcwPASDkds
字元串長度為: 10
升序排列:ADPSbcdksw
請按任意鍵繼續. . .
㈢ 問3道C語言編程題目
1.#include<stdio.h>
int main(){
int a[3]={1,2,3},b[3]={4,5,6},c[3];
for(int i=0;i<3;i++){
c[i]=a[i]+b[i];
printf("%-2d",c[i]);
}
return 0;
}
2.#include<stdio.h>
int main(){
int a[10],i,j=0,max;
for(i=0;i<10;i++){
scanf("%d",&a[i]);
}
max=a[0];
for(i=1;i<10;i++){
if(a[i]>max){
max=a[i];
j=i;
}
}
printf("%-4d下標:-4d",max,j);
return 0;
}
3.#include<stdio.h>
int main(){
char a[100]={'\0'},b;
gets(a);
int i,j;
for(i=0;a[i]!='\0';i++){;}
for(j=0;j<=(i-1)/2;j++){
b=a[j];
a[j]=a[i-1-j];
a[i-1-j]=b;
}
printf("%s\n",a);
return 0;
}
㈣ 求助!簡單C語言編程題3道
1.
#include <stdio.h>
int fn(int a,int n)
{
int count,sum=0,b=0;
for(count=1;count<=n;count++)
{
b+=a;
sum+=b;
a=a*10;
}
return sum;
}
main()
{
int a,n,sum=0;
printf("input a and n:");
scanf("%d%d",&a,&n);
sum=fn(a,n);
printf("a+aa+aaa+...=%d",sum);
}
2.
#include <stdio.h>
#include <math.h>
int dist(int x1,int y1,int x2,int y2)
{
return (int)sqrt(abs(x1-x2)*abs(x1-x2)+abs(y1-y2)*abs(y1-y2));
}
main()
{
int x1,x2,y1,y2,s;
printf("input a and n:");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
s=dist(x1,y1,x2,y2);
printf("distance=%d",s);
}
3.
#include <stdio.h>
#include <math.h>
int countdigit(int number,int digit)
{
int count=0;
do
if(number%10==digit)
count++;
while(number/=10);
return count;
}
main()
{
int number,digit,s;
printf("input a and n:");
scanf("%d%d",&number,&digit);
s=countdigit(number,digit);
printf("%d",s);
}
//全部實現 驗證成功
㈤ 三道C語言題!編寫下列三道程序!
//1個函數對應1道題
//給點財富值就行了。
#include<stdio.h>
#include<stdlib.h>
int main()
{
void f1();
void f2();
void f3();
f1();
f2();
f3();
system("PAUSE");
return EXIT_SUCCESS;
}
void f1()
{
int i,j,t,a[4];
printf("請輸入4個整數:");
for(i=0;i<4;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<3;i++)
{
for(j=3;j>=i+1;j--)
{
if(a[j]<a[j-1])
{
t=a[j-1];
a[j-1]=a[j];
a[j]=t;
}
}
}
for(i=0;i<4;i++)
{
printf("%d ",a[i]);
}
printf("\n");
}
void f2()
{
int i,j,n;
printf("請輸入要列印的行數:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=0;j<2*i+1;j++)
{
printf("*");
}
printf("\n");
}
}
void f3()
{
int i,j,minr=0,minc=0,maxr=0,maxc=0,a[5][5];
printf("請輸入5x5矩陣的各元素的值:\n");
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
scanf("%d",&a[i][j]);
if(a[i][j]>a[maxr][maxc])
{
maxr=i;
maxc=j;
}
else if(a[i][j]<a[minr][minc])
{
minr=i;
minc=j;
}
}
}
printf("max=%d,row=%d,col=%d\n",a[maxr][maxc],maxr,maxc);
printf("min=%d,row=%d,col=%d\n",a[minr][minc],minr,minc);
}
㈥ 請大家幫做3道C語言題目,一定是要用C語言啊!
/*
* 第一個,100~300之間不能被3整除的數輸出
*/
#include<stdio.h>
int main ( viud )
{
int i;
for ( i=100; i<=300; i++ ) {
if ( i%3 !=0 ) {
printf( "\n%d", i );
}
}
return 0;
}
/*
* 第二個 你那圖形估計不對,不過方法差不多,簡單處理就好了
*/
#include<stdio.h>
int main ( viud ) {
puts("&&&&&");
puts("&&&&&");
puts("&&&&&");
puts("&&&&&");
return 0;
}
/*
* 第三題 string.h 里的 strcpy 就是了
* 下邊是代碼,將src所指向的字元串復制到dest里
*/
char *strcpy(char *dest, const char *src)
{
const char *p;
char *q;
for(p = src, q = dest; *p != '\0'; p++, q++)
*q = *p;
*q = '\0';
return dest;
}
㈦ C語言編程題~3道,高分求解~~先到先得
#include "iostream.h"
struct student
{
private:
int num;
char *name;
int math,english,Cworlds,all;
public:
void set_info(int Num,char *Name);
void set_chengji(int Math,int English,int C);
int allscore();
void display();
};
void student::set_info(int Num,char *Name)
{
num=Num;
name=Name;
}
void student::set_chengji(int Math,int English,int C)
{
this->math=Math;
this->english=English;
this->Cworlds=C;
}
void student::display()
{
cout<<"學號:"<<num<<'\t';
cout<<"姓名:"<<name<<'\t';
cout<<"總成績:"<<all;
cout<<endl;
}
int student::allscore()
{
all=english+math+Cworlds;
return all;
}
void main()
{
student stu[4];
int NUM=10000;
for(int i=0;i<4;i++)
{
stu[i].set_info(NUM++,"張三");
stu[i].set_chengji(80,90,100);
stu[i].allscore();
stu[i].display();
}
}
-----------------------------------------
#include "iostream.h"
#include "math.h"
struct zuobiao
{
int x,y;
void set_x_y();
zuobiao & set_mid(zuobiao &a);
double set_long();
friend ostream & operator <<(ostream &out,zuobiao &a);
};
void zuobiao::set_x_y()
{
cout<<"請輸入橫坐標x的值:";
cin>>x;
cout<<"請輸入縱坐標y的值:";
cin>>y;
}
zuobiao & zuobiao::set_mid(zuobiao &a)
{
x=(x+a.x)/2;
y=(y+a.y)/2;
return *this;
}
double zuobiao::set_long()
{
double how_long=sqrt(x*x+y*y);
return how_long;
}
ostream & operator <<(ostream &out,zuobiao &a)
{
out<<a.x<<','<<a.y<<endl;
return out;
}
void main()
{
zuobiao zb1,zb2;
zb1.set_x_y();
zb2.set_x_y();
cout<<zb1.set_mid(zb2);
cout<<zb1.set_long();
}
-----------------------------
#include "iostream.h"
struct node
{
int work_num;
int gongz;
node *next;
};
void list(node *l)
{
while(l->next!=NULL)
{
cout<<"工號:";
cout<<l->next->work_num<<endl;
cout<<"工資:";
cout<<l->next->gongz;
cout<<endl;
l=l->next;
}
}
void creat(int n)
{
node *L;
L=new node;
L->next=NULL;
for(int i=0;i<n;i++)
{
node *s;
s=new node;
cout<<"工號:";
cin>>s->work_num;
cout<<"工資:";
cin>>s->gongz;
cout<<endl;
s->next=L->next;
L->next=s;
}
list(L);
}
void main()
{
int n;
cout<<"輸入您要創建的職工個數:";
cin>>n;
creat(n);
}
㈧ 急求 3道c語言編程題
第一道:(太晚了,困了,沒時間寫注釋,不好意思)
#include<stdio.h>
#include<string.h>
#include<math.h>
int compare(int *a,int n)
{
int temp;
if(a[0]>a[1])
temp=a[0];
else
temp=a[1];
if(temp<a[2])
temp=a[2];
return temp;
}//compare
void main()
{
int a[3];
char str[3][10];
char *p;
int i,j,count;
printf("Please input the data.\n");
for( i=0;i<3;i++)
{
gets(str[i]);
p=str[i];
while(*p!='\0')
{
if(*p>='0'&&*p<='9')
p++;
else
{
printf("Data error!Please try again!\n");
i--;
break;
}//else
}//while
}//for
for( i=0;i<3;i++)
{
puts(str[i]);
count=0;
for(j=0;j<strlen(str[i]);j++)
{
count*=10;
count=count+(int)str[i][j]-48; //減去48是把字元型的ASCII碼轉換成本身代表的整數
}//for
a[i]=count;
}//for
printf("The max number is:%d.\n",compare(a,3));
}//main
第二道條件太少,不知道樓主到底想怎樣的分寢室。
第三道:
#include<stdio.h>
void main()
{
int i,j,count,m=0;
for(i=2;i<=1000;i++)//1不是素數
{
count=0;
for(j=2;j<=i/2;j++)
{
if(i%j==0)
{ count++;
break;
}
}//for
if(count==0)
{ printf("%d\t",i);
m++;
}//if
if(m==5)
{
printf("\n");
m=0;
}//if
}//for
}//main
補充:(時間匆忙,沒時間編譯。)
#include<stdio.h>
void main()
{
int person;
printf("Please input the number of person!");
scanf("%d",&person);
if(person%6==0)
printf("The number of dormitory is %d",person/6);
else
printf("The number of dormitory is %d",person/6+1);
}//main