簡單編程題
1.
#include
<stdio.h>
int
main()
{
int
y0,
m0,
d0,
y1,
m1,
d1,
age;
while
(
scanf("%d%d%d%d%d%d",
&y0,
&m0,
&d0,
&y1,
&m1,
&d1
)
){
age
=
y1
-
y0
-
1;
if
(
m1
>
m0
||
m1
==
m0
&&
d1
>=
d0
)
++age;
printf("年齡為:%d周歲!\n",
age);
}
return
0;
}
4.
#include
<stdio.h>
#include
<memory.h>
int
main()
{
char
p[500];
int
i,
count;
while
(
scanf("%s",
&p)
){
count
=
0;
for
(
i
=
0;
i
!=
strlen(p);
++i
)
if
(
p[i]
>=
'a'
&&
p[i]
<=
'z'
)
++count;
printf("%d\n",
count);
}
return
0;
}
2.
#include
<stdio.h>
int
main()
{
int
n;
while
(
scanf("%d",
&n)
){
if
(
(
n
&
1
)
==
0
)
printf("%d是偶數!\n",
n);
else
printf("%d,是奇數!\n",
n);
}
return
0;
}
第三題(用EFO結束)?EOF吧?EOF已經是文件尾,怎樣輸出結果?
❷ 跪求大神解答兩道簡單的Delphi編程題,小弟拜託了,考試了!
編程題1:
procereTForm1.FormActivate(Sender:TObject);
constMAX_LINE=100;
vari:Integer;
begin
randomize();
withCanvasdo
begin
fori:=1toMAX_LINEdo
begin
巧春Pen.Color:=random(65536);
LineTo(random(ClientRect.Right-ClientRect.Left),random(ClientRect.Bottom-ClientRect.Top));
sleep(500);
Application.ProcessMessages;
end;
end;
end;
運行結果:
Withqry1do
begin
//添加記錄
Append;
Fieldbyname('欄位名').Value:=值;
post;
//添加記錄
Edit;
Fieldbyname('欄位名').Value:=值;
post;
//刪除記錄
DeleteRecords(arCurrent);
end;
❸ 急急急@!!!簡單C語言編程題,
#include<stdio.h>
double
Rectang(double
x,double
y)
{
double
Perimet,Area;
Perimet=(x+y)*2;
//根據長和寬計算周長
Area=x*y;
//計算面積
printf("長方形緩念周長為:%g\n",Perimet);
//輸出計算結果
printf("長方形面積為:%g\n",Area);
}
main()
{
float
x,y;
printf("請輸入長方形的長和寬,用空格隔開!例如
4
5\n");
//顯示配神提示培哪虧信息
scanf("%f
%f",&x,&y);
//輸入長方形的長和寬
Rectang(x,y);
//調用自定義函數計算長方形的周長和面積
return
1;
}
❹ 很簡單的C語言題目
二、填空題
1.答:7
因為
'H'(ASCII) -'桐源A'(ASCII) = 7(十進制)
7(十進制)+'0'(ASCII) = '7'(ASCII)
注:原文print錯誤,是printf
2.答:3
因為p/3>0,所以這個表達式返回p/10即3
3.答:b
因為'B'(ASCII) = 66(十進制),'B'+ 32 = 98(即ASCII的b)
三、編程題
1.列印的結果為9,11,9,10
因為
【m=++i;】等效於【i=i+1,m=i;】
【n=j++;】等效於【n=j,j=j+1;】
二、填空題
1要求給i賦10,給j賦20,則應該從鍵盤輸入 i=10,j=20。
因為scanf("i=%d,j=%d",&i,&j);
同理,如果scanf("%d,%d",&i,&j); 只要輸入10,20即可
注:原文scanf("i=%d,j=%d";&i,&j);語句中&i前的分號是錯的,應用逗號。
2. 以下程序的輸出結果是 2,1(加個換行)。
a=a+b;//a=3,b=2
b=a-b;//a=3,b=1
a=a-b;//a=2,b=1
三、編程題
1. 設圓半徑r=1.5,高h=3,求局升態圓周長、圓面積、圓球表面積、圓球體積、圓柱體積。用scanf輸入數據,輸出計算結果,輸出時要求有文字說明,取小數點後2位數字。請編程序。
答:
//圓周長:2*PI*r
//圓面積:PI*r*r
//圓球表面積:4*PI*r*r
//圓球體積:4/3*PI*r*r*r
//圓柱體積:PI*r*r*h
#include <stdio.h>
#define PI 3.14159f//定義π
float r;//半徑
float h;//高
void main(void)
{
printf("請輸入圓半徑,以回車確認:");
scanf("%f",&r);
printf("請輸入高,以回車確認:");
scanf("%f",&h);
printf("圓周長為:%.2f\n", 2*PI*r);
printf("圓面積為:%.2f\n"笑螞, PI*r*r);
printf("圓球表面積為:%.2f\n", 4*PI*r*r);
printf("圓球體積:%.2f\n", 4/3*PI*r*r*r);
printf("圓柱體積:%.2f\n", PI*r*r*h);
}
❺ 兩道簡單的C語言編程題 1.設給定三個整數a.b.c,試寫出尋找其中數的一個演算法,並分析在平均情況
先回答第一個問題:
#include<stdio.h>
#include<conio.h>
intmain(){
inta,b,c,d;
printf("Inputa,b,c:");
scanf("%d,%d,%d",&a,&b,&c);
if(a>=b){
if(b>=c)d=b;//a>=b>=c,比較2次
elseif(a>=c)d=c;//a>=c>b,比較3次
elsed=a;//c>a>=b,比較3次
}else{
if(a>=c)d=a;//b>a>=c,比較2次
elseif(b>=c)d=c;//b>=c>a,比較3次
elsed=b;//c>b>a,比較3次
}//平均比較次數:(2+3+3+2+3+3)/6=8/3次,最壞比較次數:3次
printf("ZhongShu=%d Finished! ",d);
getch();
return0;
}
平均比較8/3次,最壞比較3次。第二個問題:
#include<stdio.h>
#include<conio.h>
intBinMax(inta[],intlow,inthigh){//二分查找最大值,low、high為查找范圍下標
if(low>high){
printf(" BinMaxError! ");
return-32768;//范圍出錯,提示,並返回整型最小值
}
if(low==high)returna[low];
if(low==high-1)return(a[low]>a[high]?a[low]:a[high]);
intmid=(low+high)/2,m1,m2;
m1=BinMax(a,low,mid);//找前半部的最大值
m2=BinMax(a,mid+1,high);//找後半部的最大值
return(m1>m2?m1:m2);
}
intmain(){
inta[9]={3,6,2,5,9,1,8,7,10},max;//元素個數不一定要滿足n=2^k
max=BinMax(a,0,8);
printf("max=%d Finished! ",max);
getch();
return0;
}
都能看懂吧?希望能幫到你!
❻ 一 個簡單的編程題目
#include <iostream.h>
int A[15]={14,1,2,3,4,5,6,7,8,9,10,11,12,13,14};
/*A[1..14];為了下標從1開始,我用15個空間存14個數,第一個空間存數組長度
程序運行不結束是正確的因為14個數的全排列要1278945280個輸出
*/
void Write(int a[])
{
// output the the elements of an array;
//input : an array
for(int i = 1;i<=14;i++)
cout<<a[i]<<'\t';
cout<<endl;
sum++;
}
void HeapPermute(int n)
{
/*實現生成全排列的Heap演算法
輸入全局數組A[1..n]
輸出 A的全排列*/
if (n==1)
Write(A);
else
for(int i = 1;i<=n;i++)
{
HeapPermute(n-1);
if (n%2 != 0)
Swap(A[1],A[n]);
else
Swap(A[i],A[n]);
}
}
int main()
{
HeapPermute(14);
//cout<<fun(6);
//cout<<sum<<endl;
//cout<<SearchMin(A,1,5)<<endl;
return 0;
}
❼ C語言指針一道簡單的編程題
按照你的要求編寫的C語言程序如下
include<stdio.h>
intmain()
{
inta,b;
int*p=&a;
a=30;
printf("a=%d ",*p);
p=&b;
scanf("%d",p);
a=*p+24;
printf("a=%d,b=%d",a,b);
return0;
}
運行結果
a=30
76
a=100,b=76
❽ 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();
}
}
❾ 一道簡單的python編程題
按照題目要求編寫的哥德巴赫猜想的Python程序如下
def IsPrime(v):
if v>=2:
for i in range(2,v//2+1):
if v%i==0:
return False
else:
return True
else:
return False
n=int(input("輸入一個正偶數:"))
if n>2 and n%2==0:
for i in range(1,n//2+1):
if IsPrime(i)==True and IsPrime(n-i)==True:
print("%d=%d+%d" %(n,i,n-i))
else:
print("輸入數據出錯!")
源代碼(注意源代碼的縮進)
❿ 初學者請教 兩道簡單的c語言編程題目
[回答]
1. 用C語頃春言自己的時間函數
difftime(time_t time1, time_t time0) / 86400
這樣做得缺點是,這里的time_t類型,只支持到2037年
2. 自己定義函數
樓主用的方法,在實際編程里不常用,變化比較大。
下面這個方法是直接計算兩個日期到公元元年的天數
然後相減,這個是實際編程時的演算法,經過很多程序員的洗煉,已經被廣泛使用
#include<stdio.h>
#define LEAP(y) (y%(y%100?4:400)==0) /*公元y年是否閏年*/
long totaldays(int year,int month,int day)/*公元紀年的總天數*/此汪
{ int days[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int c=day,m,y=year-1;
days[2]=28+LEAP(year);
for(m=1;m<month;m++)c+=days[m];
return c+y/4-y/100+y/400+y*365L;
}
void main()
{ int y,m,d; long t1,t2;
printf("date 1: ");
scanf("%d%*c%d%*c%d",&y,&m,&d);
t1=totaldays(y,m,d);
printf("date 2: ");
scanf("%d%*c%d%*c%d",&y,&m,&d);
t2=totaldays(y,m,d);
printf("total days: %ld\n",t2-t1);
}
這應該是全部的實現方法了
樓主的方法沒必要簡化了
尤其是刻意追求短代碼就更沒有必要了
要知道雀扒耐,代碼短了,可讀性就差
兩者協調好,才能稱為好程序^^
悉雨辰寂