百钱百鸡
中国古代数学家张丘建在他的《算经》中提出了一个著名的“百钱百鸡问题”:
一只公鸡值五钱,一只母鸡值三钱,三只小鸡值一钱,
现在要用百钱买百鸡,请问公鸡、母鸡、小鸡各多少只?
package main
import "fmt"
func main1() {
/*
中国古代数学家张丘建在他的《算经》中提出了一个著名的“百钱百鸡问题”:
一只公鸡值五钱,一只母鸡值三钱,三只小鸡值一钱,
现在要用百钱买百鸡,请问公鸡、母鸡、小鸡各多少只?
*/
count := 0
for cock := 0; cock <= 20; cock++ {
for hen := 0; hen <= 33; hen++ {
for chick := 0; chick <= 100; chick++ {
count++
if chick%3 == 0 && cock+hen+chick == 100 && cock*5+hen*3+chick/3 == 100 {
fmt.Printf("公鸡:%d, 母鸡:%d, 小鸡:%d\n", cock, hen, chick)
}
}
}
}
//72114
fmt.Println(count)
}
func main() {
count := 0
for cock := 0; cock <= 20; cock++ {
for hen := 0; hen <= 33; hen++ {
for chick := 0; chick <= 100; chick += 3 {
count++
if cock+hen+chick == 100 && cock*5+hen*3+chick/3 == 100 {
fmt.Printf("公鸡:%d, 母鸡:%d, 小鸡:%d\n", cock, hen, chick)
}
}
}
}
//24276
fmt.Println(count)
}
func main3() {
count := 0
for cock := 0; cock <= 20; cock++ {
for hen := 0; hen <= 33; hen++ {
count++
chick := 100 - cock - hen
if chick%3 == 0 && 5*cock+3*hen+chick/3 == 100 {
fmt.Printf("公鸡:%d, 母鸡:%d, 小鸡:%d\n", cock, hen, chick)
}
}
}
//714
fmt.Println(count)
}
统计字符串中每个字符出现的次数
C语言
#include<stdio.h>
int main()
{
char str[] = "hellowrold";
unsigned char all[256] = { 0 };
char* tmep = str;
while (*tmep)
{
all[*tmep]++;
tmep++;
}
for (int i = 0; i < 256; i++)
{
if (all[i] != 0)
printf("字符%c出现的次数:%d\n", i, all[i]);
}
}
go