Jzoj 条件循环(while,do while) 部分代码(共25题)

2024-09-04 05:32
文章标签 代码 条件 25 循环 部分 jzoj

本文主要是介绍Jzoj 条件循环(while,do while) 部分代码(共25题),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1020: 【入门】编程求1+3+5+...+n

#include <bits/stdc++.h>
using namespace std;
int n, sum;
int main() 
{scanf("%d", &n);for(int i=1; i<=n; i+=2){sum+=i;}    printf("%d", sum);return 0;
}

1012: 【入门】两数比大小

#include <bits/stdc++.h>
using namespace std;
int a, b;
int main()
{scanf("%d %d", &a, &b);if(a>=b){printf("%d", a);	}else{printf("%d", b);	}return 0;
}

1054: 【入门】求100+97+……+4+1的值。

#include <bits/stdc++.h>
using namespace std;
int sum;
int main() 
{for(int i=1; i<=100; i+=3){sum+=i;}    printf("%d", sum);return 0;
}

1022: 【入门】编程求1平方+2平方+...+n平方

#include <bits/stdc++.h>
using namespace std;
int n, sum;
int main() 
{scanf("%d", &n);for(int i=1; i<=n; i++){sum+=i*i;}    printf("%d", sum);return 0;
}

1023: 【入门】编程求1*2*3*...*n

#include <bits/stdc++.h>
using namespace std;
int n, ans=1;
int main() 
{scanf("%d", &n);for(int i=1; i<=n; i++){ans*=i;}    printf("%d", ans);return 0;
}

1039: 【入门】求恰好使s=1+1/2+1/3+…+1/n的值大于X时n的值。

#include <bits/stdc++.h>
using namespace std;
int x, n;
double s;
int main()
{scanf("%d", &x);while(s<=x){n++;s+=1.0/n;}printf("%d", n);return 0;
}

1577: 【入门】求S的值

#include <bits/stdc++.h>
using namespace std;
int x=1, cnt, s=1;
int main()
{scanf("%d", &x);while(s<5000){cnt++; x+=cnt;s+=x;}printf("%d", s);return 0;
}

2718: 【入门】级数求和

#include <bits/stdc++.h>
using namespace std;
int m, n, x, s;
int main()
{scanf("%d", &m);while(s<=m){n++; x+=n;s+=x;}printf("%d %d", n-1, s-x);return 0;
}

1578: 【入门】求零件个数

#include <bits/stdc++.h>
using namespace std;
int main()
{for(int i=101; ; ++i){if(i%3==2 && i%5==3 && i%7==5){printf("%d", i);return 0;}}return 0;
}

1053: 【入门】统计大写英文字母的个数

#include <bits/stdc++.h>
using namespace std;
char c;
int ans;
int main()
{while(scanf("%c", &c) && c!='.'){if(c>='A' && c<='Z'){ans++;}}printf("%d", ans);return 0;
}

1036: 【入门】统计字符的个数

#include <bits/stdc++.h>
using namespace std;
char c;
int ans1, ans2, ans3;
int main()
{while(scanf("%c", &c) && c!='#'){if(c>='A' && c<='Z'){ans1++;}else if(c>='a' && c<='z'){ans2++;}else if(c>='0' && c<='9'){ans3++;}}printf("%d %d %d", ans1, ans2, ans3);return 0;
}

1296: 【入门】加法器

#include <bits/stdc++.h>
using namespace std;
int a, b;
int main()
{while(scanf("%d %d", &a, &b) && (a!=0 || b!=0)){printf("%d\n", a+b);	}return 0;
}

1073: 【基础】小青蛙回来了

#include <bits/stdc++.h>
using namespace std;
int m, n, h, ans, sum;
int main()
{scanf("%d %d %d", &m, &n, &h);while(sum<h){ans++;sum+=m;if(sum>=h){break;}sum-=n;}printf("%d", ans);return 0;
}

1579: 【入门】求落地次数

#include <bits/stdc++.h>
using namespace std;
int ans;
double h=100;
int main()
{while(h>=0.5){ans++;h/=2;}printf("%d", ans);return 0;
}

1575: 【入门】求各位数字之和

    数位分离思路

