重温基础

新手上路。打一下基础

1  if

if(判定条件){ 语句; }

else   { 语句; }//否则,即if判定条件为false后执行

eg:P1422 小玉家的电费

 

 #include<bits/stdc++.h>
 using namespace std;
 
double a,s;
 
int main()
{
    cin>>a;
    if(a<=150) s=a*0.4463;
else if(a<=400) s=(a-150)*0.4663+150*0.4463
else s=(a-400)*0.5663+(400-150)*0.4663+150*0.4463; cout<<floor(s*10+0.5)/10.0; return 0; }

 2  while

while(条件)

{ 语句; }条件成立执行语句;条件为fiase则跳过。

eg:p1423小玉在游泳

#include<bits/stdc++.h>
using namespace std;

double w; 
int s;

int main()
{
    double n=2.0;
    cin>>w;
    while(w>0)
    {
        w=w-n;
        n=n*0.98;
        s++;
    }
    cout<<s;
}

3  for

for( 起始条件;结束条件;递进条件 )

{ 语句; }  额~懂就行

 

 

#include<bits/stdc++.h>
using namespace std;

int n,x,a,s,b;

int main()
{
    cin>>n>>x;
    for(int i=1;i<=n;i++)
    {
        a=i;
        while(a!=0)
        {
            b=a%10;
            if(b==x)
                s++;
            a/=10;
        }
    }
    cout<<s;
    return 0; 
}

猜你喜欢

转载自www.cnblogs.com/berserker-slst/p/10802354.html