小球落地五次高度
假设一个球从任意高度自由落下,每次落地后反跳回原高度的一半; 再落下, 求它在第5次落地时,共经历多少米?第5次反弹多高?
最后的误差判断是小数点6位
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str;
while ((str = br.readLine()) != null) {
double height = Integer.parseInt(str);
System.out.println(get(height));
System.out.println(height(height));
}
}
public static double height(double h) {
for (int i = 0; i < 5; i++) {
h = h / 2;
}
return h;
}
public static double get(double h) {
return h + h + h / 2 + h / 4 + h / 8;
}
}
#include<stdio.h>
double jumpHigh(double a ,int count)
{
if(count == 1)
return a/2;
else
return jumpHigh(a/2,count-1);
}
int main(){
double a =0;
while(scanf("%lf",&a)!= EOF)
{
double buf = a;
double temp = 0;
int count = 0;
for(count = 0;count<5;count++)
{
temp += buf +buf/2;
buf /= 2;
}
temp -= buf;
printf("%g\n",temp);
printf("%g\n",jumpHigh(a,5));
}
return 0;
}
统计字符个数
输入一行字符,分别统计出包含英文字母、空格、数字和其它字符的个数。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = "";
while((line = br.readLine())!=null)
{
int EnglishCharCount=0;
int BlankCharCount=0;
int NumberCharCount=0;
int OtherCharCount=0;
char[] chs = line.toCharArray();
for(int i=0;i<chs.length;++i){
if((chs[i]>='a'&&chs[i]<='z')||(chs[i]>='A'&&chs[i]<='Z')){
++EnglishCharCount;
continue;
}
else if(chs[i]==' '){
++BlankCharCount;
continue;
}
else if(chs[i]>='0'&&chs[i]<='9'){
++NumberCharCount;
continue;
}
else
++OtherCharCount;
}
System.out.println(EnglishCharCount);
System.out.println(BlankCharCount);
System.out.println(NumberCharCount);
System.out.println(OtherCharCount);
}
}
}
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str;
while(getline(cin,str))
{
int a=0,b=0,c=0,d=0;
for(int i=0;i<str.size();i++)
{
if(isalpha(str[i])) a++;
else if(str[i]==' ') b++;
else if(str[i]>='0'&&str[i]<='9') c++;
else if(ispunct(str[i])) d++;
}
cout<<a<<endl<<b<<endl<<c<<endl<<d<<endl;
}
return 0;
}
迷宫问题
定义一个二维数组N*M(其中2<=N<=10;2<=M<=10),如5 × 5数组下所示:
int maze[5][5] = {
0, 1, 0, 0, 0,
0, 1, 0, 1, 0,
0, 0, 0, 0, 0,
0, 1, 1, 1, 0,
0, 0, 0, 1, 0,
};
它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。入口点为[0,0],既第一空格是可以走的路。
在这里插入代码片
#include <stdio.h>
void trace(int x, int y);
typedef struct point {
int x;
int y;
}point;
int n, m;//行、列
int count = 0;
int num[10][10];
point stack[100];
int main() {
while (scanf("%d %d", &n, &m) != EOF) {
for (int i = 0; i<n; i++) {
for (int j = 0; j<m; j++)
scanf("%d", &num[i][j]);
}
trace(0, 0);
}
return 0;
}
void trace(int x, int y) {
point p = { x,y };
stack[count++] = p;//轨迹初始
num[x][y] = 1;//走过的设为1
if (x == n - 1 && y == m - 1) {//递归结束的条件,到达右下角
for (int i = 0; i<count; i++){
printf("(%d,%d)\n", stack[i].x, stack[i].y);
}
count = 0;
//return ;
}
else {
if (y + 1<m&&num[x][y + 1] == 0)//右
trace(x, y + 1);
else if (x + 1<n&&num[x + 1][y] == 0)//下
trace(x + 1, y);
else if (y - 1 >= 0 && num[x][y - 1] == 0)//左
trace(x, y - 1);
else if (x - 1 >= 0 && num[x - 1][y] == 0)//上
trace(x - 1, y);
else {
count--;
num[x][y] = 0;
}
}
}
名字的漂亮度
给出一个名字,该名字有26个字符串组成,定义这个字符串的“漂亮度”是其所有字母“漂亮度”的总和。
每个字母都有一个“漂亮度”,范围在1到26之间。没有任何两个字母拥有相同的“漂亮度”。字母忽略大小写。
给出多个名字,计算每个名字最大可能的“漂亮度”。
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str;
while ((str = br.readLine()) != null) {
int n = Integer.parseInt(str);
for(int i=0;i<n;i++){
String s=br.readLine();
char[] c=s.toCharArray();
int[] count=new int[150];
for(int j=0;j<c.length;j++){
count[c[j]]++;
}
Arrays.sort(count);
int a=26;
int sum=0;
for(int k=count.length-1;k>=0;k--){
if (count[k] == 0) {
break;
}
sum+=count[k]*(a--);
}
System.out.println(sum);
}
}
}
}
#include <stdio.h>
#include <string.h>
int main()
{
int n, i, len, j, k, temp,s;
char str[10000];
while(scanf("%d", &n) != EOF)
{
for(k = 0; k < n; k++){
scanf("%s", &str);
int hash[26]={0};
len = strlen(str);
for(i = 0; i < len; i++){
if(str[i] >= 'A' && str[i] <= 'Z')
{
hash[str[i]-'A']++;
}
else
{
hash[str[i]-'a']++;
}
}
for(i = 0; i < 25; i++){
for(j = 25; j > i; j--){
if(hash[j] > hash[j - 1]){
temp = hash[j];
hash[j] = hash[j - 1];
hash[j - 1] = temp;
}
}
}
s=0;
for(i = 0; i < 26; i++){
s += (hash[i] *(26-i));
}
printf("%d\n", s);
}
}
}
按字节截取字符串
编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串。但是要保证汉字不被截半个,如"我ABC"4,应该截为"我AB",输入"我ABC汉DEF"6,应该输出为"我ABC"而不是"我ABC+汉的半个"。
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str;
while ((str = br.readLine()) != null) {
String[] s = str.split(" ");
int num = Integer.parseInt(s[1]);
int count = 0;
for (int i = 0; i < s[0].length(); i++) {
char c = s[0].charAt(i);
if (c < 128) {
count++;
} else {
count += 2;
}
if (count == num) {
System.out.println(s[0].substring(0, i+1));
break;
} else if (count > num) {
System.out.println(s[0].substring(0, i));
break;
}
}
}
}
}
#include<stdio.h>
#include<string.h>
char a[1000];
int main()
{
int n;
while( scanf("%s",a)!= EOF )
{
scanf("%d",&n);
if( n < strlen(a) )
{
if( a[n-1] < 0 && a[n] < 0 )
n--;
}
else
n = strlen(a);
char b[1000];
strncpy( b , a , n );
b[n] = '\0';
printf("%s\n",b);
}
return 0;
}