c語言長篇代碼,c語言長篇代碼是什麼

本文目錄一覽:

求一個c語言代碼,要就是長

//C++ 輕重鏈剖分代碼 有指針

#include iostream

#include cstdio

#include cstdlib

#include algorithm

#include cstring

using namespace std;

const int debug_mode = false; 

//int Max(int a, int b) {return ab?a:b;}

//int Min(int a, int b) {return ab?b:a;}

//

const int N = 300012;

int n, m;

int a[N + N], delta[N + N], b[N + N];

void update(int x) {

a[x] = a[x  1] + a[x * 2 + 1];

b[x] = max(b[x  1] , b[x * 2 + 1]);

}

void build(int x, int s, int t) {

if (s == t) { a[x] = 0; b[x] = 0; return; }

int mid = (s + t)  1;

build(x  1, s, mid);

build(x * 2 + 1, mid + 1, t);

update(x);

}

void change(int x, int s, int t, int pos, int key) {

if (s == t  s == pos) { a[x] = key; b[x] = key; return; }

int mid = (s + t)  1;

if (pos = mid) change(x  1, s, mid, pos, key); else change(x * 2 + 1, mid + 1, t, pos, key);

update(x);

}

int query(int x, int s, int t, int l, int r) {

if (l = s  t = r) return b[x];

int mid = s + t  1; int ans = -0x7fffffff; 

if (l = mid) ans =max(ans, query(x  1, s, mid, l, r));

if (r  mid) ans =max(ans, query(x * 2 + 1, mid + 1, t, l, r));

return ans;

}

int querysum(int x, int s, int t, int l, int r)

{

if(l = s  t = r) return a[x];

int mid = s + t  1; int ans = 0; 

if(l = mid) ans += querysum(x1, s, mid, l, r);

if(r  mid) ans += querysum(x*2+1, mid+1, t, l, r);

return ans;

}

//

struct edge{int u,v,; edge *next; bool inuse;}e[N], *P = e, *point[N];

inline void add_edge(int x, int y) {

edge *Q = ++P; ++P;

P-u = x; P-v = y; P-next = point[x]; point[x] = P; Q-u = y; Q-v = x; Q-next = point[y];point[y] = Q; P-inuse=Q-inuse = true;

}

int root,size[N],dep[N],top[N],fa[N],son[N],w[N],va[N];

void dfs1(int father,int x, int depth)

{

fa[x] = father;

dep[x] = depth;

size[x] = 1;

int sum=0, max=-1, maxi= son[x] = 0;

for(edge *j = point[x]; j; j = j-next)

{

if(j-v == fa[x]) continue;

dfs1(x,j-v,depth+1);

sum += size[j-v];

if(size[j-v]  max) maxi = j-v, max = size[j-v];

}

size[x] += sum;

if(maxi) son[x] = maxi;

}

int totw = 0;

void dfs2(int v, int Top)

{

w[v] = ++totw; top[v] = Top;

if(son[v]) dfs2(son[v],top[v]);

for(edge *j = point[v]; j; j = j-next)

if(j-v !=fa[v]  j-v != son[v])

dfs2(j-v,j-v);

}

int Qmax(int u, int v)

{

int ret = -0x7fffffff;

while(1)

{

int f1 = top[u], f2 = top[v];

if(f1==f2)

{

if(u==v) return max(ret,query(1,1,n,w[u],w[u]));

if(dep[u]  dep[v]) swap(u,v);

ret = max(ret, query(1,1,n,w[u],w[v]));

return ret;

}else

{

if(dep[f1]dep[f2]) swap(f1,f2), swap(u,v);

ret = max(ret, query(1,1,n,min(w[u],w[f1]),max(w[u],w[f1])));

u = fa[f1];

continue;

}

}

}

int Qsum(int u, int v)

{

int ret = 0;

while(1)

{

int f1 = top[u], f2 = top[v];

if(f1==f2)

{

if(u==v) return ret+va[u];

ret += querysum(1,1,n,min(w[u],w[v]),max(w[u],w[v]));

return ret;

}else

{

if(dep[f1]dep[f2]) swap(f1,f2), swap(u,v);

// u f1

ret += querysum(1,1,n,w[f1],w[u]);

u = fa[f1]; f1 = top[u];

continue;

}

}

}

void debug(int i)

{

if(!debug_mode) return;

couti”———–“endl;

cout”fa[x]   : “fa[i]endl;

cout”size[x] : “size[i]endl;

cout”dep[x]  : “dep[i]endl;

cout”son[x]  : “son[i]endl;

cout”top[x]  : “top[i]endl;

cout”w[x]    : “w[i]endl; 

}

int main()

