常见编程题
‘贰’ 几道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();
}
}