学英语
Jessi初学英语,为了快速读出一串数字,编写程序将数字转换成英文:
如22:twenty two,123:one hundred and twenty three。
说明:
数字为正整数,长度不超过九位,不考虑小数,转化结果为英文小写;
输出格式为twenty two;
非法数据请返回“error”;
关键字提示:and,billion,million,thousand,hundred。
本题含有多组输入数据。
import java.util.*;
import java.io.*;
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){
Integer l = Integer.valueOf(line);
System.out.println(parse(l));
}
}
public static String parse(int num){
String[] numStr={"zero","one","two","three","four","five",
"six","seven","eight","nine","ten","eleven","twelve",
"thirteen","fourteen","fifteen","sixteen","seventeen",
"eighteen","ninteen"};
if(num>=0 && num<20){
return numStr[num];
}else if(num>=20 && num<100){
int a=num%10;
if(num<30){
return a !=0?"twenty "+parse(a):"twenty";
}else if(num<40){
return a !=0?"thirty "+parse(a):"thirty";
}else if(num<50){
return a !=0?"forty "+parse(a):"forty";
}else if(num<60){
return a !=0?"fifty "+parse(a):"fifty";
}else if(num<70){
return a !=0?"sixty "+parse(a):"sixty";
}else if(num<80){
return a !=0?"seventy "+parse(a):"seventy";
}else if(num<90){
return a !=0?"eighty "+parse(a):"eighty";
}else if(num<100){
return a !=0?"ninety "+parse(a):"ninety";
}
}else if(num>=100 && num<1000){
int x=num/100;
int y=num%100;
if(y!=0){
return parse(x)+" hundred"+ " and "+ parse(y);
}else{
return parse(x)+" hundred";
}
}
else if(num>=1000 && num<1000000){
int x=num/1000;
int y=num%1000;
if(y!=0){
return parse(x)+" thousand "+ parse(y);
}else{
return parse(x)+" thousand";
}
}else if(num>=1000000 && num<100000000){
int x=num/1000000;
int y=num%1000000;
if(y!=0){
return parse(x)+" million "+ parse(y);
}else{
return parse(x)+" million";
}
}
return "error";
}
}
#include <stdio.h>
#include <string.h>
char num_o[][12] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten",
"eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
char num_ty[][12] = {"", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};
char *changeToStr_999(int num){
char retS[1000] = "";
int arr[3] = {0,0,0};
int c_num = num % 1000;
arr[2] = (int)(c_num / 100);
c_num = num % 100;
//如果数值大于100
if(arr[2] > 0){
strcat(retS, num_o[arr[2]]);
strcat(retS, " ");
strcat(retS, "hundred");
}
if(c_num >= 20){
//如果有百位,需加空格
if(arr[2] > 0) strcat(retS, " and ");
strcat(retS, num_ty[(int)(c_num / 10)]);
c_num = num % 10;
//如果个十位数值大于等于20,另外确定个位数
if(c_num > 0){
strcat(retS, " ");
strcat(retS, num_o[c_num]);
}
}else if(c_num < 20){
//如果 num >= 20 则前面已有东西,需加空格
if(num >= 20) strcat(retS, " and ");
strcat(retS, num_o[c_num]);
}
printf("%s", retS);
return "";
};
int main(){
int x=0;
while(scanf("%d", &x) != EOF){
if(x< 0 || x > 10000000000 - 1){
printf("error");
continue;//不符合重来
}
int bit_billion = x / (1000000000);
x = x % (1000000000);
if(bit_billion > 0){
changeToStr_999(bit_billion);
printf(" billion");
}
int bit_million = x / (1000000);
x = x % (1000000);
if(bit_million > 0){
if(bit_billion > 0) printf(" ");
changeToStr_999(bit_million);
printf(" million");
}
int bit_thousand = x / (1000);
x = x % (1000);
if(bit_thousand > 0){
if(bit_million > 0) printf(" ");
changeToStr_999(bit_thousand);
printf(" thousand");
}
int bit_one = x;
if(bit_one > 0){
if(bit_thousand > 0) printf(" ");
changeToStr_999(bit_one);
}
printf("\n");
}
return 0;
}
线性插值
信号测量的结果包括测量编号和测量值。存在信号测量结果丢弃及测量结果重复的情况。
1.测量编号不连续的情况,认为是测量结果丢弃。对应测量结果丢弃的情况,需要进行插值操作以更准确的评估信号。
采用简化的一阶插值方法,由丢失的测量结果两头的测量值算出两者中间的丢失值。
假设第M个测量结果的测量值为A,第N个测量结果的测量值为B。则需要进行(N-M-1)个测量结果的插值处理。进行一阶线性插值估计的第N+i个测量结果的测量值为A+( (B-A)/(N-M) )*i (注:N的编号比M大。)
例如:只有测量编号为4的测量结果和测量编号为7的测量结果,测量值分别为4和10
则需要补充测量编号为5和6的测量结果。
其中测量编号为5的测量值=4 + ((10-4)/(7-4))*1 = 6
其中测量编号为6的测量值=4 + ((10-4)/(7-4))*2 = 8
2.测量编号相同,则认为测量结果重复,需要对丢弃后来出现的测量结果。
请根据以上规则进行测量结果的整理。
输入描述:
输入说明
1 输入两个整数m, n
2 输入m个数据组
输出描述:
输出整理后的结果
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
String line;
while ((line=bufferedReader.readLine()) != null){
int total = Integer.parseInt(line.split(" ")[0]);
if(total>0){
StringBuffer sb = new StringBuffer();
String thisline = bufferedReader.readLine();
String[] elements = thisline.split(" ");
int num = Integer.parseInt(elements[0]);
int value = Integer.parseInt(elements[1]);
int preNum = num;
int preValue = value;
sb.append(thisline).append("\n");
for(int i=1;i<total;i++){
thisline = bufferedReader.readLine();
elements = thisline.split(" ");
num = Integer.parseInt(elements[0]);
value = Integer.parseInt(elements[1]);
if(num==preNum){
continue;
}
for(int j=preNum+1; j<num; j++){
sb.append(j).append(" ").append(preValue + (value - preValue) / (num - preNum) * (j - preNum)).append("\n");
}
preNum=num;
preValue=value;
sb.append(thisline).append("\r\n");
}
System.out.print(sb.toString());
}
}
}
}
#include<iostream>
#include<vector>
using namespace std;
int main()
{
int m,n;
while(cin>>m>>n)
{
int M,N,A,B;
cin>>M>>A;
cout<<M<<" "<<A<<endl;
for(int i=1;i<m;++i)
{
cin>>N>>B;
if(N==M) continue;
else
{
for(int j=1;j<N-M;++j)
{
cout<<M+j<<" "<<A+((B-A)/(N-M))*j<<endl;
}
cout<<N<<" "<<B<<endl;
M=N;
A=B;
}
}
}
return 0;
}
输出单向链表中倒数第K个节点
输入一个单向链表,输出该链表中倒数第k个结点,链表的倒数第1个结点为链表的尾指针。
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 input = null;
while((input = br.readLine())!=null){
//候选人数
int num = Integer.parseInt(input);
String[] numStr = br.readLine().split(" ");
int num2 = Integer.parseInt(br.readLine().trim());
if(num2<=0||num2>numStr.length){
System.out.println(num2);
}else{
System.out.println(numStr[numStr.length-num2]);
}
}
}
}
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
struct ListNode
{
int m_nkey;
ListNode *m_pNext;
ListNode (int value):m_nkey(value) {m_pNext=NULL;}
};
int main()
{
int n;
while(cin>>n)
{
if(n==0)
return NULL;
ListNode *head=NULL,*cp;
for(int i=0;i<n;i++)
{
int k;
cin>>k;
ListNode *tmp=new ListNode(k);
if(head==NULL)
{
head=tmp;
cp=tmp;
}
else
{
cp->m_pNext=tmp;
cp=tmp;
}
}
//输入k的值并返回倒数第k个值
int k;
cin>>k;
if(k>n||k<0)
cout<<"NULL"<<endl;
else if(k==0)
cout<<"0"<<endl;
else
{
ListNode *ap=NULL;
cp=head;
while(--k)
{
cp=cp->m_pNext;
}
while(cp)
{
ap=head;
head=head->m_pNext;
cp=cp->m_pNext;
}
if(ap==NULL)
cout<<head->m_nkey<<endl;
else
cout<<ap->m_nkey<<endl;
}
}
return 0;
}