{

//freopen(“bz1036.in”,”r”,stdin);freopen(“bz1036.out”,”w”,stdout);

scanf(“%d”, n);

for(int i = 1 ; i  n; i++)

{

int x,y;

scanf(“%d%d”,x, y);

add_edge(x,y);

if(i==1) root = x;

}

for(int i = 1; i = n; i++) scanf(“%d”, va[i]);

dfs1(0,root,1);

dfs2(root,root);

for(int i = 1; i = n; i++) change(1,1,n,w[i],va[i]);

//for(int i = 1; i = n; i++) debug(i);

int t;

scanf(“%d”, t);

while(t–)

{

char com[7]; scanf(“%s”, com);

int a,b; scanf(“%d%d”, a, b);

switch(com[1])

{

case ‘M’:

printf(“%d\n”,Qmax(a,b));

break;

case ‘S’:

printf(“%d\n”,Qsum(a,b));

break;

case ‘H’:

va[a] = b;

change(1,1,n,w[a],b);

break;

}

}

}

//一個資源管理器模擬的代碼

#include iostream

#include cstdio

#include cstring

#include algorithm

#include cstdlib

#include vector

using namespace std;

int idtop=0;

struct file

{

int id;

string name;

bool is_folder;

int father;

}files[2001],*root=files;

vectorintct[2001];//content

int history[10005],htop=0,hcur=0;

int current;

string rem[2001];

string prefix(int depth)

{

if(rem[depth]!=””) return rem[depth];

string ret;

for(int d = 1; d = depth; d++) ret+=”..”;

return rem[depth]=ret;

}

void print(int id, int depth)

{

//coutdepthendl;

coutprefix(depth)”\\”files[id].nameendl;//   ../folder

for(int i = 0; i  ct[id].size(); i++)

{

if(files[ct[id][i]].is_folder) print(ct[id][i],depth+1);

else coutprefix(depth+1)files[ct[id][i]].nameendl;

}

}

int newFile(bool isf, string name)

{

for(int i = 0; i  ct[current].size(); i++)

if(files[ct[current][i]].name == name  files[ct[current][i]].is_folder == isf) return -1;

files[++idtop].id = idtop;

files[idtop].name = name;

files[idtop].father = current;

files[idtop].is_folder = isf;

ct[current].push_back(idtop);

if(isf)

{

current = idtop;

history[htop = ++hcur] = idtop;

}

return 1;

}

int enter(string path)

{

int posi = 0, cur_bk = current;

if(path[0]==’\\’||path[path.length()-1]!=’\\’)

{

return -1;

}

else

{

//分離path  enter 

while(posi  path.length())

{

string split=””; int curlen = 0;

while(posipath.length())

{

char s = path[posi];

if(path[posi]!=’\\’) split+=path[posi],posi++,curlen++;

else

{

posi++;

if(curlen==0) {current = cur_bk; return -1;}

bool found = false;

for(int i = 0; i  ct[current].size(); i++)

if(files[ct[current][i]].name == split) 

{

if(files[ct[current][i]].is_folder == false) continue;

found = true;

current = ct[current][i];

break;

}

if(!found) {current = cur_bk; return -1;}

break;

}

}

history[htop = ++hcur] = current;

}

return 1;

}

}

int back()

{

if(hcur)

{

current = history[–hcur];

return 1;

}else

{

return -1;

}

}

int fore()

{

if(hcur=htop)

{

return -1;

}else

{

current = history[++hcur];

return 1;

}

}

int up()

{

if(files[current].father == -1) return -1;

else

{

current = files[current].father;

history[htop = ++hcur] = current;

}

}

void init()

{

for(int i = 0; i  35; i++) rem[i]=””;

root-id = 0; root-name = “exp”; root-father = -1; root-is_folder = true;

history[0] = 0;

current = 0;

}

#define Wrong printf(“%s\n”,”WRONG COMMAND.”);

int main()

{

//freopen(“explorer.in”,”r”,stdin);

//freopen(“explorer.out”,”w”,stdout);

init();

int n;

scanf(“%d”, n);

while(n–)

{

string com;

cincom;

if(com==”back”)

{

if(back()==-1) Wrong;

continue;

}

if(com==”fore”)

{

if(fore()==-1) Wrong;

continue;

}

if(com==”up”)

{

if(up()==-1) Wrong;

continue;

}

if(com==”print”)

{

print(0,0);

continue;

}

if(com==”new”)

{

getchar();

char comd[51]; bool isf = false; string fname = “”;

gets(comd);

  for(int i = 0; i  strlen(comd); i++)

{

if(comd[i]==’ ‘) 

{

isf = true;

continue;

}

if(isf)

{

fname += comd[i];

}

}

if(isf)

{

switch(comd[0])

{

case ‘0’:

if(comd[1]!=’ ‘)

{

Wrong;

continue;

}

isf=false;

break;

case ‘1’:

if(comd[1]!=’ ‘)

{

Wrong;

continue;

}

break;

default:

Wrong;

continue;

}

}else

{

for(int i = 0; i  strlen(comd); i++)

fname += comd[i];

}

if(newFile(isf,fname)==-1) Wrong;

continue;

}

if(com==”enter”)

{

string path;

cinpath;

if(enter(path)==-1) Wrong;

continue;

}

}

}

