當前位置:首頁 » 編程軟體 » 入門編程題

入門編程題

發布時間: 2023-03-29 08:27:34

⑴ dev-c++入門級編程題一道

#include <iostream>
#include <algorithm>
using namespace std;
#define MAXN 1010
bool f[MAXN];
int n;
int a[MAXN];
void sushu()
{
int i,j;
memset(f,1,sizeof(f));
f[0] = f[1] = 0;
f[2] = 1;
f[3] = 1;
for( i = 4 ; i < MAXN ; i+=2 )
f[i] = 0;
for( i = 3 ; i < MAXN ; i+=2 )
{
if( f[i] )
{
for( j = i<<1 ; j < MAXN ; j+=i )
{
f[j] = false;
}
}
}
}

int main()
{
sushu();
int temp,i,j;
while( scanf("%d",&n)!=EOF)
{
for( i = 0 , j = 0 ; i < n; i++)
{
scanf("%d",&temp);
if( !f[ temp ] ) continue;
a[j++] = temp;
}
sort(a,a+j);
for( i = 0 ; i < j-1; i++)
{
printf("%d ",a[i]);
}
printf("%d\n",a[i]);
}
return 0;
}

c語言入門函數編程題(調用函數int Max(int a,int b)求兩整數最大值

#include <stdio.h>
int Max(int a, int b); // 定義函數Max
void main() //無返回值主函數
{
int x, y, z; //定義整型變數x,y,z
printf("Please input two integers : "); //讓用戶輸入兩個整數,務必要用英文輸入法輸入「,」
scanf("%d,%d",&x, &y); //將用戶輸入的整型數分別存放到x,y中
if(x==y) //判斷x是否等於y,是則輸出「這兩個整數相等」
{
printf("The two integers are equal!");
}
else //否則,將x,y的值放入實參中並執行Max函數中的內容
{
z = Max(x, y); //實參
printf("The biger number is %d\n", z); //向屏幕輸出較大的數
}
}
int Max(int a, int b) //形參
{
int c = 0; //定義整型變數c
c = a>b?a:b; //用三目運算符將a,b值中大的賦值給c
return c; //返回c的值
}
//望採納^_^

⑶ c#編程題,基礎的急用謝謝

1、判斷是否子串:IndexOf,比如判斷T是否S子串,寫法是int idx = S.IndexOf(T);此時,如果idx返回不小於0的數值,則代表T是S的子串;如果不是,則返回-1。
2、idx就是你要的起始位置。
3、授人以魚不如授人以漁,所以代碼你就自己寫吧,總共只用到了IndexOf這一個方法。

⑷ 求解一道C語言基礎編程題。。

intbase(intk,chars[])//按定義,k是進制(2~16),s是輸入的字元串

{

int len=0,sum=0; //len 指s字元串長度,sum指轉換後的k進制數

int i,j,t,n;

while (s[len]!='\0') len++; //計算s數組長度,也就查看用戶從鍵盤輸入了多少字元

i=len-1; //從最高位開始,也就是從最右側開始計算,比如s=1234ab,先從b開始計算

while (s[i]!='\0') //逐個讀取字元串s,第i位的具體字元值,s[i]是否有效

{

n=0; //s[i]是字元(char),s[i]轉換成10進制對應的值

if (s[i]>='0'&&s[i]<='9') //查ascii碼表

n=s[i]-48; //48即字元 '0'

else if (s[i]>='A'&&s[i]<='F') //A--F用來代表10進制下的10~15

n=10+s[i]-'A'; //s[i]是字元(char),s[i]轉換成10進制對應的值

t=1;

for (j=0; j<len-i-1; j++) t=t*k;

/* //以上語句等於如下形式,s[i]對應的倍率,假設k=10,就好理解

t=1; //個位,即i=len-1時

t=1*k; //十位 即i=len-2時

t=1*k*k; //百位

t=1*k*k*k; //千位

.....

*/

sum += n*t; //合計,個+十+百+千+...

i--;

}

return (sum);

}

main()

{

int sz;

char str[20];

scanf("%d %s",&sz,str);

if (sz<2||sz>16)

printf("輸入錯誤。\n");

else

printf("%d\n",base(sz,str));

}

⑸ 初學編程,大家幫忙看下這道c語言題怎麼做萬分感謝

先給你第一題的,網路知道的這個編輯器真的不適合粘貼代碼

#include<stdio.h>

#include<string.h>

#define MAX_ARRAY_SIZE 1024

#define MAX_MAP_SIZE 10


/* 輸入數組,連續輸入,如:aedabcdaeas */

int inputArray(char *buff) {

int len = 0;

/* 使用fgets來防止緩沖區溢出 */

if (NULL == fgets(buff, MAX_ARRAY_SIZE, stdin)) {

return 0;

}

len = strlen(buff);

/* fgets 返回的數據可能是換行符結尾的,也可能不是,對換行符結尾的進行處理 */

if (buff[len - 1] == ' ') {

buff[len - 1] = '';

len -= 1;

}

return len;

}

int processArray(int len, char *chars, char *map) {

/* 保存反向映射便於查找 */

int tmap[128];

int maplen = 0;

int i = 0;

char *p = chars;

memset(tmap, -1, sizeof(int) * 128);

for (i = 0; i < len; i++) {


if (*p > 'z' || *p < 'a') {

return -*p;

}

if (tmap[*p] == -1) {

if (maplen >= MAX_MAP_SIZE) {

return -1;

}

tmap[*p] = maplen;

map[maplen] = *p;

maplen += 1;

}

*p = '0' + tmap[*p];

p++;

}

return maplen;

}

int main() {

/* 用於輸入的字元數組 */

char buff[MAX_ARRAY_SIZE];

/* 用於保存轉換規則的數組 */

char map[MAX_MAP_SIZE];

/* 保存字元數組長度 */

int len = 0;

int maplen = 0;

int i = 0;


len = inputArray(buff);


if (len <= 0) {

puts("Cancelled");

} else if (len < 10) {

puts("Not enough 10 chars");

} else {


maplen = processArray(len, buff, map);

if (maplen >= 0) {

puts("轉換結果:");

for (i = 0; i < len; i++) {

printf("%c ", buff[i]);

}

puts("");

puts("映射規則:");

for (i = 0; i < maplen; i++) {

printf("%c -> %d ", map[i], i);

}

puts("");

} else if (maplen == -1) {

puts("Different Chars count is OverLimit of 10");

} else if (maplen <= -2) {

printf("Unexpected char %c ", -maplen);

}

}

return 0;

}

執行結果:

java入門編程題:某班有十位同學,請順序輸入十位同學的學號,保存在數組中,並輸出所有同學的學號

import java.util.Scanner;

public class Students {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String[] students=new String[10];
String No=null;
for (int i = 0; i <10 ; i++) {
System.out.println("信笑請瞎鏈輸入學號:");
No=in.next();
students[i]=No;
}
System.out.println("滑神含學號是:");
for (String a:students) {
System.out.print(a+" ");
}
}
}

