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/n/144912.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
RVUERVUE
上一篇 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

发表回复

登录后才能评论