編程題提問
所有程序在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" );
}