⑺ 求解一道C語言基礎編程題。

首先看一下程序的邏輯(雖然貌似題主應該不是在這一塊有問題:

首先得知道每個字元和數值的對應關系(圖不清晰可看戳這里:ascii編碼對應表

好了,現在看程序中的第一個if語句,在用大於、小於這些鎮絕悔運算比較符比較char的時候,會自動轉換為整數比較,也就是說『0』會轉換成48,『1』轉換成49……以此類推,最後是『9』轉換成57,你會發現把這些char減去48就會得到它們各自相對應的整數數值,這就是第一個if裡面減去48的目的。同理,接下來的else-if語句,『A』到『F』也會轉換成整數數值,具體對應的數值可以參看ascii表,一樣的道理減去『A』然後加10的目的也是轉換成數值,因為在大於10的進制下,A代表10,B代表11……以此類推,因為這個程序最高就16進制了,所以判斷到F就可以了。

然後我們來看進制的解釋:

所謂進制,其實就是組合數字的方式,理解了這一點就很好說了。比如說10進制,為什麼198等於198(好像很傻一問題)?其實是因宏並為在十進制下,198 (10) = 1 * 10^2 + 9 * 10^1 + 8 * 10^0 = 198(好像是這么回事,(。ì _ í。)),同一個數字,放在不同的位置,它所代表的分量也不一樣,即組合數字的方式會影響數字的值,1後面還有2個數字,所以這個1實際上是1 * 10^2 = 100,而不是1,其它位置的數字同理,然後把這些值加起來,就得到了整個數字所代表的最終的值,因此我們才有了 198 = 198(好像很有道理)。

但是,198也可能不等於198,什麼時候不等於呢?在不同的進制下。比如說假如我們的這個198是在16進制下的198,那麼 198 (16) = 1 * 16^2 + 9 * 16^1 + 8 * 16^0 = 408 (10) = 408。

為什麼會產生這種差別呢?因為16進制下的那個1代表的分量是1 * 16^2了,而不是1 * 10^2了,同理,在其它進制下只需要把乘的數字換成對應的進制的數就好了,比如在9進制下那個1就是1 * 9^2等等。

這樣一來上面程序裡面的for語句就好理解了,之所以用for是因為要算出次方(這個應該不用解釋),一個數要乘的次方是它後面跟著的數字的個數,所以是「j = 0; j < len - 1」。

然後把這些值加起來,就得到這個數字對應的十進制下的數值,也就完成了最終的轉換。

題主可以隨便寫些不同進制下的數字,然後自己算出十進制下對應的數值,和網站上得出的結果比較比較,這樣也可以加深對進制的理解,同時提高計算能力。

戳這里:在線進制轉換

這里給出了一御正個鏈接,這種網頁到處都是,隨便搜一下就可以找到。

⑻ 基礎編程題

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);
}
//---------------------------------------
上面的一些文件路徑你自己改了,唉,其實我自己給你寫的那些演算法更好,。

熱點內容
鍵盤按f9鍵不可以編譯怎麼調 發布:2025-07-18 00:11:34 瀏覽:312
安卓手機的廣告如何刪除 發布:2025-07-18 00:10:50 瀏覽:105
linux安裝composer 發布:2025-07-18 00:04:52 瀏覽:241
地址存儲器的容量 發布:2025-07-17 23:42:56 瀏覽:167
win7電腦用戶名和密碼在哪裡查詢 發布:2025-07-17 23:39:44 瀏覽:476
安卓手機顏色怎麼變色 發布:2025-07-17 23:26:43 瀏覽:375
java離線安裝 發布:2025-07-17 23:23:31 瀏覽:377
位置伺服器地址是什麼 發布:2025-07-17 23:18:00 瀏覽:841
phpif一行 發布:2025-07-17 23:12:27 瀏覽:730
安裝腳本精靈 發布:2025-07-17 23:11:35 瀏覽:127