c語言必背代碼有哪些?

1、/*輸出9*9口訣。共9行9列,i控制行,j控制列。*/

#include “stdio.h”

main()

{int i,j,result;

for (i=1;i10;i++)

{ for(j=1;j10;j++)

{

result=i*j;

printf(“%d*%d=%-3d”,i,j,result);/*-3d表示左對齊,佔3位*/

}

printf(“\n”);/*每一行後換行*/

}

}

2、/*古典問題:有一對兔子,從出生後第3個月起每個月都生一對兔子,小兔子長到第三個月後每個月又生一對兔子,假如兔子都不死,問每個月的兔子總數為多少?

兔子的規律為數列1,1,2,3,5,8,13,21….*/

main()

{

long f1,f2;

int i;

f1=f2=1;

for(i=1;i=20;i++)

{ printf(“%12ld %12ld”,f1,f2);

if(i%2==0) printf(“\n”);/*控制輸出,每行四個*/

f1=f1+f2; /*前兩個月加起來賦值給第三個月*/

f2=f1+f2; /*前兩個月加起來賦值給第三個月*/

}

}

3、/*判斷101-200之間有多少個素數,並輸出所有素數及素數的個數。

程序分析:判斷素數的方法:用一個數分別去除2到sqrt(這個數),如果能被整除,

則表明此數不是素數,反之是素數。*/

#include “math.h”

main()

{

int m,i,k,h=0,leap=1;

printf(“\n”);

for(m=101;m=200;m++)

{ k=sqrt(m+1);

for(i=2;i=k;i++)

if(m%i==0)

{leap=0;break;}

if(leap)    /*內循環結束後,leap依然為1,則m是素數*/

{printf(“%-4d”,m);h++;

if(h%10==0)

printf(“\n”);

}

leap=1;

}

printf(“\nThe total is %d”,h);

}

4、/*一個數如果恰好等於它的因子之和,這個數就稱為”完數”。例如6=1+2+3.編程

找出1000以內的所有完數。*/

main()

{

static int k[10];

int i,j,n,s;

for(j=2;j1000;j++)

{

n=-1;

s=j;

for(i=1;ij;i++)

{if((j%i)==0)

{  n++;

s=s-i;

k[n]=i;

}

}

if(s==0)

{printf(“%d is a wanshu:  “,j);

for(i=0;in;i++)

printf(“%d,”,k[i]);

printf(“%d\n”,k[n]);

}

}

}

5、/*下面程序的功能是將一個4×4的數組進行逆時針旋轉90度後輸出,要求原始數組的數據隨機輸入,新數組以4行4列的方式輸出,

請在空白處完善程序。*/

main()

{  int  a[4][4],b[4][4],i,j;       /*a存放原始數組數據,b存放旋轉後數組數據*/

printf(“input 16 numbers: “);

/*輸入一組數據存放到數組a中,然後旋轉存放到b數組中*/

for(i=0;i4;i++)

for(j=0;j4;j++)

{  scanf(“%d”,a[i][j]);

b[3-j][i]=a[i][j];

}

printf(“array b:\n”);

for(i=0;i4;i++)

{  for(j=0;j4;j++)

printf(“%6d”,b[i][j]);

printf(“\n”);

}

}

6、/*編程列印直角楊輝三角形*/

main()

{int i,j,a[6][6];

for(i=0;i=5;i++)

{a[i][i]=1;a[i][0]=1;}

for(i=2;i=5;i++)

for(j=1;j=i-1;j++)

a[i][j]=a[i-1][j]+a[i-1][j-1];

for(i=0;i=5;i++)

{for(j=0;j=i;j++)

printf(“%4d”,a[i][j]);

printf(“\n”);}

}

7、/*通過鍵盤輸入3名學生4門課程的成績,

分別求每個學生的平均成績和每門課程的平均成績。

要求所有成績均放入一個4行5列的數組中,輸入時同一人數據間用空格,不同人用回車

其中最後一列和最後一行分別放每個學生的平均成績、每門課程的平均成績及班級總平均分。*/

