Skip to content

Instantly share code, notes, and snippets.

View wingofthestar's full-sized avatar

wingofthestar

View GitHub Profile
@wingofthestar
wingofthestar / 用数组实现队列
Created August 5, 2017 02:33
桌上有一叠牌,从第一张牌(即位于顶面的牌)开始从上往下依次编号为1~n。当至少还剩两张牌时进行以下操作:把第一张扔掉,然后把新的第一张放到整叠牌的最后。
package site.yourdiary.demo;
import java.util.Scanner;
public class QueueDemo {
public static void main(String[] args) {
int n, front, rear;
Scanner in = new Scanner(System.in);
n = in.nextInt();
int[] queue = new int[50];
@wingofthestar
wingofthestar / BaiduByName
Created July 23, 2017 11:20
创建新的InetAddress对象,获取主机名
public class BaiduByName {
public static void main(String[] args) {
try {
InetAddress address = InetAddress.getByName("www.baidu.com");
System.out.println(address);
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
@wingofthestar
wingofthestar / 周期串
Created July 12, 2017 02:58
如果一个字符串可以由某个长度为k的字符串重复多次得到,我们说该串以k为周期。例如,abcabcabcabcabc以3为周期
#include<stdio.h>
#include<string.h>
int main() {
char word[100];
scanf("%s", word);
int len = strlen(word);
for (int i = 1; i <= len; i++) {
if (len % i == 0) {
@wingofthestar
wingofthestar / 回文
Created July 7, 2017 05:47
判断回文
package site.yourdiary.demo;
import java.util.Scanner;
/**
* Created by star on 2017/7/7.
*/
public class HuiWen {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
#include<stdio.h>
#include<string.h>
int main() {
int i, ok, abc, de, x, y, z, count = 0;
char s[20], buf[99];
scanf("%s", s);
for (abc = 111; abc <= 999; abc++) {
for (de = 11; de <= 99; de++) {
x = abc*(de % 10); y = abc*(de / 10); z = abc*de;
sprintf(buf, "%d%d%d%d%d", abc, de, x, y, z);
@wingofthestar
wingofthestar / 近似计算
Created July 4, 2017 06:38
计算Pi/4 = 1 - 1/3 + 1/5 - 1/7 + ...,直到最后一项小于10^-6。
#include<stdio.h>
#define N 0.000001
int main() {
double n = 1.0;
int i;
int flag = 1;
double sum = 0.0;
for(i = 1; n >= N; i++){
@wingofthestar
wingofthestar / 倒三角形输出
Created July 4, 2017 06:01
输入整数n<=20,输出一个n层的倒三角形
#include<stdio.h>
int main() {
int n;
int m;
scanf("%d", &n);
m = n;
for (int i = 0; i < n; i++) {
for (int k = 0; k < (2 * (n-m))/2; k++) {
printf(" ");
@wingofthestar
wingofthestar / 倒三角形输出
Created July 4, 2017 06:00
输入整数n<=20,输出一个n层的倒三角形
#include<stdio.h>
int main() {
int n;
int m;
scanf("%d", &n);
m = n;
for (int i = 0; i < n; i++) {
for (int k = 0; k < (2 * (n-m))/2; k++) {
printf(" ");
@wingofthestar
wingofthestar / 韩信点兵算法
Created July 4, 2017 04:51
相传韩信才智过人,从不直接清点自己军队的人数,只要让士兵先后以三人一排、五人一排、七人一排地变换队形,而他每次只掠一眼队伍的排尾就知道总人数了。输入3个非负整数a,b,c ,表示每种队形排尾的人数(a<3,b<5,c<7),输出总人数的最小值(或报告无解)。已知总人数不小于10,不超过100 。
#include<stdio.h>
int main() {
int i = 10;
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
while (i < 100) {
if ((i % 3 == a) && (i % 5 == b) && (i % 7 == c)) {
printf("%d\n", i);
return 0;
}
@wingofthestar
wingofthestar / 水仙花数
Last active July 4, 2017 03:23
输出1000~999中的所有水仙花数,若3位数ABC满足ABC=A^3 + B^3 + C^3,则称其为水仙花数
#include<stdio.h>
#include<math.h>
int main() {
for (int i = 100; i <= 999; i++) {
int a = i % 10;
int b = i / 10 % 10;
int c = i / 100;
if (i == (pow(a, 3) + pow(b, 3) + pow(c, 3))) {
printf("%d\n", i);
}