#include <bits/stdc++.h>
using namespace std;
int n, ans;
int main()
{scanf("%d", &n);while(n){ans+=n%10;n/=10;}printf("%d", ans);return 0;
}

1094: 【入门】调尾巴数

#include <bits/stdc++.h>
using namespace std;
int n, ans;
int main()
{for(int i=100007; i<=999997; i+=10){n=i%10*100000+i/10;if(5*i==n){printf("%d", i);break;}}return 0;
}

1576: 【入门】周末大优惠

#include <bits/stdc++.h>
using namespace std;
double a, ans;
int main()
{while(scanf("%lf", &a) && a!=0){ans+=a;}if(ans>100){ans-=100;ans*=0.9;ans+=100;}printf("%.3lf", ans);return 0;
}

1794: 【入门】角谷猜想

#include <bits/stdc++.h>
using namespace std;
int n, ans;
int main()
{scanf("%d", &n);while(n!=1){if(n%2==0){n/=2;}else{n=n*3+1;}ans++;}printf("%d", ans);return 0;
}

2774: 【基础】军事演习

#include <bits/stdc++.h>
using namespace std;
int ans;
double s, vc, vd, vm, dis, t;
int main()
{scanf("%lf %lf %lf %lf", &s, &vc, &vd, &vm);dis=s;	//c、d两队相距初始化为s while(dis>0.8){	//只要距离大于0.8则一直模拟//因为问的是摩托车跑的趟数, 所以按摩托车跑的趟数来模拟 //模拟摩托车从c向d出发花的时间 t=dis/(vd+vm); 	//总路程除以d和摩托车的速度之和 //此时c和d的距离dis-=(vc+vd)*t;//判断有没有必要再从d驶向c ans++;if(dis<=0.8){break;} //模拟摩托车从d向c出发花的时间 t=dis/(vc+vm); 	//总路程除以c和摩托车的速度之和//此时c和d的距离dis-=(vc+vd)*t;ans++;}printf("%d", ans);return 0;
}

1074: 【基础】寻找2的幂

#include <bits/stdc++.h>
using namespace std;
long long n, ans=1;
int main()
{scanf("%lld", &n);while(ans<=n){ans<<=1;}	if(n-ans/2<=ans-n){printf("%lld", ans/2);}else{printf("%lld", ans);	}return 0;
}

1029: 【入门】求两个自然数M和N的最大公约数

#include <bits/stdc++.h>
using namespace std;
long long n, m, temp, ans;
int main()
{scanf("%lld %lld", &n, &m);while(1){if(n%m==0){ans=m;break;}else{temp=m;  m=n%m;   n=temp;  }}	printf("%lld", ans);return 0;
}

1028: 【入门】两个自然数M和N的最小公倍数。

#include <bits/stdc++.h>
using namespace std;
long long n, m, temp, a, b, ans;
int main()
{scanf("%lld %lld", &n, &m);a=m;b=n;while(1){if(n%m==0){ans=m;break;}else{temp=m;  m=n%m;   n=temp;  }}	printf("%lld", a/ans*b);return 0;
}

1038: 【基础】质因子

