‘壹’ 已知字符串s='苹果:12个;梨:20个;桔子:35个;香蕉:18个;',试编写程序,实现以下
public class Test{
public static void main(String[] args) {
String str = "苹果:12个;梨:20个;桔子:35个;香蕉:18个;";
//解决第一个问题
System.out.println("苹果总共有" + getCount(str, "苹果") + "个");
//解决第二个问题
System.out.println("梨和香蕉总共有" + getCount(str, "梨", "香蕉") + "个");
//解决第三个问题
System.out.println(str.replaceAll("桔子", "橙子"));
}
private static Integer getCount(String str, String... name) {
String[] arr = str.split(";");
Map<String, Integer> map = new HashMap();
for (String tmp : arr) {
String[] arr2 = tmp.split(":");
map.put(arr2[0], Integer.valueOf(arr2[1].replace("个", "")));
}
int total = 0;
for (String s : name) {
total = total + map.get(s);
}
return total;
}
}
‘贰’ C语言编程:如果梨子一斤3元,橙子一斤2元,香蕉两斤1元。用45元正好买45斤水果
#include<stdio.h>
int main(){
int pear=3;
int org=2;
int ban=0.5;
int i , j , k;//i , j , k分别表示梨、橙子、香蕉的数量(单位斤)
for( i=0; i<45; i++){
for( j=0; j<45; j++){
for( k=0; k<45; k+=2){
if( pear*i + org*j + ban*k == 45 && i+j+k==45){
printf("梨子:%d,橙子:%d,香蕉:%d
" , i , j , k);
}
}
}
}
}

‘叁’ C语言编程题目:查询水果的单价 有4 种水果,苹果(apple)梨(pear)橘子(orange)和葡萄(grape),单价
#include<stdio.h>
int main(void){
int i,x,n;
float a=3.0,p=2.5,o=4.1,g=10.2;
printf("Enter choice: ");
scanf("%d",&x);
for(i=1;;i++){
switch(x)
{case 0:break;
case 1:
printf("price=%.1f\n",a);
printf("Enter choice:");
scanf("%d",&x);
break;
case 2:
printf("price=%.1f\n",p);
printf("Enter choice: ");
scanf("%d",&x);
break;
case 3:
printf("price=%.1f\n",o);
printf("Enter choice: ");
scanf("%d",&x);
break;
case 4:
printf("price=%.1f\n",g);
printf("Enter choice: ");
scanf("%d",&x);
break;
default:
printf("price=0\n");
printf("Enter choice: ");
scanf("%d",&x);}
n++;
if(n==6){
printf("Thank you!");
break;}
}
return 0;
}
‘肆’ 编写一个C语言程序,求:用40元钱买苹果、梨和西瓜,总数能买100个,已知苹果为0.4元一个,梨0.2元一个,
#include <stdio.h>
main(){
int na,np,nw,n; // 个数,总个数
double va=0.4,vp=0.2,vw=4; // 单价
double v; //总费用
for (na=0;na<=100;na++)
for (np=0;np<=100;np++)
for (nw=0;nw<=100;nw++){
v=na*va+np*vp+nw*vw;
n=na+np+nw;
if (n==100 && v==40.0) //若总个数和总费用符合要求,则输出结果:
printf("Apple=%d Pear=%d Watermel=%d\n",na,np,nw);
}
return 0;
}
结果:
Apple=5 Pear=90 Watermel=5
Apple=24 Pear=72 Watermel=4
Apple=43 Pear=54 Watermel=3
Apple=62 Pear=36 Watermel=2
Apple=81 Pear=18 Watermel=1
Apple=100 Pear=0 Watermel=0
‘伍’ c程序编写一个水果店售货员算账的程序
#include<stdio.h>
void main()
{
float n[4],apple=2.5,pear=1.8,banana=2,orange=1.6,price,charge,money;
printf("请输入水果重量苹果 鸭梨 香蕉 橘子 (不买的水果请输入0): \n");
int i;
for(i=0;i<4;i++)scanf("%f",&n[i]);
printf("应付钱 %.2f 元\n",price=apple*n[0]+pear*n[1]+banana*n[2]+orange*n[3]);
printf("请输入付款数:");scanf("%f",&money);
printf("应找钱 %.2f 元\n",money-price);
}
‘陆’ 请教C语言题目
/*1.编写一个C 程序,显示下面的提示:
Enter the length of the room:
Enter the width of the room:
在显示每个提示之后,你的程序可以接收键盘的数据。
在输入房间的长度(length)和宽度(width)之后,该程序可以计算和显示房间的面积。
*/
#include<stdio.h>
#include <stdlib.h>
void main()
{
float length,width,area;
char a;
loop: printf("Please enter the length of the room: ");
fflush(stdin); //清除键盘缓冲区
scanf("%f",&length);
printf("Please enter the width of the room: ");
fflush(stdin);
scanf("%f",&width);
area=length*width;
printf("According to your information, the area of this room is: %.4f\n",area);
printf("Continue or not? Please entre Y/N:"); //可以在不退出的情况下多次运行程序得出不同结果
fflush(stdin);
scanf("%c",&a);
if(a=='y'||a=='Y')
goto loop;
else
exit(0);
}
/*运行结果:
Please enter the length of the room: 12.3
Please enter the width of the room: 14.5
According to your information, the area of this room is: 178.3500
Continue or not? Please entre Y/N:y
Please enter the length of the room: 24.3
Please enter the width of the room: 13.6
According to your information, the area of this room is: 330.4800
Continue or not? Please entre Y/N:n*/
/*2..编写一个程序从键盘输入圆柱体的半径r和高度h,计算其体积。
*/
#include<iostream>
using namespace std;
void main()
{
double r,h,pi;char a;
pi=3.14;
loop: cout<<"请输入圆柱体的半径:";
cin>>r;
cout<<"请输入圆柱体的高:";
cin>>h;
cout<<"圆柱体的体积="<<r*r*h*pi<<endl;
cout<<endl<<"继续吗? 请输入 Y/N:";
fflush(stdin);
cin>>a;
if(a=='y'||a=='Y')
goto loop;
else
exit(0);
}
/*运行结果:
请输入圆柱体的半径:3
请输入圆柱体的高:4
圆柱体的体积=113.04
继续吗? 请输入 Y/N:n*/
//哎,用c++做简单多了
//老兄还是学c++吧
/*3.编写一个程序用于超市中的记账:
已知苹果每千克5.0元,鸭梨每千克3.4元,香蕉每千克4.0元,橘子每千克2.4元,
要求输入各类水果的销售重量,计算并输出应收款的数额。*/
#include<iostream>
using namespace std;
void main()
{
double apple,pear,banana,orange;
double apple_amount,pear_amount,banana_amount,orange_amount;
apple=5.0;
pear=3.4;
banana=4.0;
orange=2.4;
printf("请分别输入各类水果的销售重量:\n");
printf("苹果:");
cin>>apple_amount;
printf("鸭梨:");
cin>>pear_amount;
printf("香蕉:");
cin>>banana_amount;
printf("橘子:");
cin>>orange_amount;
cout<<"应付:"<<apple_amount*apple+pear_amount*pear+banana_amount*banana+orange_amount*orange<<endl;
}
/*运行结果:
请分别输入各类水果的销售重量:
苹果:1.2
鸭梨:1.1
香蕉:2.3
橘子:0
应付:18.94
*/
‘柒’ 问一道简单C语言题
#include<stdio.h>
main()
{
double a,b,c,d,money;
printf("请输入苹果的重量\n");
scanf("%lf",&a);
printf("请输入鸭梨的重量\n");
scanf("%lf",&b);
printf("请输入香蕉的重量\n");
scanf("%lf",&c);
printf("请输入橘子的重量\n");
scanf("%lf",&d);
printf("应付钱数为%lf\n",2.50*a+1.80*b+2*c+1.6*d);
printf("请输入客户付款数\n");
scanf("%lf",&money);
printf("应找钱数%lf\n",money-2.50*a+1.80*b+2*c+1.6*d);
}