编程题提问
所有程序在win-tc和Dev-c++下试验通过,代码简练。
第一题:
/* 编一个程序,输入n个互不相等的整数存于数组中,并输出。程序如发现输入的数据已输入过,则要求重新输入。要求用指针实现。 */
#include <stdio.h>
#include <malloc.h>
int main( )
{
int i=0,j,k,n,flag,*p;
printf("Please input number of the digits:\n");
scanf("%d",&n);
p=(int *)malloc(n*sizeof(int));
printf("Please input %d digits one by one:\n",n);
do
{ flag=0;
scanf("%d",p+i);
for(j=0;j<i;j++)
{ for(k=j+1;k<i+1;k++)
if(*(p+k)==*(p+j))
{ printf("Input repetition! Please input again!\n");
flag=1;
break; /*如有重复立即退出该层循环,提高判断速度*/
}
if(flag==1)
break; /*如有重复立即退出该层循环,提高判断速度*/
}
if(flag==0)
i++;
}while(i<n);
printf("The input digits are:\n",n);
for(i=0;i<n;i++)
printf("%d\n",*(p+i));
system("pause");
return 0;
}
第二题:
# include <stdio.h>
# include <string.h>
# define MAX 81 /* 输入字符串不超过80字符,可更改 */
int cycle(char *s)
{
char *h,*t;
for(h=s,t=s+strlen(s)-1;t>h;h++,t--)
if(*h!=*t) break;
return t<=h;
}
int main()
{
char s[MAX];
system("cls");
while(1) /* 输入应不包括字符@,输入字符串的第一个字符为@,退出 */
{
printf("Please input the string:(input '@' to quit)\n");
scanf("%s",s);
if(s[0]=='@')
break;
if(cycle(s))
printf("%s is a cycle string.\n",s);
else
printf("%s is not a cycle string.\n",s);
}
return 0;
}
第三题:
# include <stdio.h>
# define N 20 /*设定人数为20,实际请更改*/
struct workerdent
{ char num[11]; /*工号超过10位时请加长*/
char name[13]; /*实际请更改,名字不超过12字符,一个汉字2个字符*/
int age;
int page;
}worker[N];
int main()
{
int i;
/*以下为输入*/
for(i=0;i<N;i++)
{printf("\nInput work number of worker %d:\n",i+1);
printf("NO.:");
scanf("%s",worker[i].num);
printf("name:");
scanf("%s",worker[i].name);
printf("age:");
scanf("%d",&worker[i].age);
printf("page:");
scanf("%d",&worker[i].page);
}
/*以下为输出*/
printf("NO. name age page\n");
for(i=0;i<N;i++)
{ printf("%-11s%-13s%-5d%-5d\n",worker[i].num,worker[i].name,worker[i].age,worker[i].page);
}
system("pause");
return 0;
}
Ⅱ 提问一道c语言的编程题,求答案
你好,代码如下:
#include<stdio.h>
typedefstructcuboid
{
intlength;
intwidth;
intheight;
}Cuboid,*cuboid;
intmain()
{
Cuboidb={15,10,5};
cuboidp=&b;
printf("长方体的长宽高分别为(通过结构体变量输出):%d,%d,%d ",b.length,b.width,b.height);
printf("长方体的长宽高分别为(通过指针输出):%d,%d,%d ",p->length,p->width,p->height);
return0;
}
运行结果:
Ⅲ 3道java编程题,求解
packageTestPerson;
/**
*(1)编写程序实现如下功能:已知Person类包含三个公共成员变量(姓名、性别、年龄)和一个构造方法,
*Student类是Person类的派生类,包含两个新的公共成员变量(学号、班号)、两个公共方法(修改年龄、显示基本信息)及一个构造方法。
*在测试类Test1中,定义一组学生对象,并初始化他们的基本信息,然后依次输出。
*/
publicclassTest1{
publicstaticvoidmain(String[]args){
Student[]student=newStudent[3];
student[0]=newStudent("小李","男",12,20181101,01);
student[1]=newStudent("小南","女",13,20001102,01);
student[2]=newStudent("小李","男",12,20181103,01);
for(Studentstu:student){
stu.showInformation();
}
}
}
classPerson{
publicStringname;
publicStringsex;
publicintage;
publicPerson(Stringname,Stringsex,intage){
super();
this.name=name;
this.sex=sex;
this.age=age;
}
}
classStudentextendsPerson{
publiclongstudentId;
publiclongclassId;
publicvoidsetAge(intage){
age=this.age;
}
publicvoidshowInformation(){
System.out.println("我的姓名是"+name+","+"我的性别是"+sex+","+"我的年龄是"+age
+"岁,"+"我的学号是"+studentId+","+"我的班号是"+classId+"班");
}
publicStudent(Stringname,Stringsex,intage,longstudentId,
longclassId){
super(name,sex,age);
this.studentId=studentId;
this.classId=classId;
}
}
不可否认,我现在是有点闲,所以我就帮你写第一个吧,至于后面两个,我就不写了,看看还有没有其他人有点闲时间,看缘分吧
运行结果:
我的姓名是小李,我的性别是男,我的年龄是12岁,我的学号是20181101,我的班号是1班
我的姓名是小南,我的性别是女,我的年龄是13岁,我的学号是20001102,我的班号是1班
我的姓名是小李,我的性别是男,我的年龄是12岁,我的学号是20181103,我的班号是1班
Ⅳ 一些简单C语言编程问题
第一个
#include<stdio.h>
main()
{
int days,day,t=1;
long msr=1,fw;//msr=陌生人,fw=富翁
scanf("%d",&days);
if(days<=30&&days>=0)
if(days==1)
fw=100000;
msr=1;
for(day=2;day<=days;day++)
{
fw=100000*day;
t=t*2;
msr=msr+t;
}
printf("%d\n%d\n",fw,msr);
}
这里的是一块钱,你把它按比例缩小就好了!
第二个定义一个结构体
struct student
{
char name[20];
float math;
……
float ave;
}
main()
{
这里对平均分判断,然后输出整个结构
}.
第三题只要4个for循环,里边一个 if判断就好了,输出所有可能的值!
Ⅳ C语言编程问题,急求答案
使用冒泡的算法,将p后面的n-p-1个元素向前交换p+1次:
#include "stdafx.h"
#include <iostream>
using namespace std;
#define N 1000
int main()
{
int n, p;
int num[N];
cout << "请输入n值和p值:" << endl;
cin >> n >> p;
cout << "请输入" << n << "个数:" << endl;
for (int i = 0; i < n; i++)
cin >> num[i];
for (int i = 0; i <= p; i++)
{
for (int j = 0; j < n - p - 1; j++)
{
int temp = num[p+j-i];
num[p + j-i] = num[p + j + 1-i];
num[p + j + 1-i] = temp;
}
}
for (int i = 0; i < n; i++)
cout << num[i] << " ";
cout << endl;
system("pause");
return 0;
}
Ⅵ c语言编程提问
#include<stdio.h>
intmain()
{
intn,i,j;
charc1,c2;
printf("要生成的正方形的大小和符号c1c2,用空格隔开");
scanf("%d%c%c",&n,&c1,&c2);
printf("输出的正方形为: ");
for(i=0;i<n;i++)
{
if(i==0||i==(n-1)){
for(j=0;j<n;j++)
{
printf("%c",c1);
}
}else{
for(j=0;j<n;j++)
{
if(j==0||j==(n-1)||j==i||j==(n-1-i))
{
printf("%c",c1);
}else{
printf("%c",c2);
}
}
}
printf(" ");
}
return0;
}
Ⅶ C语言提问:如何编程此题:给定人何以正整数n,对它进行以下操作:
#include <stdio.h>
main(void)
{
int n,t=0;
scanf("%d",&n);
while(1)
{
printf("%d->",n);
if(n%2==0)
{
n=n/2;
t++;
}
else
{
n=3*n+1;
t++;
}
if(n==1)
{printf("%d\n",n);
break;
}
}
printf("周期是:%d",t);
}
输入13
输出:13->40->20->10->5->16->8->4->2->1
周期是9
Ⅷ C语言编程习题,急需做!
1题:
#include <stdio.h>
#define M 4
void main()
{
int suzu[M][M];
int x, y;
int zu = 0;
for(x = 0; x<M; x++)
for(y = 0; y<M; y++)
{
scanf("%d",&suzu[x][y]);
}
for(x = 0; x<M; x++)
{
zu = zu + suzu[x][x];
}
printf("zu i jiao xian de he :%d\n",zu);
getch();
}
2题:
#include <stdio.h>
void main()
{
int i = 0;
int sun = 0;
int su;
for(i; i<100; i++)
{
scanf("%d",&su);
sun += su;
}
printf("sun = %d\n",sun);
getch();
}
3题:
30
3
54
16
7,7
4题:
(1):
6
(2):
-5,-12,-7
** c的写法应该这样
#include <stdio.h>
sub(int x, int y,int *z)
{
*z = y - x;
}
main( )
{
int a,b,c;
sub(10,5,&a);
sub(7,a,&b);
sub(a,b,&c);
printf("%d,%d,%d\n",a,b,c);
getch();
return 0;
}
-----------------------------------------------------
1.在有问题请核对提问是否正确 continue也写错。。
2.象这样的问题在简单不过 应该努力去学
网络 老岩 [email protected]
Ⅸ 新手提问:C语言编程题
#include<stdio.h>
double max_min(double a[],double &maxp,double &minp,int n)
{
int i;
maxp=a[0];
minp=a[0];
for(i=1;i<n;i++)
{
if(a[i]>maxp)
maxp=a[i];
}
for(i=1;i<n;i++)
{
if(a[i]<minp)
minp=a[i];
}
return maxp,minp;
}
int main()
{
int n,i;
double a[10],max,min;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%lf",&a[i]);
max_min(a,max,min,n);
printf("%.2f\n",max);
printf("%.2f\n",min);
return 0;
}
兄弟 这题你还没做完啊?
Ⅹ 有几个简单的编程问题想请各位高人解答一下!
第一题:
/* 程序名称: arrayList.c */
/* 程序目的: 用数组实现约瑟夫问题 */
#include<stdio.h>
#define size 100 /* 输入人数的上限 */
void main()
{
int person[size];
int i, j; /* 循环修正变量 */
int arrayLen; /* 数组长度 */
int start, overNum; /* 开始位置各跨过位置 */
int deleNum; /* 出列人所在数组中的下标 */
int name, total; /* 输入时,人的信息以及人的总数 */
printf( "请输入圆桌上人的总数: " );
scanf( "%d", &arrayLen ); printf( "\n" );
if( ( arrayLen > size ) || ( arrayLen < 0 ) )
{
printf( "超出范围,请重新输入: " );
scanf( "%d", &arrayLen ); printf( "\n" );
};
printf( "请输入各个人的信息(整数): \n" );
for( i = 0; i < arrayLen; i++ )
{
scanf( "%d", &name );
person[i] = name;
}
printf( "你输入的数据的顺序为: \n" );
for( i = 0; i < arrayLen - 1; i++ )
printf( " %d ==>", person[i] );
printf( "%d \n", person[arrayLen - 1] );
printf( "你打算从第几个人开始? 请输入开始号: " );
scanf( "%d", &start );
printf( "\n" );
start = start - 1;
printf( "请输入相邻两出列人之间的间隔: " );
scanf( "%d", &overNum );
printf( "\n" );
total = arrayLen;
printf( "程序运行后,出列人的顺序为:\n\n" );
for( i = 0; i < total; i++ ) /* 要打印total个人的情况,故做total次 */
{
if ( arrayLen == 1 )
printf( "%d", person[0] ); /* 如果是数组只剩一个元素,直接出列 */
else
{
deleNum = ( start + overNum - 1 ) % arrayLen; /* 此取模保证循环 */
printf( "%d ==> ", person[deleNum] );
for ( j = deleNum; j < arrayLen; j++ ) /* 将出列元素后面的各元素前移 */
person[j] = person[j+1];
start = deleNum;
arrayLen = arrayLen - 1; /* 移动完毕后,数组长度减1 */
}
}
printf( "\n\n" );
}