#include stdio.h

#include stdlib.h

main()

{ float a[4][5],sum1,sum2;

int i,j;

for(i=0;i3;i++)

for(j=0;j4;j++)

scanf(“%f”,a[i][j]);

for(i=0;i3;i++)

{ sum1=0;

for(j=0;j4;j++)

sum1+=a[i][j];

a[i][4]=sum1/4;

}

c語言必背100代碼有哪些?

/*輸出9*9口訣。共9行9列,i控制行,j控制列。*/

#include “stdio.h”

main()

{int i,j,result;

for (i=1;i10;i++)

{ for(j=1;j10;j++)

{

result=i*j;

printf(“%d*%d=%-3d”,i,j,result);/*-3d表示左對齊,佔3位*/

}

printf(“\n”);/*每一行後換行*/

}

}

擴展資料:

C語言的字元串其實就是以’\0’字元結尾的char型數組,使用字元型並不需要引用庫,但是使用字元串就需要C標準庫裡面的一些用於對字元串進行操作的函數。它們不同於字元數組。使用這些函數需要引用頭文件string.h。

文件輸入/輸出

在C語言中,輸入和輸出是經由標準庫中的一組函數來實現的。在ANSI C中,這些函數被定義在頭文件stdio.h;中。

標準輸入/輸出

有三個標準輸入/輸出是標準I/O庫預先定義的:

stdin標準輸入

stdout標準輸出

stderr輸入輸出錯誤

參考資料來源:百度百科-c語言

原創文章,作者:RVUE,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/144912.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
RVUE的頭像RVUE
上一篇 2024-10-26 11:53
下一篇 2024-10-26 11:53

相關推薦

  • Python周杰倫代碼用法介紹

    本文將從多個方面對Python周杰倫代碼進行詳細的闡述。 一、代碼介紹 from urllib.request import urlopen from bs4 import Bea…

    編程 2025-04-29
  • Python字元串寬度不限制怎麼打代碼

    本文將為大家詳細介紹Python字元串寬度不限制時如何打代碼的幾個方面。 一、保持代碼風格的統一 在Python字元串寬度不限制的情況下,我們可以寫出很長很長的一行代碼。但是,為了…

    編程 2025-04-29
  • Python基礎代碼用法介紹

    本文將從多個方面對Python基礎代碼進行解析和詳細闡述,力求讓讀者深刻理解Python基礎代碼。通過本文的學習,相信大家對Python的學習和應用會更加輕鬆和高效。 一、變數和數…

    編程 2025-04-29
  • AES加密解密演算法的C語言實現

    AES(Advanced Encryption Standard)是一種對稱加密演算法,可用於對數據進行加密和解密。在本篇文章中,我們將介紹C語言中如何實現AES演算法,並對實現過程進…

    編程 2025-04-29
  • Python滿天星代碼:讓編程變得更加簡單

    本文將從多個方面詳細闡述Python滿天星代碼,為大家介紹它的優點以及如何在編程中使用。無論是剛剛接觸編程還是資深程序員,都能從中獲得一定的收穫。 一、簡介 Python滿天星代碼…

    編程 2025-04-29
  • 學習Python對學習C語言有幫助嗎?

    Python和C語言是兩種非常受歡迎的編程語言,在程序開發中都扮演著非常重要的角色。那麼,學習Python對學習C語言有幫助嗎?答案是肯定的。在本文中,我們將從多個角度探討Pyth…

    編程 2025-04-29
  • 倉庫管理系統代碼設計Python

    這篇文章將詳細探討如何設計一個基於Python的倉庫管理系統。 一、基本需求 在著手設計之前,我們首先需要確定倉庫管理系統的基本需求。 我們可以將需求分為以下幾個方面: 1、庫存管…

    編程 2025-04-29
  • 寫代碼新手教程

    本文將從語言選擇、學習方法、編碼規範以及常見問題解答等多個方面,為編程新手提供實用、簡明的教程。 一、語言選擇 作為編程新手,選擇一門編程語言是很關鍵的一步。以下是幾個有代表性的編…

    編程 2025-04-29
  • Python實現簡易心形代碼

    在這個文章中,我們將會介紹如何用Python語言編寫一個非常簡單的代碼來生成一個心形圖案。我們將會從安裝Python開始介紹,逐步深入了解如何實現這一任務。 一、安裝Python …

    編程 2025-04-29
  • Python被稱為膠水語言

    Python作為一種跨平台的解釋性高級語言,最大的特點是被稱為”膠水語言”。 一、簡單易學 Python的語法簡單易學,更加人性化,這使得它成為了初學者的入…

    編程 2025-04-29

發表回復

登錄後才能評論