go 函数举例练习
1. 求1到100之内的所有质数,并打印到屏幕上
package main import "fmt" // 求1-100 内的质数
func justfy(i int) bool {
if i <= {
return false
} for j := ; j <= i/; j++ {
if i%j == {
return false
}
} return true
} func exp() {
for i := ; i <= ; i++ {
if justfy(i) {
fmt.Printf("%d is 质数\n", i)
}
}
} func main() {
exp()
}
2. 求出 100-999 之间所有的水仙花数
package main
import "fmt"
func issxh(i int) bool {
ge := i %
shi := (i / ) %
bai := i /
sum := ge*ge*ge + shi*shi*shi + bai*bai*bai
if sum == i {
return true
} else {
return false
}
}
func exp() {
for i := ; i <= ; i++ {
if issxh(i) {
fmt.Printf("%d 是水仙花数\n", i)
}
}
}
func main() {
// fmt.Println(111 / 10)
exp()
}
3.输入一个字符,分别统计出其中英文字目、空格、数字和其它字符的个数
package main import "fmt" // 求一个字符串中英文个数 空格个数 数字个数 其他个数
// 字符串用双引号 字符 用单引号
func counttest(str string) (charCount, spaceCount, numCount, otherCount int) {
utf8_str := []rune(str)
for i := ; i < len(utf8_str); i++ {
if (utf8_str[i] >= 'a' && utf8_str[i] <= 'z') || (utf8_str[i] >= 'A' && utf8_str[i] <= 'Z') {
charCount++
continue
} else if utf8_str[i] == ' ' {
spaceCount++
continue
} else if utf8_str[i] >= '' && utf8_str[i] <= '' {
numCount++
continue
} else {
otherCount++
continue
}
}
return
} func main() {
str := "yunnan is beautiful 云南欢迎你 123"
charCount, spaceCount, numCount, otherCount := counttest(str)
fmt.Printf("charCount = %d \n spaceCount=%d \n numCount=%d \n otherCount=%d \n", charCount, spaceCount, numCount, otherCount)
}
go 函数举例练习的更多相关文章
- Windows下编程2----- C语言常用函数举例
几个小函数 1. //MessageBoxA(0,"网络故障,重新登录","qq error",3); //弹出对话框 2. //ShellExec ...
- SQL SERVER 函数举例
需求说明 将字符串按照指定的分隔符进行分割,并将结果按照从后往前的顺序倒序排列,拼接后的结果用‘/’符连接.(也可以按照指定符号分割为多个列,修改最后一部分即可) 创建测试表及数据 /* 创建一张测试 ...
- Spark--sql--所有函数举例(spark-2.x版本)
! expr - Logical not. % expr1 % expr2 - Returns the remainder afterexpr1/expr2. Examples: > SELEC ...
- 1.forEach():遍历数组,并为每个元素调用传入的函数; 举例:
var a = [1,2,3]; var sum = 0; //传一个参数 a.forEach(function(v){ sum += v; }); console.log(sum);//6 //传三 ...
- getchar()函数举例
#include<stdio.h>void main(){ char ch; ch=getchar(); printf("%c",ch);}
- Python标准模块--built-ins函数
1.Python内置函数 2.Python内置函数举例 2.1 数学运算 abs,计算绝对值: >>> abs(-1) 1 >>> abs(3) 3 round,四 ...
- Matlab中函数定义方法
Matlab自定义函数的六种方法 n1.函数文件+调用函数(命令)文件:需单独定义一个自定义函数的M文件: n2.函数文件+子函数:定义一个具有多个自定义函数的M文件: n3.Inline:无需M文件 ...
- 字符串哈希函数(String Hash Functions)
哈希函数举例 http://www.cse.yorku.ca/~oz/hash.html Node.js使用的哈希函数 https://www.npmjs.org/package/string-has ...
- Python 常用函数大体分类
==================系统库函数================ 字符串函数 举例数学函数 import math val=math.sin(3.14/6) val=math.sin(m ...
随机推荐
- 聊聊flink的NetworkEnvironmentConfiguration
本文主要研究一下flink的NetworkEnvironmentConfiguration NetworkEnvironmentConfiguration flink-1.7.2/flink-runt ...
- [UVALive 3983] Robotruck
图片加载可能有点慢,请跳过题面先看题解,谢谢 设状态 \(f[i][j]\) 为,当前垃圾序号为 \(i\) ,当前承重为 \(j\) 的最小路程,好的这道题做完了 O(NC) G烂 $ $ 我们这样 ...
- 【洛谷4005】小Y和地铁(搜索)
[洛谷4005]小Y和地铁(搜索) 题面 洛谷 有点长. 题解 首先对于需要被链接的两个点,样例中间基本上把所有的情况都给出来了. 但是还缺了一种从下面绕道左边在从整个上面跨过去在从右边绕到下面来的情 ...
- Group Anagrams - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Group Anagrams - LeetCode 注意点 字母都是小写的 解法 解法一:用一个字符串表示strs[i]中出现的字母,比如:abc-> ...
- MacBook设置终端颜色,补全忽略大小写,设置命令别名alias,设置vim,设置显示git分支
1.启用终端颜色 修改配置文件 $ vim .bash_profile #enables colorin the terminal bash shell export export CLICOLOR= ...
- 2:spring中的@resource
@Resource 其实是spring里面的注解注入. @Resource(这个注解属于J2EE的),默认安照名称进行装配,名称可以通过name属性进行指定, 如果没有指定name属性,当注解写在字段 ...
- 配置iOS项目的设备系统目标设置:Base SDK和Deployment Target
配置iOS项目的设备系统目标设置:Base SDK和Deployment Target Xcode为开发者提供了两个可配置的设置:第一个是Base SDK,第二个是iOS的Deployment Tar ...
- Codeforces Round #290 (Div. 2) 拓扑排序
C. Fox And Names time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- 详细MATLAB 中BP神经网络算法的实现
MATLAB 中BP神经网络算法的实现 BP神经网络算法提供了一种普遍并且实用的方法从样例中学习值为实数.离散值或者向量的函数,这里就简单介绍一下如何用MATLAB编程实现该算法. 具体步骤 这里 ...
- The POSIX API/nss/nscd
https://code.google.com/p/nsscache/wiki/BackgroundOnNameServiceSwitch The POSIX API POSIX is a stand ...