常見編程題
『貳』 幾道c語言編程題
1.
#include<stdio.h>
#define N 100
void main()
{
int a,b,*p,maxLen,temp,i,k,arr[N]={0};
int linkrun(int a,int b,int *p);
printf("please input two integer:\n");
scanf("%d %d",&a,&b);
if (a>b) { temp=a; a=b; b=temp; }
p=arr;
maxLen=linkrun(a,b,p);
printf("sequence length is \n");
for (i=0;i<b-a+1;i++)
printf("%d \n",p[i]);
printf("\nMax Length is %d\n",maxLen);
}
int linkrun(int a,int b,int *p)
{
int i,j,n,max=0;
for (i=a;i<=b;i++)
{
n=i; j=1; //printf("---%d ",n);
while(n>1)
{
if (n%2==0)
n=n/2;
else
n=3*n+1;
j++; //printf("---%d ",n);
}
p[i-a]=j;
//printf("\n");
if (j>max) max=j;
}
return max;
}
2.
#include <stdio.h>
#include <string.h>
#define N 80
void main()
{
char s[N],s1[N],s2[N];
void inv(char b[]);
void merage(char a[],char b[],char c[]);
printf("please input the first string: ");
gets(s1);
printf("please input the second string: ");
gets(s2);
inv(s1);
printf("\nThe first string after inverting:\n");
printf("%s",s1);
merage(s1,s2,s);
printf("\nThe string after meraging:\n");
printf("%s",s);
}
void inv(char b[]) //將b指向的字元串逆序存放
{
int i=0, len=strlen(b);
char temp;
while(i<len)
{
len--;
temp=b[i];
b[i]=b[len];
b[len]=temp;
i++;
}
}
void merage(char a[], char b[], char c[])//合並字元串
{
int lena=strlen(a),lenb=strlen(b), len=0;
int i=0,j=0,k=0;
while(i<lena && j<lenb)
{
c[k]=a[i]; i++; k++;
c[k]=b[j]; j++; k++;
}
if (i>=lena)
{
while(j<lenb)
{
c[k]=b[j];
j++;
k++;
}
}
else
{
while(i<lena)
{
c[k]=a[i];
i++;
k++;
}
}
c[k]='\0';
}
3.
#include <stdio.h>
#define N 100
double fun(int n,int a[])
{
int i,k;
double sum=0;
k=0;
for(i=2;i<n;i++)
{
if(n%i==0)
{
printf("%d,",i);
a[k]=i; k++;
}
}
for (i=0;i<k;i++)
sum=sum+1.0/a[i];
return (sum);
}
void main()
{
int n,arr[N]={0};
double d;
printf("please input a integer less than 100: ");
scanf("%d",&n);
while(n>=100)
{
printf("the integer less than 100: ");
scanf("%d",&n);
}
d=fun(n,arr);
printf("\nsum=%lf \n",d);
}
4.
#include <stdio.h>
#define N 10
int max,min;
int fun(int *a,int n)
{
int i,maxi,mini;
max=min=a[0];
maxi=mini=0;
for(i=1;i<n;i++)
{
if(a[i]>max)
{
max=a[i];
maxi=i;
}
if(a[i]<min)
{
min=a[i];
mini=i;
}
}
a[maxi]=min;
a[mini]=max;
}
void main()
{
int i,n,arr[N]={0};
printf("please input the number of array elements: ");
scanf("%d",&n);
printf("\nplease input %d integers: ",n);
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
printf("\n");
fun(arr,n);
for (i=0;i<n;i++)
printf("%d ",arr[i]);
printf("\nmax=%d min=%d\n",max,min);
}
5.
#include <stdio.h>
#include <string.h>
#define N 80
void main()
{
char a[N]= "ABCDEFGHIJK";
int m;
void fun(char *w,int m);
printf("please input a string: ");
gets(a);
printf("The original string:\n");
puts(a);
printf("Enter m: ");
scanf("%d",&m);
fun(a,m);
printf("\nThe string after moving:\n");
puts(a);
}
void fun(char *w,int m)
{
char b[N];
int i,j=0;
for(i=0;i<m;i++)
{
b[j]=w[i];
j++;
}
for (i=0;i<strlen(w)-m;i++)
w[i]=w[i+m];
for(j=0;j<m;j++)
{
w[i]=b[j];
i++;
}
w[i]='\0';
}
6.
#include <stdio.h>
#include <string.h>
#define SIZE 5
struct student
{
char num[6];
char name[6];
int age;
}stu[SIZE];
main()
{
int i;
void fun();
/*輸入*/
printf("please input %d students \n",SIZE);
for (i=0;i<SIZE;i++)
{
printf("學號: ");
scanf("%s",stu[i].num);
printf("姓名: ");
scanf("%s",stu[i].name);
printf("年齡: ");
scanf("%d",&stu[i].age);
}
fun();
for (i=0;i<SIZE;i++)
printf("學號:%s 姓名:%s 年齡:%d \n",stu[i].num,stu[i].name,stu[i].age);
printf("the result after sorting is:\n");
for (i=0;i<SIZE;i++)
printf("學號:%s 姓名:%s 年齡:%d \n",stu[i].num,stu[i].name,stu[i].age);
}
void fun()
{
int i,j;
struct student stu_temp;
for(i=0;i<SIZE;i++)
{
for (j=i+1;j<SIZE;j++)
if (strcmp(stu[i].name,stu[j].name)<0)
{
stu_temp=stu[i];
stu[i]=stu[j];
stu[j]=stu_temp;
}
if (strcmp(stu[i].name,stu[j].name)==0)
{
if (stu[i].age<stu[j].age)
{
stu_temp=stu[i];
stu[i]=stu[j];
stu[j]=stu_temp;
}
}
}
}
給我追加分數吧~~咱一個學校的應該!
『叄』 問幾個c語言的編程題
所有程序在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;
}
『肆』 基礎編程題
LZ想要的是這種答案吧。。。。
//-------------------------------第一題
#include <stdio.h>
#include "e:\myc\zylib\zylib.h"
STRING GetString(STRING prompt);
double GetReal(STRING prompt);
int main()
{
double bookprice;
STRING bookname;
bookname=GetString("請輸入字元串:");
bookprice=GetReal("請輸入實數:");
printf("字元串為:%s\n",bookname);
printf("實數為:%.2f\n",bookprice);
}
STRING GetString(STRING prompt)
{
STRING name;
printf("%s",prompt);
name=GetStringFromKeyboard();
return name;
}
double GetReal(STRING prompt)
{
double price;
printf("%s",prompt);
price=GetRealFromKeyboard();
return price;
}
//-------------------------------------第二題
#include <stdio.h>
#include "e:\myc\zylib\zylib.h"
BOOL IsPrime(int n);
int main()
{
int n;
printf("請輸入一個整數:");
scanf("%d",&n);
if(n>2)
if(IsPrime(n))printf("%d是素數\n",n);
else printf("%d不是素數\n",n);
else printf("數據非法\n");
return 0;
}
BOOL IsPrime(int n)
{
int i;
for(i=2;i<n;i++)
if(n%i= =0) return FALSE;
return TRUE;
}
//--------------------------------第三題
#include <stdio.h>
#define TRUE 1
int gcd(int x,int y);
int main()
{
int m,n,max;
printf("請輸入兩個正整數:");
scanf("%d %d",&m,&n);
max=gcd(m,n);
printf("最大公約數為:%d\n",max);
return 0;
}
int gcd(int x,int y)
{
int r;
while(TRUE)
{
r=x%y;
if(r==0)break;
x=y;
y=r;
}
return y;
}
//--------------------------------第四題
#include <stdio.h>
#include "e:\myc\zylib\zylib.h"
typedef enum{sun,mon,tue,thi,wen,fri,sat}WEEKDAY;//定義枚舉類型
int GetInteger(STRING prompt);//輸入一下整數
int Count(int year,int month);//計算某年某月之前到2007年1月1日的天數
BOOL IsLeapYear(int n);//判斷某年是否是閏年
int month_day(int year,int month);//計算某個月的天數
void print(int year,int month,int total);//列印某年某月的日歷
void print1(WEEKDAY weekday);//列印某月的第1天
int main()
{
int year,month,total;
year=GetInteger("please input year:");
if(year<2007)
PrintErrorMessage(FALSE,"年份小於2007,錯誤\n");
month=GetInteger("please input month:");
total=Count(year,month);
print(year,month,total);
}
int GetInteger(STRING prompt)
{
int t;
printf("%s",prompt);
t=GetIntegerFromKeyboard();
return t;
}
int Count(int year,int month)
{
int s,i;
s=0;
for(i=2007;i<year;i++)
if(IsLeapYear(i))s+=366;
else s+=365;
for(i=1;i<month;i++)
s+=month_day(year,i);
return s;
}
BOOL IsLeapYear(int n)
{
return n%4==0&&n%100!=0||n%400==0;
}
int month_day(int year,int month)
{
int day;
switch(month)
{
case 1:
case 3:
case 5:
case 7:
case 9:
case 10:
case 12:day=31;break;
case 2:day=28+IsLeapYear(year);break;
default:day=30;
}
return day;
}
void print(int year,int month,int total)
{
WEEKDAY weekday;
const WEEKDAY first=mon;
int i,day;
printf("%d-%d canlendar\n",year,month);
printf("-----------------------------------\n");
printf(" sun mon tue thi wen fri sat\n");
printf("-----------------------------------\n");
day=month_day(year,month);
for(i=1;i<=day;i++)
{
weekday=(WEEKDAY)((total+i+first-1)%7);
if(i==1)print1(weekday);
else if(weekday==sat)
printf("%4d\n",i);
else printf("%4d",i);
}
printf("\n------------------------------------\n");
}
void print1(WEEKDAY weekday)
{
if(weekday==0)printf("%4d",1);
else if(weekday==1)printf("%8d",1);
else if(weekday==2)printf("%12d",1);
else if(weekday==3)printf("%16d",1);
else if(weekday==4)printf("%20d",1);
else if(weekday==5)printf("%24d",1);
else if(weekday==6)printf("%28d\n",1);
}
//---------------------------------------
上面的一些文件路徑你自己改了,唉,其實我自己給你寫的那些演算法更好,。
『伍』 c語言編程題經典題目
編寫一個程序,求出1~100之間所有的素數。
編寫一個程序早態友,從鍵盤輸入一個字元串,輸出該字元串中所有的單詞。
編寫一個程序,將輸入的字元串反轉後輸出。
編寫一個程序,求出兩個整數的最大公約數和陸槐最小公倍數。
編寫一個程序,從鍵盤輸入一組數據,對這組數據進行排序並輸出結果。
編寫一個程序,輸入一個正整數,輸出其各位數字的和。
編寫一個程序,求出一個數組中的最大值和最小值。
編寫一個程序,從鍵盤輸入一個字元串,判斷該字元串是否為迴文字元串。
編寫一個程序,實現二分查找演算法。
編寫一個程序,實現快速排序演算法。
這些題目涵蓋閉顫了常見的演算法和數據結構,對於提高編程能力和解決實際問題都非常有幫助。在實際開發中,也可以參考這些題目的思路和實現方式,加快開發進度和提高程序質量。
『陸』 幾道關於C語言的編程題
第三題:已運行過(vc++6.0):
#include "stdio.h"
void main()
{
int i;
int positive=0,negative=0,zero=0;
int ch[20];
for(i=0;i<20;i++)
{
printf("請輸入第%d個整數:",20-i);
scanf("%d",&ch[i]);
if(ch[i]>0)
positive++;
else if(ch[i]==0)
zero++;
else
negative++;
}
printf("正數%d個,零%d個,負數%d個\n",positive,zero,negative);
}
第四題:
#include"stdio.h"
float stratum(int num)
{
int i;
float sum=1;
for(i=1;i<=num;i++)
{
sum=sum*i;
}
return sum;
}
void main()
{
int i;
float sum=0;
for(i=1;i<=10;i++)
{
sum+=stratum(i);
}
printf("1!+2!+3!+...+10!=%f",sum);
}
『柒』 5道簡單的java編程題(高分懸賞)
很詳細的幫你寫下,呵呵,所以要給分哦!
1、
(1)源程序如下:
public class One {
public static void main(String[] args) {
String name = "張三";
int age = 23;
char sex = '男';
String myclass = "某某專業2班";
System.out.println("姓名:" + name);
System.out.println("姓名:" + age);
System.out.println("姓名:" + sex);
System.out.println("姓名:" + myclass);
}
}
(2)
編寫完程序的後綴名是.java,如本題,文件名就是One.java。
開始\運行\cmd,進入「命令提示符窗口」,然後用javac編譯器編譯.java文件,語句:javac One.java。
(3)
編譯成功後,生成的文件名後綴是.class,叫做位元組碼文件。再用java解釋器來運行改程序,語句:java One
2、編寫程序,輸出1到100間的所有偶數
(1)for語句
public class Two1 {
public static void main(String[] args) {
for(int i=2;i<=100;i+=2)
System.out.println(i);
}
}
(2)while語句
public class Two2 {
public static void main(String[] args) {
int i = 2;
while (i <= 100) {
System.out.println(i);
i += 2;
}
}
}
(3)do…while語句
public class Two3 {
public static void main(String[] args) {
int i = 2;
do {
System.out.println(i);
i += 2;
}while(i<=100);
}
}
3、編寫程序,從10個數當中找出最大值。
(1)for循環
import java.util.*;
public class Three1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int number;
int max = 0;
for (int i = 0; i < 10; i++) {
System.out.print("輸入第" + (i + 1) + "個數:");
number = input.nextInt();
if (max < number)
max = number;
}
System.out.println("最大值:" + max);
}
}
(2)while語句
import java.util.*;
public class Three2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int number;
int max = 0;
int i = 0;
while (i < 10) {
System.out.print("輸入第" + (i + 1) + "個數:");
number = input.nextInt();
if (max < number)
max = number;
i++;
}
System.out.println("最大值:" + max);
}
}
(3)do…while語句
import java.util.*;
public class Three3 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int number;
int max = 0;
int i = 0;
do {
System.out.print("輸入第" + (i + 1) + "個數:");
number = input.nextInt();
if (max < number)
max = number;
i++;
}while(i<10);
System.out.println("最大值:" + max);
}
}
4、編寫程序,計算從1到100之間的奇數之和。
(1)for循環
public class Four1 {
public static void main(String[] args) {
int sum=0;
for(int i = 1;i<=100;i+=2){
sum+=i;
}
System.out.println("1~100間奇數和:" + sum);
}
}
(2)while語句
public class Four2 {
public static void main(String[] args) {
int sum = 0;
int i = 1;
while (i <= 100) {
sum += i;
i += 2;
}
System.out.println("1~100間奇數和:" + sum);
}
}
(3)do…while語句
public class Four3 {
public static void main(String[] args) {
int sum = 0;
int i = 1;
do {
sum += i;
i += 2;
} while (i <= 100);
System.out.println("1~100間奇數和:" + sum);
}
}
5、
(1)什麼是類的繼承?什麼是父類?什麼是子類?舉例說明。
繼承:是面向對象軟體技術當中的一個概念。如果一個類A繼承自另一個類B,就把這個A稱為"B的子類",而把B稱為"A的父類"。繼承可以使得子類具有父類的各種屬性和方法,而不需要再次編寫相同的代碼。在令子類繼承父類的同時,可以重新定義某些屬性,並重寫某些方法,即覆蓋父類的原有屬性和方法,使其獲得與父類不同的功能。另外,為子類追加新的屬性和方法也是常見的做法。繼承需要關鍵字extends。舉例:
class A{}
class B extends A{}
//成員我就不寫了,本例中,A是父類,B是子類。
(2)編寫一個繼承的程序。
class Person {
public String name;
public int age;
public char sex;
public Person(String n, int a, char s) {
name = n;
age = a;
sex = s;
}
public void output1() {
System.out.println("姓名:" + name + "\n年齡:" + age + "\n性別:" + sex);
}
}
class StudentPerson extends Person {
String school, department, subject, myclass;
public StudentPerson(String sc, String d, String su, String m, String n,
int a, char s) {
super(n, a, s);
school = sc;
department = d;
subject = su;
myclass = m;
}
public void output2() {
super.output1();
System.out.println("學校:" + school + "\n系別:" + department + "\n專業:"
+ subject + "\n班級:" + myclass);
}
}
public class Five2 {
public static void main(String[] args) {
StudentPerson StudentPersonDemo = new StudentPerson("某某大學", "某某系別",
" 某專業", "某某班級", " 張三", 23, '男');
StudentPersonDemo.output2();
}
}