① 如何用c語言編程列印2-170之間的所有素數,要求每行輸出13個素數。
#include <stdio.h>
#include <math.h>
int ss(int n) /*檢查n是否為素數,如果是則返回1,否則返回0*/
{
int i;
for (i=2; i<=(int)sqrt((double)n); i++)
if (n%i==0) return 0;
return 1;
}
int main(void)
{
int i,j=0;
for (i=2; i<=170; i++)
if(ss(i))
{
j++;
printf("%5d",i);
if (j%13==0) printf("\n");
}
printf("\ntotal prime=%d\n",j);
return 0;
}
② C語言編程
你這樣的程序會出現數據段溢出的。char*
p
沒有指向的空間,是個野指針。怎麼能拿來賦值。C語言里的字元串是用連續的字元來表示的,'\0'表示結束。比如你一個"abcde",在物理空間上是abcde0('\0'的機器值就是0.)每個字元都是一個位元組,用ASCII碼表示。C語言定義字元串的方式有兩個:(1)char*
p
=
"abced";這是定義一個字元指針,並且定義一個』abcde\0『的字元串常量在進程空間中,並把這個字元串的首地址賦給p。這樣的定義,如果p沒有初始化,它將是一個野指針,不指向任何數據,千萬要注意不能操作野指針的值。p在程序運行過程中也可以改變值,指向別的地址。而如果按另一個人說的sizeof(p),返回的是指針類型的大小,4位元組。(2)char
a[6]
=
"abcde"這是定義一個字元數組,並且把abcde賦值給每個空間。a[0]就是a,a[1]就是b,a[5]就是'\0'。這里的定義,定義的是確定的地址空間,而不是一個指針。sizeof(a)將返回6。a在程序運行過程中,不能改變它指向別的地址,因為數組是指針常量。用strlen(char*)可以返回一個字元串的長度,這個函數從你傳入的參數開始,一直讀取到'\0'。如果你定義的char
a[6],沒有初始化,用這個函數可能會發生非常嚴重的後果。還要注意strlen返回的是字元長度,"abcde"返回5,而它實際占空間是6.C語言的字元串不能當做簡單的像int那些類型一樣的來處理,一定要注意指針的使用,這也是C語言的精髓所在。
③ C語言編程
生命游戲
/* ------------------------------------------------------ */
/* PROGRAM game of life : */
/* This is a finite implementation of John H. Conway's */
/* Game of Life. Refere to my book for detail please. */
/* */
/* Copyright Ching-Kuang Shene July/25/1989 */
/* ------------------------------------------------------ */
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 50 /* board size */
#define OCCUPIED 1 /* occupied flag */
#define UNOCCUPIED 0
#define YES 1
#define NO 0
char cell[MAXSIZE][MAXSIZE]; /* the board */
char work[MAXSIZE][MAXSIZE]; /* a working */
int row; /* No. of rows you want */
int column; /* no. of columns you want */
int generations; /* maximum no. of generation*/
/* ------------------------------------------------------ */
/* FUNCTION read_in : */
/* This function reads in the number of generations, */
/* the number of rows, the number of columns and finally */
/* the initial configuration (the generation 0). Then put*/
/* your configuration to the center of the board. */
/* ------------------------------------------------------ */
void read_in(void)
{
int max_row, max_col; /* max # of row and col. */
int col_gap, row_gap; /* incremnet of row and col */
int i, j;
char line[100];
gets(line); /* read in gens, row and col*/
sscanf(line, "%d%d%d", &generations, &row, &column);
for (i = 0; i < row; i++)/* clear the board */
for (j = 0; j < column; j++)
cell[i][j] = UNOCCUPIED;
max_col = 0; /* read in the config. */
for (max_row = 0; gets(line) != NULL; max_row++) {
for (i = 0; line[i] != '\0'; i++)
if (line[i] != ' ')
cell[max_row][i] = OCCUPIED;
max_col = (max_col < i) ? i : max_col;
}
row_gap = (row - max_row)/2; /* the moving gap */
col_gap = (column - max_col)/2;
for (i = max_row + row_gap - 1; i >= row_gap; i--) {
for (j = max_col + col_gap - 1; j >= col_gap; j--)
cell[i][j] = cell[i-row_gap][j-col_gap];
for ( ; j >= 0; j--)
cell[i][j] = UNOCCUPIED;
}
for ( ; i >= 0; i--)
for (j = 0; j < column; j++)
cell[i][j] = UNOCCUPIED;
}
/* ------------------------------------------------------ */
/* FUNCTION display : */
/* Display the board. */
/* ------------------------------------------------------ */
#define DRAW_BOARDER(n) { int i; \
printf("\n+"); \
for (i = 0; i < n; i++) \
printf("-"); \
printf("+"); \
}
void display(int gen_no)
{
int i, j;
if (gen_no == 0)
printf("\n\nInitial Generation :\n");
else
printf("\n\nGeneration %d :\n", gen_no);
DRAW_BOARDER(column);
for (i = 0; i < row; i++) {
printf("\n|");
for (j = 0; j < column; j++)
printf("%c", (cell[i][j] == OCCUPIED) ? '*' : ' ');
printf("|");
}
DRAW_BOARDER(column);
}
/* ------------------------------------------------------ */
/* FUNCTION game_of_life : */
/* This is the main function of Game of Life. */
/* ------------------------------------------------------ */
void game_of_life(void)
{
int stable; /* stable flag */
int iter; /* iteration count */
int top, bottom, left, right; /* neighborhood bound */
int neighbors; /* # of neighbors */
int cell_count; /* # of cells count */
int done;
int i, j, p, q;
display(0); /* display initial config. */
done = NO;
for (iter = 1; iter <= generations && !done; iter++) {
memmove(work, cell, MAXSIZE*MAXSIZE); /**/
stable = YES; /* assume it is in stable */
cell_count = 0; /* # of survived cells = 0 */
for (i = 0; i < row; i++) { /* scan each cell...*/
top = (i == 0) ? 0 : i - 1;
bottom = (i == row - 1) ? row-1 : i + 1;
for (j = 0; j < column; j++) {
left = (j == 0) ? 0 : j - 1;
right = (j == column - 1) ? column-1 : j + 1;
/* compute number of neighbors */
neighbors = 0;
for (p = top; p <= bottom; p++)
for (q = left; q <= right; q++)
neighbors += work[p][q];
neighbors -= work[i][j];
/* determine life or dead */
if (work[i][j] == OCCUPIED)
if (neighbors == 2 || neighbors == 3) {
cell[i][j] = OCCUPIED;
cell_count++;
}
else
cell[i][j] = UNOCCUPIED;
else if (neighbors == 3) {
cell[i][j] = OCCUPIED;
cell_count++;
}
else
cell[i][j] = UNOCCUPIED;
stable = stable && (work[i][j] == cell[i][j]);
}
}
if (cell_count == 0) {
printf("\n\nAll cells die out.");
done = YES;
}
else if (stable) {
printf("\n\nSystem enters a stable state.");
done = YES;
}
else
display(iter);
}
}
/* ------------------------------------------------------ */
void main(void)
{
read_in();
game_of_life();
}
請採納答案,支持我一下。
④ C語言編程
C語言是一種計算機程序設計語言。它既有高級語言的特點,又具有匯編語言的特點。它可以作為系統設計語言,編寫工作系統應用程序,也可以作為應用程序設計語言,編寫不依賴計算機硬體的應用程序。因此,它的應用范圍廣泛。主要有以下特點:
C語言在很多方面都可以用,不僅僅是在軟體開發上,各類科研都是需要用到C語言的。具體應用比如我是學硬體的,單片機以及嵌入式系統都可以用C來開發。
C 語言發展如此迅速, 而且成為最受歡迎的語言之一, 主要因為它具有強大的功能。許多著名的系統軟體, 如DBASE Ⅲ PLUS、DBASE Ⅳ 都是由C 語言編寫的。用C 語言加上一些匯編語言子程序, 就更能顯示C 語言的優勢了, 象PC- DOS 、WORDSTAR等就是用這種方法編寫的。歸納起來C 語言具有下列特點:1. C是中級語言它把高級語言的基本結構和語句與低級語言的實用性結合起來。C 語言可以象匯編語言一樣對位、位元組和地址進行操作, 而這三者是計算機最基本的工作單元。
2. C是結構式語言結構式語言的顯著特點是代碼及數據的分隔化, 即程序的各個部分除了必要的信息交流外彼此獨立。這種結構化方式可使程序層次清晰, 便於使用、維護以及調試。C 語言是以函數形式提供給用戶的, 這些函數可方便的調用, 並具有多種循環、條件語句控製程序流向, 從而使程序完全結構化。
3. C語言功能齊全C 語言具有各種各樣的數據類型, 並引入了指針概念, 可使程序效率更高。另外C 語言也具有強大的圖形功能, 支持多種顯示器和驅動器。而且計算功能、邏輯判斷功能也比較強大, 可以實現決策目的編游戲,編3D游戲,做資料庫,做聯眾世界,做聊天室,做PHOTOSHOP做FLASH,做3DMAX。
4. C語言適用范圍大C語言還有一個突出的優點就是適合於多種操作系統, 如DOS、UNIX,也適用於多種機型。
C語言對操作系統和系統使用程序以及需要對硬體進行操作的場合,用C語言明顯優於其它解釋型高級語言,有一些大型應用軟體也是用C語言編寫的。
C語言具有繪圖能力強,可移植性,並具備很強的數據處理能力,因此適於編寫系統軟體,三維,二維圖形和動畫。它是數值計算的高級語言。
⑤ c語言編程:計算170的階乘
前幾天正好做過一個,用數組記錄結果,暴力演算法模擬手工計算過程,談不上效率
#include <stdio.h>
#define N 10000
int main()
{
int a[N]={1};
char d[4]={0};
int i,j,t,e,f=1,m=0;
int l=0;
int k=0;
int n;
char ch='N';
printf("n=");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=0;j<=l;j++)
{
a[j]=a[j]*i+k;
k=a[j]/10000;
a[j]=a[j]%10000;
}
if(k)
{
l++;
a[j]=k;
k=0;
}
}
t=a[l];
while(t)
{
d[m]=t%10;
t/=10;
m++;
}
e=4*l+m-1;
if(e>30)
{
printf("%d!=%d.",n,d[m-1]);
for(i=m-2;i>=0;i--)
{
printf("%d",d[i]);
}
for(i=0;i<8;i++)
{
printf("%04d",a[l-1-i]);
}
printf("E+%d\n",e);
printf("輸出精確值?(Y/N)");
getchar();
ch=getchar();
if(ch!='y'&&ch!='Y')
f=0;
}
if(f)
{
i=l;
printf("%d!=%d",n,a[i--]);
for(;i>=0;i--)
{
printf("%04d",a[i]);
}
}
printf("\n");
return 0;
}
n=170
170!=7.+306
輸出精確值?(Y/N)y
170!=000000000000000000000000000
請按任意鍵繼續. . .
⑥ c語言程序設計170行大作業
1 具
2 體
3 什
4 么
5 要
6 求
7
8
9
10
11
12
13
14
15
。。。。。。
⑦ C語言編程
#include<stdio.h>
#include<stdlib.h>
intmain()
{
chars2[100];//這個數自己設置
charc='