『壹』 已知字元串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);
}