#include <bits/stdc++.h>
using namespace std;
int n;
bool flag;	 
int main()
{scanf("%d", &n);while(n>1){for(int i=2; i<=n; ++i){if(n%i==0){	//首先满足因子 flag=true;	//默认i是质数 //判断i是否为质数 for(int j=2; j<=sqrt(i); ++j){if(i%j==0){flag=false;	//标记i不是质数 break; }} if(flag){n/=i;printf("%d\n", i);break;} }}	}return 0;
}

1051: 【基础】约瑟夫问题

#include <bits/stdc++.h>
int n, m, cnt, out, a[101]; //a[i]=0表示在圈里 
int main()
{scanf("%d %d", &n, &m);while(1){for(int i=1; i<=n; i++){if(a[i]==0){cnt++;} if(cnt==m){cnt=0;out++;printf("%d\n",i);a[i]=1;if(out==n){return 0;}}}	}	return 0;
}

1124: 【基础】n的阶乘尾部有多少个连续的0

//看约数里面有几个5, 有几个2 
#include <bits/stdc++.h>
using namespace std;
int n, temp, cnt2, cnt5;	 
int main()
{scanf("%d", &n);for(int i=1; i<=n; ++i){temp=i;while(temp && temp%2==0){cnt2++;temp/=2;}temp=i;while(temp && temp%5==0){cnt5++;temp/=5;}	}printf("%d", min(cnt2, cnt5));return 0;
}

这篇关于Jzoj 条件循环(while,do while) 部分代码(共25题)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1135134

相关文章

Spring 依赖注入与循环依赖总结

《Spring依赖注入与循环依赖总结》这篇文章给大家介绍Spring依赖注入与循环依赖总结篇,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1. Spring 三级缓存解决循环依赖1. 创建UserService原始对象2. 将原始对象包装成工

Redis实现高效内存管理的示例代码

《Redis实现高效内存管理的示例代码》Redis内存管理是其核心功能之一,为了高效地利用内存,Redis采用了多种技术和策略,如优化的数据结构、内存分配策略、内存回收、数据压缩等,下面就来详细的介绍... 目录1. 内存分配策略jemalloc 的使用2. 数据压缩和编码ziplist示例代码3. 优化的

Python 基于http.server模块实现简单http服务的代码举例

《Python基于http.server模块实现简单http服务的代码举例》Pythonhttp.server模块通过继承BaseHTTPRequestHandler处理HTTP请求,使用Threa... 目录测试环境代码实现相关介绍模块简介类及相关函数简介参考链接测试环境win11专业版python

Python从Word文档中提取图片并生成PPT的操作代码

《Python从Word文档中提取图片并生成PPT的操作代码》在日常办公场景中,我们经常需要从Word文档中提取图片,并将这些图片整理到PowerPoint幻灯片中,手动完成这一任务既耗时又容易出错,... 目录引言背景与需求解决方案概述代码解析代码核心逻辑说明总结引言在日常办公场景中,我们经常需要从 W

使用Spring Cache本地缓存示例代码

《使用SpringCache本地缓存示例代码》缓存是提高应用程序性能的重要手段,通过将频繁访问的数据存储在内存中,可以减少数据库访问次数,从而加速数据读取,:本文主要介绍使用SpringCac... 目录一、Spring Cache简介核心特点:二、基础配置1. 添加依赖2. 启用缓存3. 缓存配置方案方案

从基础到进阶详解Python条件判断的实用指南

《从基础到进阶详解Python条件判断的实用指南》本文将通过15个实战案例,带你大家掌握条件判断的核心技巧,并从基础语法到高级应用一网打尽,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一... 目录​引言:条件判断为何如此重要一、基础语法:三行代码构建决策系统二、多条件分支:elif的魔法三、

MySQL的配置文件详解及实例代码

《MySQL的配置文件详解及实例代码》MySQL的配置文件是服务器运行的重要组成部分,用于设置服务器操作的各种参数,下面:本文主要介绍MySQL配置文件的相关资料,文中通过代码介绍的非常详细,需要... 目录前言一、配置文件结构1.[mysqld]2.[client]3.[mysql]4.[mysqldum

Python多线程实现大文件快速下载的代码实现

《Python多线程实现大文件快速下载的代码实现》在互联网时代,文件下载是日常操作之一,尤其是大文件,然而,网络条件不稳定或带宽有限时,下载速度会变得很慢,本文将介绍如何使用Python实现多线程下载... 目录引言一、多线程下载原理二、python实现多线程下载代码说明:三、实战案例四、注意事项五、总结引

IDEA与MyEclipse代码量统计方式

《IDEA与MyEclipse代码量统计方式》文章介绍在项目中不安装第三方工具统计代码行数的方法,分别说明MyEclipse通过正则搜索(排除空行和注释)及IDEA使用Statistic插件或调整搜索... 目录项目场景MyEclipse代码量统计IDEA代码量统计总结项目场景在项目中,有时候我们需要统计

MySQL设置密码复杂度策略的完整步骤(附代码示例)

《MySQL设置密码复杂度策略的完整步骤(附代码示例)》MySQL密码策略还可能包括密码复杂度的检查,如是否要求密码包含大写字母、小写字母、数字和特殊字符等,:本文主要介绍MySQL设置密码复杂度... 目录前言1. 使用 validate_password 插件1.1 启用 validate_passwo